Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Create a temporary table in Stored Proc using variables

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-01-30 : 13:23:07
Mark writes "I need to be able to create a temporary table in a stored procedure using variables created in the proc. e.g I want a table with columns named Y1999, Y2000, Y2001, but the 'year' bits to be created by

SELECT @YR_Last1 = DATEPART(yy,GETDATE()) - 1 for 2000, etc.

I tried this:

SELECT @PrintCommand="CREATE TABLE #CBC06CustYrs
(
Product_Name varchar(40),
Y" + convert(varchar,@YR_Last2) + " money,
Y" + convert(varchar,@YR_Last1) + " money,
Y" + convert(varchar,@YR_This) + " money,
Overall money
)"

PRINT @PrintCommand

and the output shows a correct CREATE TABLE statement.

However, it doesn't work! I'm stumped! (SQL Server 6.5, SP5)"
   

- Advertisement -