I need to execute one procedure from another in background\asynchronously.
procedure1:
CREATE PROCEDURE procedure1
WITH EXECUTE AS OWNER
AS
BEGIN
//something happens here
EXEC procedure2
//something happens here
END
procedure2:
CREATE PROCEDURE procedure2
WITH EXECUTE AS OWNER
AS
BEGIN
WAITFOR DELAY '00:02'
END
I need procedure1 to execute without waiting for procedure2 to end. I've read this answer in other post:
There was once I tried to achieve this by wrapping the stored
procedure into Job, and then Calling the job in the procedure through
sp_start_job system sp. EXEC dbo.sp_start_job N'Job name' ;
but I don't understand it. Can anyone explain it to me, please? Cause it doesn't look like something advanced but I cannot achieve what I want.
They are referring to creating a SQL Agent Job. This is in its simplest a way to schedule scripts to be run on the server. Typically you would have scripts that need to be run every hour, day, week, etc and rather than run them manually you set up an Agent Job to run them automatically for you.
The Jobs all live in the SQL Server Agent section of your Object Explorer:
Once you have created the job, you can call it manually using sp_start_job. If you only want the job to run when you call it, simply create the job and don't assign a schedule.
Related
My company has several clients/scenario combos worked on by different instances of the same java program. Once the java programs posts data to the database, each calls process_data(client, scenario) that further processes the data.
Occasionally, we have to reprocess the posted data for all the programs. I have created a redo procedure that figures out what arguments were used by all the programs of a given target_date and runs process_data(client, scenario).
It works fine. But, the redo procedure calls process_data one-by-one and I'd like to run each call in parallel. There is no risk of locking, as process_data normally runs in parallel when the java programs kick them off and there hasn't been an issue.
Here's what I mean in pseudo plpgsql code:
CREATE OR REPLACE PROCEDURE redo_data(target_date date)
LANGUAGE plpgsql
AS $procedure$
DECLARE
-- cursor that determines what needs re-doing.
list_what_to_redo cursor(carg_target_date);
begin
for redo in list_what_to_redo(target_date)
loop
-- I'd like this call to not block.
call process_data(redo.client, redo.scenario);
end loop;
-- Maybe wait here for all the non-blocking calls to finish.
END;
$procedure$
;
Is something like this possible? I know I can create something elaborate, like another java program that reads a job table with arguments to call and then creates a thread for each job and runs proces_data(), etc... But I was hoping to avoid doing something like that.
Several articles suggest dblink might help, but I don't know what to call. The exec calls look like blocking calls(?). There is an asynchronous dblink_send_query, but that is for queries. (Perhaps I can wrap process_data inside a table function and use dblink_send_query(myconn, "select wrapped_process_data(client, scenario)")
Thanks.
I have two scripts that do the same thing but for different companies, and during the process they both use the same tables.
It's imperative that only one script runs at once, as sometimes the timings vary greatly, and they are scheduled rather close together purposely. My question is, what is the best method to ensure these scripts do not run together? I tried to have a global field, set to 1 at the beginning of the script, and 0 at the end, so when the 2nd script runs, if global field = 1 - exit script -
This did not work, as both these scripts are scheduled server side, and I have read that the GLOBAL variable is local in this instance.
I assume, we are talking about FileMaker Server schedules.
Global variable will be reset every time you run a scheduled script. Every script will run on it's own session. You can not use them to ensure the scripts do not clash.
As far as I know, FileMaker Server does not run two schedules at the same time. The second script will be delayed until the first one finishes.
FileMaker Server can run simultaneous schedules if they are script schedules, thus an overlap can occur.
What you need to do is set a field that is not a global, so that the schedules can check against the value of that field.
A single record table would be ideal for this.
Make sure that you commit after setting the field, or you may get record locking issues.
Create an OS-level script that uses the fmsadmin command line to run one script, then run the second.
Set the FM Server schedule to run the OS script (which then runs the PSoS scripts).
I'm working on a PostgreSQL 9.3-database on an Ubuntu 14 server.
I try to write a trigger-function (AFTER EACH ROW) that launches an external process that needs to access the row that fired that trigger.
My problem:
Even tough I can run queries on the table including the new row inside the trigger, the external process does not see it (while the trigger function is still running).
Is there a way to manage that?
I thought about starting some kind of asynchronous function call to give the trigger some time to terminate first, but that's of course really ugly.
Also I read about notifiers and listeners, but that would require some refactoring of my existing code and also some additional listener, which I tried to prevent with my trigger. (I'm also afraid of new problems which may occur on this road.)
Any more thoughts?
Robin
In my application I have SQL Server 2008 in the backend and MS Access 2010 in the front end. I have long running procedures that are executed through MS Access. For example now I need to show a messege box in the MS ACCESS front end to the user when the SQL procedure is currently running. Please provide me an idea how can I accomplish that... Thanks in advance!
You can't use MS-Access in asynchronous calls - even if you had a way to set a file-based counter from your stored procedure - you wouldn't be able to track it from inside Access while the stored procedure is running.
The only two ways I see that could do something like this is to
change your stored procedure to handle some kind of paging counter -
using a loop in VBA, and passing, tracking and incrementing a first
pointer and last pointer to your stored procedure - then reporting on
progress, or,
find a way to shell-nowait out to call the stored procedure and then
monitor and report on the progress in VBA
Is it possible to set a database role before running a report? I have a number of databases each containing a number of schemas with the same set of tables, where each schema has a number of roles to control read, write, data management and so on. None of these are default roles.
In sqlplus or TOAD I can do SET ROLE , before running a select statement. I would like to do the same in BIRT.
It may be possible to do this using the afterOpen event for the ODA Data Source, but I have not found any examples on how to get and use the native connection in JavaScript.
I am not allowed to add or change anything on the server end.
You can make an additional call to the database in the afterOpen method of the Data Source using Java. You can use JavaScript or a Java Event Handler to execute the SET ROLE statement, or to call a stored procedure that will execute it for you. This happens after the initial db connection is made, but before the Data Set query runs. It will be a little tricky to use the data source connection to make that call however, and I don't have the code right now to provide as an example.
Another way is to create a stored proc Data Set that will execute the desired command, and have that execute first. Drag and drop the Data Set into the report design, and make it invisible. It will run first before any other queries. Not the cleanest solution, but easy to do
Hope that helps
Le Birt Expert
You can write a login trigger and do a set role in this trigger ( PL/SQL: DBMS_SESSION.SET_ROLE). You can determine the username, osuser, program and machine of the user who want to log in.
The approach to use a stored procedure for setting the role won't work - at least not on Apache Derby. Reason: lifetime of the set role is limited to the execution of the procedure itself - after returning from the procedure the role will be the same as before the procedure has been called, i.e. for executing the report the same as no role would have ever been set.