Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables when calling sp_rename

I try to make a stored proc which will

  1. Drop a primary key
  2. Rename the column name where the primary key was set
  3. Create the new primary key

I'm struggling with the point number 2.

I'm trying to rename the column with sp_rename with the parameters passed to the stored proc like this:

EXEC sp_rename '[' + @SCHEMA + '].[' + @TABLE + '].[ID]' , 'Id', 'COLUMN'

But this way I got this error:

Procedure or function 'sp_RENAME' expects parameter '@newname', which was not supplied.

How can I use sp_rename with parameters ?

like image 373
Dipiks Avatar asked Nov 19 '25 03:11

Dipiks


1 Answers

Try like this

DECLARE @SCHEMA NVARCHAR(30)='your schema name'
DECLARE @TABLE NVARCHAR(30)='table Name'
DECLARE @OLD NVARCHAR(30) = '[' + @SCHEMA + '].[' + @TABLE + '].[ID]'
EXEC sp_rename  @OLD, 'Id'
like image 191
Jaydip Jadhav Avatar answered Nov 20 '25 18:11

Jaydip Jadhav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!