我在存储过程中有以下SQL代码:
CREATE PROCEDURE sp_createAColDiffTable (@table_name nvarchar(128))
AS
CREATE TABLE @table_name (
[table_name] VARCHAR (128) NOT NULL,
[column_name] VARCHAR (128) NULL,
[what_changed] VARCHAR (128) NULL,
[sr_data_type] VARCHAR (128) NOT NULL,
[tr_data_type] VARCHAR (128) NOT NULL,
[sr_max_length] SMALLINT NOT NULL,
[tr_max_length] SMALLINT NOT NULL,
[sr_is_nullable] CHAR NULL,
[tr_is_nullable] CHAR NULL,
[sr_precision] SMALLINT NULL,
[tr_precision] SMALLINT NULL,
[sr_scale] SMALLINT NULL,
[tr_scale] SMALLINT NULL )
ON [PRIMARY]
WITH (DATA_COMPRESSION = NONE)
我像这样调用存储过程:
EXEC [sp_createAColDiffTable] 'hq193.dbo.arch_con_col_s193_s202'
当我运行代码时,我收到错误:
'@ table_name'附近的语法不正确。
当我替换@table_name
的实际表名时,它可以正常工作。
有人可以用我的语法指出问题吗?
答案 0 :(得分:3)
您需要使用动态SQL,因为CREATE TABLE
不采用变量名称。
e.g。
CREATE PROCEDURE sp_createAColDiffTable (@table_name nvarchar(128))
AS
DECLARE @sql nvarchar(MAX)
SET @sql = 'CREATE TABLE ' + @table_name + ' (
[table_name] VARCHAR (128) NOT NULL,
[column_name] VARCHAR (128) NULL,
[what_changed] VARCHAR (128) NULL,
[sr_data_type] VARCHAR (128) NOT NULL,
[tr_data_type] VARCHAR (128) NOT NULL,
[sr_max_length] SMALLINT NOT NULL,
[tr_max_length] SMALLINT NOT NULL,
[sr_is_nullable] CHAR NULL,
[tr_is_nullable] CHAR NULL,
[sr_precision] SMALLINT NULL,
[tr_precision] SMALLINT NULL,
[sr_scale] SMALLINT NULL,
[tr_scale] SMALLINT NULL )
ON [PRIMARY] WITH (DATA_COMPRESSION = NONE)';
EXEC(@sql);
请注意,使用动态SQL可能会导致SQL注入,因此请确保您永远不会将任何用户输入传递给@table_name
。
答案 1 :(得分:1)
@table_name
,而table_name
或#table_name
或##table_name
用于创建表/临时表。
所以你会有
DECLARE @table TABLE (
ID INT,
....
和
CREATE TABLE #table_name (
ID INT,
....
如果您希望动态创建表,则必须使用动态sql。
要声明表变量,请查看DECLARE @local_variable (Transact-SQL)
要创建表/临时表,请查看CREATE TABLE (Transact-SQL)
除此之外,还可以查看The Curse and Blessings of Dynamic SQL
答案 2 :(得分:0)
也许尝试CREATE TABLE [dbo].[' + @table_name + ']
。
答案 3 :(得分:0)
您应该使用sp_executesql,请参阅http://technet.microsoft.com/en-us/library/ms188001.aspx
答案 4 :(得分:0)
You need to write here a dynamic query like this..
declare @table_name nvarchar(200)='table1'
declare @query nvarchar(4000)='
create table '+ @table_name +'(
[table_name] VARCHAR (128) NOT NULL,
[column_name] VARCHAR (128) NULL,
[what_changed] VARCHAR (128) NULL,
[sr_data_type] VARCHAR (128) NOT NULL,
[tr_data_type] VARCHAR (128) NOT NULL,
[sr_max_length] SMALLINT NOT NULL,
[tr_max_length] SMALLINT NOT NULL,
[sr_is_nullable] CHAR NULL,
[tr_is_nullable] CHAR NULL,
[sr_precision] SMALLINT NULL,
[tr_precision] SMALLINT NULL,
[sr_scale] SMALLINT NULL,
[tr_scale] SMALLINT NULL )'
EXEC sp_executesql @query
exec('select * from '+@table_name)
答案 5 :(得分:0)
您可以随时使用重命名存储过程,例如
CREATE PROCEDURE sp_createAColDiffTable
(
@table_name nvarchar(128)
)
AS
CREATE TABLE TempTable
(
[table_name] VARCHAR (128) NOT NULL,
[column_name] VARCHAR (128) NULL,
[what_changed] VARCHAR (128) NULL,
[sr_data_type] VARCHAR (128) NOT NULL,
[tr_data_type] VARCHAR (128) NOT NULL,
[sr_max_length] SMALLINT NOT NULL,
[tr_max_length] SMALLINT NOT NULL,
[sr_is_nullable] CHAR NULL,
[tr_is_nullable] CHAR NULL,
[sr_precision] SMALLINT NULL,
[tr_precision] SMALLINT NULL,
[sr_scale] SMALLINT NULL,
[tr_scale] SMALLINT NULL
)
EXEC sp_rename 'TempTable', @table_name