Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Execute a Stored Proc as a background process Expand / Collapse
Author
Message
Posted Monday, May 12, 2008 4:04 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, July 16, 2010 8:44 AM
Points: 13, Visits: 50
I want to initiate a Procedure and then return-continue execution. The reasoning behind this is to use SQL Express but because Express does not have an Agent I need to initiate a recurring task somehow.
I have an external app feeding data in SQL so I am hoping there is a way, as part of the call by that external app, that I can initiate a 'task'.

I am sure this is a fairly common need with SQL Express - ideas will be great.
Post #498630
Posted Monday, May 12, 2008 6:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:30 PM
Points: 14,802, Visits: 27,275
Can't you simply open another connection to the database and fire the procedure asynchronously? That way it won't be part of the main application's execution path.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #498697
Posted Monday, May 12, 2008 11:08 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 07, 2010 2:19 PM
Points: 27, Visits: 34
If you are programming using a multithreaded environment like java or .net you can just run the long running query from a new thread. If you're in a single thread environment you have a lot less options.
Post #499307
Posted Monday, May 12, 2008 11:19 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
robertcaustin (5/12/2008)
I want to initiate a Procedure and then return-continue execution. The reasoning behind this is to use SQL Express but because Express does not have an Agent I need to initiate a recurring task somehow.
I have an external app feeding data in SQL so I am hoping there is a way, as part of the call by that external app, that I can initiate a 'task'.

I am sure this is a fairly common need with SQL Express - ideas will be great.


Hey folks... does SQL Express have Osql.exe? If so, couldn't Robert create a batch file and use Windows Task Scheduler to execute it on a regularly scheduled basis?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #499312
Posted Tuesday, May 13, 2008 5:40 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:30 PM
Points: 14,802, Visits: 27,275
Yeah, OSQL will work fine. It's still back to what I said. Open a new connection, either by spawning a thread or using another executable, one you built or one you bought, and run the query seperate from the other processes. It shouldn't be a big deal.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #499473
Posted Tuesday, May 13, 2008 2:31 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 07, 2010 2:19 PM
Points: 27, Visits: 34
If its just a matter of running a batch process regularly an executable or batch file started from scheduled tasks would work just fine.
Post #499975
Posted Tuesday, May 13, 2008 2:45 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
Another thing you can do is create a startup proc, and use the WaitFor command and a While loop to make it execute something over and over again.

While 1 = 1
begin
exec dbo.RecurringCommandProc
waitfor delay '00:05'
end

That will make it run every 5 minutes. (Well, after it finishes, there will be a 5 minute delay till it runs again. Not the same as starting it every 5 minutes.)

sp_procoption will set it to run automatically at startup. Has to be created in master database and be owned by sa account.

I just tested this in an instance of Express and it seems to have worked.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #499985
Posted Thursday, May 15, 2008 2:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, July 16, 2010 8:44 AM
Points: 13, Visits: 50
Thanks to all the replies but I think my logic and reasoning is the opposite to normal programming. I live inside SQL and only write or use outside programs when I can't do it in SQL. My logic is that locks and DB activity is easiest managed from within the SQL structures. Having an outside program pushing instructions in means a lot of overhead needed to make sure that external process does not mess with the core activity.

So the idea is to run the normal insert process (from an external app) and to return to the external app. But if the insert required some further processing then I want to be able to leave this running as an internally-originated thread/process without holding up the external app.

Hence the thought that it may be possible to call a Stored Procedure from another proc with a flag to run in the background.

I have an existing C Service running on the server that calls for any data that needs to be pushed out to the client. I wanted to use this as a 'timer' to initiate any pending processing BUT I don't want to stall this service while it waits for an unrelated process to complete.

On my Normal (as opposed to SQL Express) servers I have a SQL Agent Task that starts when SQL Starts and is set to restart every minute but because it never ends, it only restarts if the {code} fails
While 1=1
begin
{code}
WAITFOR()
end

Express does not have Agent so there is nothing that can initiate this process that does not 'hang' whatever was used to initiate it.
Post #501106
Posted Friday, May 16, 2008 7:17 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
Did you look at the startup proc idea I wrote up? That doesn't require any outside process, nor SQL Server Agent.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #502023
Posted Friday, May 16, 2008 7:31 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
robertcaustin (5/15/2008)
Thanks to all the replies but I think my logic and reasoning is the opposite to normal programming. I live inside SQL and only write or use outside programs when I can't do it in SQL. My logic is that locks and DB activity is easiest managed from within the SQL structures. Having an outside program pushing instructions in means a lot of overhead needed to make sure that external process does not mess with the core activity.


You're a man after my own heart, Robert. Sorry I can't help on the express side of the house.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #502037
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse