sql - using parameter values in stored procedures -
i have following sql code in stored procedure:
create procedure sp_createacoldifftable (@table_name nvarchar(128)) 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] (data_compression = none)
i call stored procedure this:
exec [sp_createacoldifftable] 'hq193.dbo.arch_con_col_s193_s202'
when run code error:
incorrect syntax near '@table_name'.
when substitute actual table name @table_name
works fine. can point out problem syntax?
you need use dynamic sql this, create table
doesn't take variable name.
e.g.
create procedure sp_createacoldifftable (@table_name nvarchar(128)) 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] (data_compression = none)'; exec(@sql);
be aware using dynamic sql can lead sql injection though, make sure never pass user input @table_name
.
Comments
Post a Comment