Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i want to add column in a table using Stored procedure but when i am trying getting error

Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1
Procedure expects parameter @statement; of type ;ntext/nchar/nvarchar;

What I have tried:

SQL
alter proc SpgetEMP
as
begin
exec sp_executeSQL  'alter table emp add adrs varchar(20) null'
end
Posted
Updated 25-Nov-16 7:20am
Comments
Richard Deeming 25-Nov-16 14:10pm    
That procedure will only work once. As soon as it works, the next time you call it you'll get an error telling you that the column already exists.

Modifying the database structure from a stored procedure is almost always a sign of a bad design.

And you application should not be connecting as a user which has permissions to modify the structure of the database. It should be connecting as a user which has the minimum permissions necessary for your application to run.

1 solution

Modify Proc as below
SQL
declare @statement as nvarchar(100);
set @statement='alter table emp add adrs varchar(20) null'
exec sp_executeSQL @statement
 
Share this answer
 
Comments
Sql_Lover_rahul_Singh 26-Nov-16 12:22pm    
Thanks its is work :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900