简体   繁体   中英

T-SQL Create new tables based on another table

I am new to T-SQL so have zero knowledge & am using SQL Server 2012. Currently I have a table called dbo.Securities which contains two columns. One column is called PairName ( nchar(15) ) & the other column is called RunPair ( bit ). This table contains 30 rows.

What I would like to do is create 30 new tables that all have the same structure. The structure is one column of type datetime which will be the primary key & must go down to the second. There are 12 other columns all of type decimal(5,5) .

The 30 tables would be named after the PairName column in the table dbo.Securities . I have been trying to find how out to loop through the dbo.Securities table and use the PairName to create a new table based on the structure mentioned above.

However after reading some guides it appears looping through a table is not the best way to go about trying to complete this task.

Note, that you'll want to edit the table schema part in the below code. This loop will build thirty tables. As far as the best and fastest approach? Using TSQL this would be faster than manually building each table, but I don't know if it's the fastest method overall.

DECLARE @Build TABLE(
        ID SMALLINT IDENTITY(1,1),
        TableName VARCHAR(250)
)

INSERT INTO @Build
SELECT PairName
FROM dbo.Securities

DECLARE @begin SMALLINT = 1, @max SMALLINT, @sql NVARCHAR(MAX), @table NVARCHAR(250)
SELECT @max = MAX(TableID) FROM @Build

WHILE @begin <= @max
BEGIN

        SELECT @table = TableName FROM @Build WHERE ID = @begin

        SET @sql = 'CREATE TABLE ' + @table + '(
        -- EDIT TABLE COLUMNS HERE
        )'

        EXECUTE sp_executesql @sql

        PRINT 'TABLE ' + @table + ' has been built.'

        SET @begin = @begin + 1

END

Are you sure you need to create 30 tables with the exact same structure? Can you elaborate on what you are trying to do? This seems like a potential database or application design issue, and unless you are creating an application installer of some sort which needs to set up an initial database, there is quite likely a better way to accomplish what you are doing.

Even if it turns out creating 30 tables is, indeed, the best approach to your problem, it is potentially something that might be better handled by application/business logic instead of SQL. Again, depends on what you are trying to do.

Beyond the points above, it seems to me there really aren't any better ways to do what you describe (using T-SQL) above other than iterating over the 30 rows.

You can do this with a cursor to loop through the securities table, and then dynamic sql to create the tables. Looping is a perfectly acceptable way to perform a number of create tables. There's no real way to do that in a set oriented way, which is the usual reason people say to avoid cursors.

declare 
    @sql nvarchar(max),
    @pair sysname;

declare securities_cursor cursor local fast_forward for
select PairName from Securities;

open securities_cursor;

fetch next from securities_cursor into @pair;

while @@fetch_status = 0
begin
    set @sql = '
    Create Table dbo.' + quotename(@pair) + ' (
        datekey datetime2(0) not null constraint ' + quotename('PK_' + @pair) + ' primary key,
        column1 decimal(10, 5),
        column2 decimal(10, 5),
        column3 decimal(10, 5),
        column4 decimal(10, 5),
        column5 decimal(10, 5) -- etc
    );'
    exec sp_executesql @sql;

    fetch next from securities_cursor into @pair;
end

close securities_cursor;
deallocate securities_cursor;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM