DO NOT DO IT IN THIS WAY.
Try creating a stored proc that can do this. Create the below stored procedure in your sql and do all the validations from your end on the same in the proc.
create proc createTable
(
@tableName varchar(30),
@column1 varchar(30),
@column1DataType varchar(30),
@column2 varchar(30),
@column2DataType varchar(30)
)
AS
DECLARE @CreateTableString NVARCHAR(MAX)
BEGIN
IF EXISTS (SELECT NAME FROM sys.tables WHERE name=@tableName)
BEGIN
PRINT ('TABLE ALREADY EXISTS');
RETURN -1;
END
ELSE
BEGIN
SET @CreateTableString='CREATE TABLE '+@tableName +'('+@column1+' ' +@column1DataType+','+@column2+' ' +@column2DataType+')'
EXEC (@CreateTableString);
PRINT ('TABLE CREATED SUCCESSFULLY');
END
END
Now from your front end, you have to create sql parameters with all values as strings and then execute it. They way it should be passed is like :-
EXEC createTable 'test1','test1c1','varchar(30)','test1c2','varchar(30)'
I guess this solves the issue well. I have tried running it in my system and it is fine.
Vote it and mark it as solution if it solves your problem..
Regards
Jashobanta