Stored Procedure

cipher7836 86 Reputation points
2021-10-22T19:52:31.913+00:00

I’m a bit clueless writing this stored procedure. I want to be able to execute the stored procedure to change the name of any server in my sql_servers table. So I wrote this:

Create proc chngservername
@keyid int, @srvname nvarchar(120)
As
Begin
Update dbo.sql_servers set name = @srvname
Where keyid = @keyid
End
Go

Exec chngservername ‘blue’, 87119

But that gives me an error that it can’t convert an nvarchar to an integer.

What am I doing wrong?

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,547 questions
{count} votes

Accepted answer
  1. Guoxiong 8,126 Reputation points
    2021-10-22T19:59:57.977+00:00

    You may need to reverse the order of the parameters provided:

    Exec chngservername 87119, ‘blue’

    Or you can provide the parameter names when you call the SP:

    Exec chngservername @srvname = ‘blue’ , @keyid = 87119

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MelissaMa-MSFT 24,176 Reputation points
    2021-10-25T02:06:15.823+00:00

    Hi @cipher7836 ,

    Welcome to Microsoft Q&A!

    You could refer to Guoxiong's answer.

    Create proc chngservername
    @keyid int, @srvname nvarchar(120)

    If you create the procedure with the parameters in above order, you have to provide the value of the parameters in the same order when you execute the procedure which means that provide the 87119 firstly and ‘blue’ secondly.

    Best regards,
    Melissa


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments