create procedure createTable(@tableName varchar(25), @columnName varchar(25), @columnType varchar(25)) as begin declare @sqlQuery as varchar(MAX) set @sqlQuery = 'create table ' + @tableName + '(' + @columnName + ' ' + @columnType + ' primary key)' print (@sqlQuery) exec @sqlQuery end go
Above code produces below output and error:
create table proba2(ceva int primary key) Msg 2812, Level 16, State 62, Procedure createTable, Line 9 [Batch Start Line 13] Could not find stored procedure 'create table proba2(ceva int primary key)'.
Strangely, if I copy the first line in the output(which is the printed query itself) and execute it, it works; the table will be created. Nonetheless, the exec in the stored procedure does not. As far as I know, the exec call of the procedure should work. It should even work without specifying 'primary key' in the end of the string.