如何在 SQL 服务器中动态创建表?
How do I create tables dynamically in SQL Server?
我正在尝试使用另一个 table 中的信息动态创建 table。
例如,有一个 table(比如 table1),其中包含有关要创建的 table 列表的信息。我想使用此 table1 使用模式 St. 动态创建新的 tables,并在名称末尾包含 _New def 即我想创建一个 table 'St.TableA_New' 而不是 table 名称 'TableA' 在 table1 中。这是我使用的代码。
declare @table1 table(idx int identity(1,1), table_name varchar(50))
insert into @table1 (table_name)
select'TableA' union
select'TableB' union
select'TableC'
DECLARE @COUNT INT = 1;
WHILE @COUNT <= (select count(*) from @table1)
BEGIN
Declare @table_name varchar(200) = (select table_name from @table1 where idx=@COUNT);
Declare @new_table varchar(50) = 'St.+'@table_name+'_New';
IF OBJECT_ID(@new_table) IS NOT NULL
DROP TABLE @new_table;
CREATE TABLE @new_table
WITH
(
DISTRIBUTION = ROUND_ROBIN,
HEAP
)
AS
SELECT *
FROM [Ext].[@table_name]
OPTION (LABEL = '');
SET @COUNT = @COUNT + 1
END;
错误显示 'incorrect syntax near '@newtable。'在 'DROP TABLE @new_table;' 行需要 '.'、ID、IF 或 QUOTED_ID'。我应该怎么做才能使用 'table1' table?
中的名称动态创建所有 table
您可以使用 sp_executesql 来执行此操作。只需撤消下面的评论。您可能还需要为架构名称添加代码。
declare @table1 table(idx int identity(1,1), table_name varchar(50))
insert into @table1 (table_name) values ('TableA'), ('TableB'), ('TableC');
Declare @table_name varchar(200)
, @new_table varchar(50)
, @sql nvarchar(1000);
DECLARE @COUNT INT = 1;
WHILE @COUNT <= (select count(*) from @table1)
BEGIN
select @table_name = table_name from @table1 where idx=@COUNT;
set @new_table = @table_name + '_New';
set @sql = concat('drop table if exists ', quotename(@new_table), ';');
set @sql = @sql + 'CREATE TABLE ' + quotename(@new_table) + ' WITH(DISTRIBUTION = ROUND_ROBIN, HEAP) AS SELECT * FROM ' + quotename(@table_name) + ' OPTION (LABEL = '''')';
print @sql;
--exec sys.sp_executesql @sql;
SET @COUNT = @COUNT + 1
END;
我正在尝试使用另一个 table 中的信息动态创建 table。
例如,有一个 table(比如 table1),其中包含有关要创建的 table 列表的信息。我想使用此 table1 使用模式 St. 动态创建新的 tables,并在名称末尾包含 _New def 即我想创建一个 table 'St.TableA_New' 而不是 table 名称 'TableA' 在 table1 中。这是我使用的代码。
declare @table1 table(idx int identity(1,1), table_name varchar(50))
insert into @table1 (table_name)
select'TableA' union
select'TableB' union
select'TableC'
DECLARE @COUNT INT = 1;
WHILE @COUNT <= (select count(*) from @table1)
BEGIN
Declare @table_name varchar(200) = (select table_name from @table1 where idx=@COUNT);
Declare @new_table varchar(50) = 'St.+'@table_name+'_New';
IF OBJECT_ID(@new_table) IS NOT NULL
DROP TABLE @new_table;
CREATE TABLE @new_table
WITH
(
DISTRIBUTION = ROUND_ROBIN,
HEAP
)
AS
SELECT *
FROM [Ext].[@table_name]
OPTION (LABEL = '');
SET @COUNT = @COUNT + 1
END;
错误显示 'incorrect syntax near '@newtable。'在 'DROP TABLE @new_table;' 行需要 '.'、ID、IF 或 QUOTED_ID'。我应该怎么做才能使用 'table1' table?
中的名称动态创建所有 table您可以使用 sp_executesql 来执行此操作。只需撤消下面的评论。您可能还需要为架构名称添加代码。
declare @table1 table(idx int identity(1,1), table_name varchar(50))
insert into @table1 (table_name) values ('TableA'), ('TableB'), ('TableC');
Declare @table_name varchar(200)
, @new_table varchar(50)
, @sql nvarchar(1000);
DECLARE @COUNT INT = 1;
WHILE @COUNT <= (select count(*) from @table1)
BEGIN
select @table_name = table_name from @table1 where idx=@COUNT;
set @new_table = @table_name + '_New';
set @sql = concat('drop table if exists ', quotename(@new_table), ';');
set @sql = @sql + 'CREATE TABLE ' + quotename(@new_table) + ' WITH(DISTRIBUTION = ROUND_ROBIN, HEAP) AS SELECT * FROM ' + quotename(@table_name) + ' OPTION (LABEL = '''')';
print @sql;
--exec sys.sp_executesql @sql;
SET @COUNT = @COUNT + 1
END;