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
 Transact-SQL (2000)
 Create tbl using Variable

Author  Topic 

Richard Branson
Yak Posting Veteran

84 Posts

Posted - 2005-04-12 : 03:31:57
Hi

I want to find out if it's possible to create a tbl using a variable name. Like in this example:


Declare @Period varchar(23)

Select @Period = 'Newunits_'
+ Cast(Month(Max(Load_Period)) as Varchar)
+ '_' + Cast(Year(Max(Load_Period))as Varchar)
+ '_Matched'
from load_Period

--Load_Period period is a datetime


Select Tax_Nbr
Into @Period
from Newunits


I get the following error:
Server: Msg 170, Level 15, State 1, Line 11
Line 11: Incorrect syntax near '@Period'.


You can't teach an old mouse new clicks.

Kristen
Test

22859 Posts

Posted - 2005-04-12 : 03:45:20
Nope, you can't do that.

DECLARE @strSQL varchar(8000)
SELECT @strSQL =
'Select Tax_Nbr
Into ' + @Period + '
from Newunits'

EXEC (@strSQL)

should be OK

Kristen
Go to Top of Page

Richard Branson
Yak Posting Veteran

84 Posts

Posted - 2005-04-12 : 04:22:43
Why didn't I think of that
D-SQL

Thanks Kristen

You can't teach an old mouse new clicks.
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2005-04-12 : 05:02:16
You should consider why you are having to do this though. It may be indicative of a design that could use improvement.

-------
Moo. :)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-04-12 : 12:16:27
Good point Mr Mist

Richard: I can offer you my first class consultancy service to help you solve that problem. First Class Fare to ZA will be required, of course!

Kristen
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-04-12 : 13:36:51
Just create 1 table, and add a datetime column....



Brett

8-)
Go to Top of Page
   

- Advertisement -