sql server - Creating a table using dynamic sql -
i want use dynamic sql within stored procedure create table.
here call stored procedure:
exec [spcreateacoldifftable] 'hq193.dbo.arch_con_col_s193_s202' here relevant parts of stored procedure:
create procedure sp_createacoldifftable (@table_name nvarchar(128)) ...
set @ssql = '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)' print @ssql exec @ssql go
when run stored procedure receive error:
sql server database error: name 'create table hq193.dbo.arch_con_col_s193_s202 ( [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)'
is not valid identifier.
notice in stored procedure printed out sql before executed it. if cut , paste sql gets printed query editor window works fine.
what missing?
try this:
exec(@ssql) this common error. without parenthesis, exec @ssql means "execute stored procedure name in @ssql variable", rather want "execute command string in variable @ssql."
Comments
Post a Comment