views:

95

answers:

3

Is my the only option to wrap sp_rename or similar into stored procedure and then

sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
sqlCommand.ExecuteNonQuery();
+1  A: 

When you call it directly (i.e. without wrapping it in another stored procedure), what error are you getting?

Cade Roux
I actually haven't tried yet. So you are suggesting sp_rename is going to be treated as usual sql for selectCmd.CommandType = System.Data.CommandType.Text;
MicMit
Silly of me. Somehow I missed sp_rename is just a stored procedure.
MicMit
@MicMit it's a stored procedure in master, but any type you call a stored procedure prefixed with sp_, master is searched first and then the current database on the connection. master SPs typically act on objects in the current database, which is one of their most useful abilities. Normal SPs within a database act on objects within the database where the SP is located if they are not qualified with a database.
Cade Roux
+1  A: 

sp_rename is a stored procedure, so you should be able to call it like any other...

BlueRaja - Danny Pflughoeft
+1  A: 

As long as the user account with which you are connecting to the database has rights to call sp_rename, there is no reason you cannot call it just like any other stored procedure like so:

var connString = ...
using ( var conn = new SqlConnection( connString ) )
{
    using ( var cmd = new SqlCommand( "exec sp_rename 'Table_1', 'Table_2'", conn ) )
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}
Thomas