0

How do I call the Stored Procedure sp_addserver from C# with the local switch?

In SSMS I can call add server like this:

    EXEC sp_addserver 'MYNEWSERVERNAME',local

My relevant code as of now:

cmd.CommandText = "sp_addserver";
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@server", "MYNEWSERVER"));
cmd.ExecuteNonQuery();

This errors out as sp_addserver is not supported without the local switch.

1
  • Pass the other parameter too? Commented Feb 1, 2018 at 21:59

2 Answers 2

2

sp_addserver supports the formal parameter called "@local".

sp_addserver [ @server = ] 'server' ,
[ @local = ] 'local'
[ , [ @duplicate_ok = ] 'duplicate_OK' ]

So just pass a parameter called 'local' with the value 'local'

cmd.Parameters.Add(new SqlParameter("@local", "local"));

Sign up to request clarification or add additional context in comments.

Comments

1

Create your own SP that adds the extra parameters you need and then call that SP from your code.

1 Comment

I thought about doing that but I don't know quite how at this point. Something I'll have to learn how to do. Thanks @Neil

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.