Description:I have to update a store procedure on live site and we don't have the access of the database at our development environment so we need to do this through code in C#. I have search a lot but unable to find a proper solution For example ,
I have a following stored procedure,
ALTER PROCEDURE [dbo].[SelectCustomers]
AS
BEGIN
SELECT * from Customer order by Customer.DateCreated DESC
END
ALTER PROCEDURE [dbo].[SelectCustomers]
AS
BEGIN
SELECT * from Customer where Customer.Status=1 order by Customer.DateCreated DESC
END
Posted by: Junaid Aziz | Posted on: May 09, 2019
5
2
using (SqlConnection connection = new SqlConnection("YourConnectionString"))
{
connection.Open();
ServerConnection srvCon = new ServerConnection(connection);
Server srv = new Server(srvCon);
Database db = srv.Databases[connection.Database];
StoredProcedure sp = new StoredProcedure(db, "SelectCustomers");
sp.TextMode = false;
sp.AnsiNullsStatus = false;
sp.QuotedIdentifierStatus = true;
sp.ImplementationType = ImplementationType.TransactSql;
sp.Schema = "dbo";
sp.Refresh();
sp.TextBody = "SELECT * from Customer where Customer.Status=1 order by Customer.DateCreated DESC";
sp.Alter();
}
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
Replied by: Peter Andre | Replied on: May 10, 2019
The code provided by Peter will update the stored procedure but it also adds WITH RECOMPILE to the stored procedure as well which is obviously unwanted addition, so I recommend to do it by sending the complete query batch using SqlClient. For the purpose you can use the following method.
To make the above updates to the stored procedure you can call the method as shown below.
Replied by: Tabish Usman | Replied on: May 13, 2019