Solved

Exec SP Based on Inserted Value

Posted on 2010-11-30
4
252 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 39

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 39

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 to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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 to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now