Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL table variable initialization

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};
like image 666
CodeKingPlusPlus Avatar asked Sep 25 '12 18:09

CodeKingPlusPlus


People also ask

How do you DECLARE a table variable in SQL?

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.

How do you initialize a variable in SQL query?

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.

Which is the correct way to DECLARE a table variable?

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.


2 Answers

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

like image 131
Conrad Frix Avatar answered Sep 30 '22 15:09

Conrad Frix


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.

like image 29
HABO Avatar answered Sep 30 '22 15:09

HABO