Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if we want to rename a column in sql we use the statement
Sp_rename 'TableName.OldColumnName' , 'NewColumnName','Column'
in my stored procedure i want to pass tableName,OldColumnName and NewColumnName
as parameters i mean dynamic statement
the code is:
create Procedure A
@TableName nvarchar(255),
@OldColumnName nvarchar(255),
@NewColumnName nvarchar(255)

can any one help me to write the statement with the parameters
i dont know ho to put this special character ' in my statment
thanks
Posted

1 solution

check this


SQL
Create Procedure RenameColumnName
( 
@TableName nvarchar(255),
@OldColumnName nvarchar(255),
@NewColumnName nvarchar(255)
)
As
BEGIN
--declare @TableName nvarchar(255)='History',
--@OldColumnName nvarchar(255)='Column3',
--@NewColumnName nvarchar(255)='Column4'
declare @strsql varchar(max)
set @strsql = 'Sp_rename '''+@TableName+'.'+@OldColumnName+''' , '''+@NewColumnName+''',''Column''' 
exec (@strsql)

END
 
Share this answer
 
Comments
oula alsheikh 27-Nov-14 3:53am    
thanks very much it works well
putting ' as ''' in @strsql was what i didn't
know how to make
Shweta N Mishra 27-Nov-14 3:55am    
np:)

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