Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exec SP Based on Inserted Value

Posted on 2010-11-30
4
Medium Priority
?
258 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

719 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