Dotnet
Dotnet

Reputation: 109

To call a stored procedure automatically

I have SQL Server 2012 installed in godaddy by default which is shared hosting. And I want to execute a stored procedure at 12.00 PM. Is it is possible to schedule a task? Please help me.

Upvotes: 2

Views: 261

Answers (2)

Brian Clifton
Brian Clifton

Reputation: 701

Since you're using shared hosting, I don't think SQL Jobs will be available (I checked my GoDaddy account and it doesn't show). Are you using Plesk? If so, you can try a work-around. Launch your panel and click Scheduled Tasks in the right side-menu.

From here, you can create a new scheduled task. It'll ask for a path to an executable, which is where you can specify an ASPX page (or a PHP page) that you'd like to run on a schedule.

Your ASPX page would then just simply open a connection and execute the stored procedure. It would look something like this:

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="c#" %>
<%
   using (SqlConnection cn = new SqlConnection("connection string here")) {
       SqlCommand cmd = new SqlCommand {
           Connection = cn,
           CommandText = "StoredProcedureNameHere",
           CommandType = CommandType.StoredProcedure
       };

       cmd.Parameters.Add("exampleParam1", SqlDbType.NVarChar, 50).Value = "test";

       cn.Open();
       int rowsAffected = cmd.ExecuteNonQuery();
       cn.Close();
   }
%>

At this point, you should be good to go!

Upvotes: 0

Brian Stork
Brian Stork

Reputation: 980

Sql agent jobs would be best, if they're available. A worse way would be to make a stored procedure that executes the one you want after A WAITFOR delay. It would work, though I can't recommend it.

Upvotes: 2

Related Questions