1

I have the following SQL code in a stored procedure:

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)

I call the stored procedure like this:

EXEC [sp_createAColDiffTable] 'hq193.dbo.arch_con_col_s193_s202'

When I run the code I get the error:

Incorrect syntax near '@table_name'.

When I substitute the actual table name for @table_name it works fine. Can someone point out the problem with my syntax?

4

7 回答 7

3

您需要为此使用动态 SQL,因为CREATE TABLE它不需要变量名。

例如

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.

于 2013-10-07T12:15:08.877 回答
1

@table_name在声明表变量时使用,而table_nameor#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)

除此之外,还可以看看动态 SQL 的诅咒和祝福

于 2013-10-07T12:15:25.467 回答
0

也许试试CREATE TABLE [dbo].[' + @table_name + ']

于 2013-10-07T12:16:54.337 回答
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)
于 2013-10-07T12:28:45.133 回答
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
于 2013-10-07T12:30:30.250 回答
0

您应该使用 sp_executesql,请参阅http://technet.microsoft.com/en-us/library/ms188001.aspx

于 2013-10-07T12:24:09.117 回答
0

我使用了 CREATE TABLE tblVarName LIKE staticname;。我有静态表定义,并且 CREATE TABLE LIKE 允许传递一个字符串变量,它创建具有相同定义的表,除了外键约束。所以,在创建后我执行 ALTER TABLE ADD CONSTRAINS

于 2019-03-12T22:54:50.253 回答