Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Name a Table by using @Variable 2

Status
Not open for further replies.

wg26

Programmer
Mar 21, 2002
135
US
Hi everyone:

I am trying to ceate many tables on the fly with different names. I am using GUID,which I retrieved from another table as new tables names..code is as the following...but SQL keeps giving me syntax error, where @VariableName is being used...Can anyone please kindly take a look at my code and let me know what is wrong and what is the solution for it? Thanks alot

Declare CursorTable Cursor
Global
Static
for
Select [Lookup ID] ---GUID
from [Detail Name Lookup]

declare @TableName varchar(50)
Open CursorTable
Fetch next from CursorTable into @TableName

while @@Fetch_status = 0
begin
create table @TableName
(
[PK Run Detail] bigint identity(1,1) primary key not null,
[FK Run Analyses] uniqueidentifier default newid() not null,
[Detail Name] varchar(300) not null,
[Detail NumData] float not null,
)
Fetch next from CursorTable into @TableName
end
deallocate CursorTable
 
Hi,

Try this code

Declare CursorTable Cursor
Global
Static
for
Select [Lookup ID] ---GUID
from [Detail Name Lookup]

declare @TableName varchar(50)
Declare @SQL Varchar(1000)
Open CursorTable
Fetch next from CursorTable into @TableName

while @@Fetch_status = 0
begin
SET @SQL = 'create table ' + @TableName
+ '(
[PK Run Detail] bigint identity(1,1) primary key not null,
[FK Run Analyses] uniqueidentifier default newid() not null,
[Detail Name] varchar(300) not null,
[Detail NumData] float not null,
)'

exec @SQL
Fetch next from CursorTable into @TableName
end
deallocate CursorTable



Sunil
 

Hope this helps

declare @cmd varchar (1000)

set @cmd = 'create table ' + @TableName +
'(
[PK Run Detail] bigint identity(1,1) primary key not null,
[FK Run Analyses] uniqueidentifier default newid() not null,
[Detail Name] varchar(300) not null,
[Detail NumData] float not null,
)'

exec (@cmd)

cjw
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top