Should be OK now:
CREATE PROCEDURE CreateTable (
@tablename VARCHAR(50)
,@column VARCHAR(50)
,@type VARCHAR(50)
,@column2 VARCHAR(20)
,@type2 VARCHAR(50)
,@extra VARCHAR(20) = NULL
)
AS
BEGIN
DECLARE @sqlQuery AS VARCHAR(MAX)
SET @sqlQuery = 'create table ' + @tablename + '( id int ,' + @column + ' ' + @type + ' , ' + @column2 + ' ' + @type2 + ' PRIMARY KEY(id))'
PRINT (@sqlQuery)
EXEC (@sqlQuery)
END
GO
EXEC CreateTable 'Customers'
,'Name'
,'varchar(50)'
,'Age'
,'int'
If you compare the parameters you will find the type of @type2
was specified as int
while it should be varchar
to be part of the executed statement.
You were also missing comma(,)
in your executable string inside the stored procedure.
CLICK HERE to find out more related problems solutions.