I have the following TSQL table variable:
declare @NumDaysMonth table
(
month_id smallint,
num_days smallint
)
I just want a quick look-up for the number of days in each month. How can I initialize this table like a C array:
int numDaysMonth[] = {31, 28, 30, ... , 31};
To declare a table variable, start the DECLARE statement. The name of table variable must start with at(@) sign. The TABLE keyword defines that used variable is a table variable. After the TABLE keyword, define column names and datatypes of the table variable in SQL Server.
Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.
Syntax. If we want to declare a table variable, we have to start the DECLARE statement which is similar to local variables. The name of the local variable must start with at(@) sign. The TABLE keyword specifies that this variable is a table variable.
Well you can't. The best you can do is something like this
Insert Into @NumDaysMonth
Values
(1,31),
(2,28),
(3,31),
...
(12,31);
Then retrieval might be something like
DECLARE @LookItUp int
SELECT @LookItUp = num_days
FROM @NumDaysMonth
WHERE month_Id = 12;
PRINT @LookItUp
SQL Fiddle Demo
The following does not address the OP's question of initializing a table. You are welcome to treat it as a formatted comment.
A trick that is handy for the odd lookup table is to create a virtual table on the fly:
declare @Foo as Table ( Month Int )
insert into @Foo values ( 1 ), ( 3 ), ( 9 )
select *
from @Foo as F inner join
( select month_id, num_days
from ( values
( 1, 31 ), ( 2, 28 ), ( 3, 31 ), ( 4, 30 ), ( 5, 31 ), ( 6, 30 ),
( 7, 31 ), ( 8, 31 ), ( 9, 30 ), ( 10, 31 ), ( 11, 30 ), ( 12, 31 )
) as NumDaysMonth( month_id, num_days ) ) as NumDaysMonth on
NumDaysMonth.month_id = F.Month
For getting the number of days in a month I would be more inclined to create a function that takes the year and month and returns the correct value. When I need a quick one off translation from some code to something readable the un-table is convenient.
If you need to refer to the faux table a few times in one place:
; with NumDaysMonth as (
( select month_id, num_days
from ( values
( 1, 31 ), ( 2, 28 ), ( 3, 31 ), ( 4, 30 ), ( 5, 31 ), ( 6, 30 ),
( 7, 31 ), ( 8, 31 ), ( 9, 30 ), ( 10, 31 ), ( 11, 30 ), ( 12, 31 )
) as NumDaysMonth( month_id, num_days ) ) ),
FooMonths as (
select *
from @Foo as F inner join
NumDaysMonth as NDM on NDM.month_id = F.Month ),
FooWithFollowingMonths as (
select *
from FooMonths
union
select *
from @Foo as F inner join
NumDaysMonth as NDM on NDM.month_id = F.Month + 1 )
select *
from FooWithFollowingMonths
Beyond that the lookup table should probably be kept as a real table or table valued function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With