mardi 26 mai 2015

How to generate tables in MS SQL?

I need to generate tables like 'test_table_1', 'test_table_2'... What I am doing wrong?

declare @i int = 1;
declare @i_str varchar(1024);
declare @table VARCHAR(1024);


while (@i < 500)
BEGIN
SET @i = @i + 1;
SET @table = 'TEST_TABLE_' + convert(varchar, @i);

IF NOT EXISTS(SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'dbo' 
                 AND  TABLE_NAME = @table) 

CREATE TABLE @table (
        ID INT,
        userid INT,
        name TEXT,
        last_name TEXT,
        country TEXT,
        city TEXT
)

END

Error: Incorrect syntax near @table. Expecting '.', ID or QUOTED_ID

Aucun commentaire:

Enregistrer un commentaire