Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exec SP Based on Inserted Value

Posted on 2010-11-30
4
Medium Priority
?
259 Views
Last Modified: 2012-08-13
I have a table that gets written to from a vb.net win app
I would like to have a trigger on my table on insert to select the correct procedure to run

for example:
a row gets inserted into my table, in a particular field called stored_proc "sendemail1" gets inserted
then exec procedure where procedure = stored_proc field contents on inserted

how can this be done?
0
Comment
Question by:H-SC
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 34239857
I guess you could use a trigger like below however you may need to use sp_executesql instead of the EXEC if you want to run SQL code instead of an SP and change the variable length accordingly

CREATE TRIGGER [dbo].[clientsinsert]
   ON  [dbo].[Clients]
   AFTER INSERT
AS
BEGIN
DECLARE @sqlstr varchar(1000)

      if exists (select stored_proc from inserted where stored_proc = 'sendemail1')
      begin
            SET @sqlstr = 'EXEC ' + cast((select proc_name from inserted where proc_name = 'sendemail1') as varchar(512)) + ';';
            exec @sqlstr;
      end

END
GO
0
 
LVL 1

Author Comment

by:H-SC
ID: 34240007
lcohan,

Thanks for the response..
will this select dynamiclly?
In other words it may not always be sendmail1..could be sendmail2 or sendmail3 and so on.
I need it to select the procedure that matches that field value

so if someone were to insert new row with test123 in the column then it would look for a stored procedure called test123

any ideas?
0
 
LVL 40

Accepted Solution

by:
lcohan earned 2000 total points
ID: 34240162
Not realy dynamic...for that we need to know if you allow batch insert or not because if you do then we would have to create the trigger with a cursor and that may be a performance issue even if it's an "AFTER" trigger. Let me say way - let's say you insert 1000 rows in a single statement(batch) so your trigger could but would have to get all these SP names from INSERTED and loop through a cursor or pseudo cursor to execute them one by one and that may be a big hit or....you could build a string with all these 1000 spnames and call a SP with this as a parameter OR...which I did and recommend if you know sql coding - create a sql job on the fly from the trigger and execute it then at the end of it you could have a step to check if the job itself was executed properly then delete itself.
The last option with the job will not hold the table row(s) lock(s) until the trigger action is completed for all 1000 or so rows from the batch but adds complexity and you may/will have failures thet need to deal with.

Lets take the simple aproach and consider that your INSERT can be ONLY 1 ROW at a time no matter what. In that case the triger to do it dynamicaly based on your single value inserted into stored_proc column will look like:

CREATE TRIGGER [dbo].[clientsinsert]
   ON  [dbo].[Clients]
   AFTER INSERT
AS
BEGIN
DECLARE @sqlstr varchar(1000)
      if  (select count(stored_proc) from inserted) = 1 -- test to make sure no batch insert happens
      begin
            SET @sqlstr = 'EXEC ' + cast((select proc_name from inserted where proc_name = stored_proc) as varchar(512)) + ';';
            EXEC @sqlstr;
      end
END
GO



0
 
LVL 1

Author Comment

by:H-SC
ID: 34241818
lcohan,

Many thanks for this!  I just tried and I think that it will indeed work for what I am trying to accomplish here.

thanks for the explanation and much needed help on this one.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question