Solved

Exec SP Based on Inserted Value

Posted on 2010-11-30
4
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

739 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