Solved

Triggers

Posted on 1998-08-04
2
338 Views
Last Modified: 2010-03-19
Hi, I am trying to create an update trigger and everything is correct except how do I pass variables into a stored procedure that I am calling within a trigger. My example is below:

CREATE TRIGGER master_update_trigger
ON dbo.ksu_master_term_table
FOR UPDATE

AS

if update(hall)
begin
      if exists (select * from restv.dbo.restv_table, inserted where restv.dbo.restv_table.
      client_id=inserted.stud_id and restv.dbo.restv_table.term_code=inserted.term_code
      and  restv.dbo.restv_table.active ="Y")
      begin
      
            update restv.dbo.restv_table set restv.dbo.restv_table.active = "N",
            restv.dbo.restv_table.done = "N",restv.dbo.restv_table.hall=inserted.hall,
            restv.dbo.restv_table.room = inserted.room, restv.dbo.restv_table.local_phone=inserted.
            local_phone From inserted where restv.dbo.restv_table.client_id=inserted.stud_id and
            restv.dbo.restv_table.term_code=inserted.term_code

--------------------------->  exec restv_insert_sp

--------------------------->insert into restv.dbo.restv_table (client_id,hall,room,first_name,last_name,term_code,
            converter_cost,converter_qty,coax_cable_qty,coax_cable_cost,hbo_cost,showtime_cost,
            total_cost,previous_term_code,done,active,date_time_stamp,local_phone,billed,order_num)
            values ("deleted.client_id","deleted.hall","deleted.room","deleted.first_name","deleted.
                  last_name","deleted.term_code","deleted.converter_cost",CONVERT(tinyint,"deleted.converter_qty"),
            CONVERT(tinyint,"deleted.coax_cable_qty"),CONVERT(smallmoney,"deleted.coax_cable_cost"),
            CONVERT(smallmoney,"deleted.hbo_cost"),CONVERT(smallmoney,"deleted.showtime_cost"),
            CONVERT(money,"deleted.total_cost"),"deleted.previous_term_code","deleted.done","deleted.active",
            "getdate()","deleted.local_phone","deleted.billed",CONVERT(int,"deleted.order_num"))
            From deleted where restv.dbo.restv_table.client_id=inserted.stud_id and
            restv.dbo.restv_table.term_code=inserted.term_code
      end      
end


All I am trying to do is to create an update trigger that will update a record in another datebase and will
insert the old record before the update also into another datebase. Can you tell mw what is wrong with my syntax where I am trying to insert the old record after the stored procedure
0
Comment
Question by:kevykev
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
jbiswas earned 100 total points
ID: 1089403
To call a stored proc from inside a trigger all you have to do is pass the right parameters to the stored proc. So if the stored proc is expecting two parameters, just call the stored proc with the right parameters inside paranthesis
exec stored_proc(parameterA, parameterB)

That should work.
0
 
LVL 2

Expert Comment

by:jbiswas
ID: 1089404
By the way I do not know of any before update triggers in MS SQL Server..maybe you wanna check whether that is allowed. However I do not see any before or update clause in your code. maybe you're doing so by the stored proc?
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

828 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