Thursday, January 14, 2010

SQL Script to Generate Your Time Dimension Table

Do you need a Time Dimension in your warehouse? Use my script (included below) to generate your time dimension database table from scratch. The table allows for a dimension to be built with three or fewer layers - Time of Day (Hour Category), Hour and Minute

For Hour Categories I use: Wee Hours, Morning, Noon Hour, Afternoon, Evening, Night and Midnight Hour. Obviously, clients generally want these changed to fit their exact needs to I have made the editing of those values super obvious and easy.

[Click on this graphic to enlarge the image]

===================================================================
CREATE TABLE #24hours (
HourID int,
HourName char(15),
[24Hour] varchar(2),
[12Hour] varchar(2),
AMPM varchar(2),
HourCategory varchar(15),
JoinID int
)

--this routine could have been done with far fewer lines of code
--had I used a looping structure, however, I wanted people to be
--able to quickly and easily understand what would be entered and
--be able to quickly and easily make modifications to suit their needs
insert into #24hours
(HourID, HourName,[24Hour],[12Hour],AMPM, HourCategory)
select 0, '12 Midnight','00',12,'AM', 'Midnight Hour'
union
select 1, '1:00 AM','01',1,'AM', 'Wee Hours'
union
select 2, '2:00 AM','02',2,'AM', 'Wee Hours'
union
select 3, '3:00 AM', '03',3,'AM', 'Wee Hours'
union
select 4, '4:00 AM', '04',4,'AM', 'Wee Hours'
union
select 5, '5:00 AM', '05',5,'AM', 'Wee Hours'
union
select 6, '6:00 AM', '06',6,'AM', 'Morning'
union
select 7, '7:00 AM', '07',7,'AM', 'Morning'
union
select 8, '8:00 AM', '08',8,'AM', 'Morning'
union
select 9, '9:00 AM', '09',9,'AM', 'Morning'
union
select 10, '10:00 AM', '10',10,'AM', 'Morning'
union
select 11, '11:00 AM', '11',11,'AM', 'Morning'
union
select 12, '12 Noon', '12',12,'PM', 'Noon Hour'
union
select 13, '1:00 PM', '13',1,'PM','Afternoon'
union
select 14, '2:00 PM', '14',2,'PM','Afternoon'
union
select 15, '3:00 PM', '15',3,'PM','Afternoon'
union
select 16, '4:00 PM', '16',4,'PM','Afternoon'
union
select 17, '5:00 PM', '17',5,'PM','Evening'
union
select 18, '6:00 PM', '18',6,'PM','Evening'
union
select 19, '7:00 PM', '19',7,'PM','Evening'
union
select 20, '8:00 PM', '20',8,'PM','Night'
union
select 21, '9:00 PM', '21',9,'PM','Night'
union
select 22, '10:00 PM', '22',10,'PM','Night'
union
select 23, '11:00 PM', '23',11,'PM','Night'


CREATE TABLE #60Minutes (
JoinID int,
MinString varchar(2),
)

declare @counter int
set @counter = 1

while @counter <>
begin
insert into #60Minutes (MinString)
select Right('00' + convert(varchar, @counter),2)
set @counter = @counter + 1
end


Create table dbo.dim_Time
([TimeID] [int] IDENTITY(1,1) NOT NULL,
[24HourInt] int NOT NULL,
[12HourInt] int NOT NULL,
[MinInt] int NOT NULL,
HourName varchar(15) NOT NULL,
AMPM varchar(2) NOT NULL,
[12HourName] varchar(8) NOT NULL,
[24HourName] varchar(5) NOT NULL,
HourCategory varchar(50)
) ON [PRIMARY]


INSERT INTO [dim_Time]
([24HourInt]
,[12HourInt]
,[MinInt]
,[HourName]
,[AMPM]
,[12HourName]
,[24HourName]
,[HourCategory])
(Select
HourID as [24HourInt]
,Convert(varchar, [12Hour]) as [12HourInt]
, Convert(int,MinString) as MinInt
, HourName
, AMPM
,[12HourName] = [12Hour] + ':' + MinString + ' ' + AMPM
,[24HourName] = [24Hour] + ':' + MinString
,HourCategory
from #24hours H
join #60Minutes M
on IsNull(H.JoinID,0) = IsNull(M.JoinID,0))

drop table #24hours
drop table #60Minutes

--needs a key
--needs an index

Monday, March 31, 2008

SSIS - Hello World! Part 2

This blog is part two of this: SSIS - Hello World! Part 1

In phase one of the Hello World SSIS package we setup a variable and then set up a script task to have it show us the value of our variable. More helpful than you might think. I use it all the time.

What's even more valuable though is setting the variable on the fly. This can easily be done with another script task - which is what we will do in this blog.

1) Start by renaming your task to something like "Msg Box" or "tell me my variable's value". Also, drag your task down a bit to make room for a task that will come right before it.







2) Pull another script task in from the toolbox, then click on the box to make the green arrow show up. Pull the green arrow and drop it onto the Msg Box task to connect the two in the Control Flow sequence.









3) Double-click to open the new script task. On the script tab set the "PrecompileScript..." property to False. Type the name of your Variable into the ReadWriteVariables property.







4) Click the "Design Script..." button and type in a line to set the variable to some other value. Like this:DTS.Variables("myVar").Value = "Hello Big, Bright, Beautiful World!!!"








5) Run your package to see it work.





Thursday, March 27, 2008

SSIS - Hello World! Part 1

I am almost 40 years old - which means I'm literally from the old school. So when I set about to create my first SSIS package I wanted to do "Hello World!"

In other words, I wanted to define some variables, have the package set those variables and prove to me I had set them. I wasn't able to find anything out there telling me how to do this. I eventually figured it out.

This is how I did it...

We'll approach the task in two phases. I'm calling them phases in order to avoid calling them jobs, tasks or steps - which all have their specific SSIS and SQL Server meanings well defined.

In phase 1 we'll create a variable and present it's value through a message box.

PHASE ONE:
1) Create a project in Visual Studio.
2) Right-click in any blank area on the control pane and choose "Variables" (4th item down). This makes the Variables area show up in a left-side of screen, vertical pane.



3) Click the "Add Variable" button. Name the variable something, set the data type to String and put "Hello World!" or "I ROCK SSIS" into the value field.



4) In the Toolbox, find the Script Task and drag one onto the Control Flow pane.



5) Double-click on the Script Task to open it. On the General "tab" you can change the name if you want. On the Script "tab" set the "PrecompileScriptIntoBinaryCode" property to false. Type the name of your variable into the "ReadOnlyVariables" field and click the "Design Script" button.



6) Type one line into the code area - using the Message Box function...

MsgBox(DTS.Variables("myVar").Value.ToString())



7) Save your way back out the main screen.
8) Right-click on your script task (maybe you renamed it?) and choose "Execute".


The task should turn Yellow, then a message box should pop up, which you will need to dismiss, then it will turn Green.



If it hangs on yellow, look for a message box that may be hidden under your Visual Studio screen. If it hangs too long, just click "Stop" (a blue box in the menu bars along the top). Actually, you'll need to click stop to get back to dev mode anyway.

So that completes the first phase of this little walk-through. You set up a variable and confirmed it's value programmatically. This may seem like a task that will quickly become irrelevant to you but I put one of these confirmation steps in all my packages. After I feel a certain level of confidence, I disable the step and only use it when I am troubleshooting, but it's nice to have them in there. And with SSIS (as opposed to DTS) you can disable a task in line and the package will hop right over it and continue on it's merry way - so these confirmation tasks are easy to develop and convenient to switch off and on.

PHASE 2 is posted here.