CodeKingPlusPlus CodeKingPlusPlus - 2 years ago 82
SQL Question

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};

Answer Source

Well you can't. The best you can do is something like this

Insert Into @NumDaysMonth

Then retrieval might be something like

DECLARE @LookItUp int

SELECT @LookItUp = num_days 
FROM @NumDaysMonth
WHERE month_Id = 12;

PRINT @LookItUp 

SQL Fiddle Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download