Solved

Translate Following PL-SQL trigger to T-SQL trigger

Posted on 2004-09-01
8
572 Views
Last Modified: 2007-12-19
Hi everybody. I got a question as stated in title. Is there anybody know how to translate follwing  PL-SQL trigger to T-SQL trigger?

CREATE OR REPLACE TRIGGER trigger1
BEFORE UPDATE ON employee

FOR EACH ROW
WHEN (NEW.sal = 100)
BEGIN
  -- some actions here --
END;

Whenever any record in employee table is going to be updated to 100, some actions will be taken before that update (eg. change the new value from 100 to 120, then update the relevant records with 120 rather 100).

Thanks a lot.
?@_@?
0
Comment
Question by:lenzge
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 4

Assisted Solution

by:chipple
chipple earned 40 total points
ID: 11960359
This should work:

CREATE TRIGGER trigger1 ON employee
FOR UPDATE
AS
  DECLARE @sal int
  SELECT @sal=sal FROM inserted

  IF @sal = 100
  BEGIN
    -- some actions here --
  END

Good luck!
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11960392


CREATE TRIGGER trigger1 ON employee
FOR UPDATE
AS
  DECLARE @sal int

  SET ROWCOUNT 1
  WHILE EXISTS (SELECT sal FROM inserted WHERE SAL = 100)
      BEGIN
          --YOUR STATEMENTS HERE
          DELETE FROM INSERTED WHERE SAL = 100
      END
  SET ROWCOUNT 0
0
 
LVL 4

Expert Comment

by:chipple
ID: 11960410
ram2098 is on the money.
I guess I'm a bit rusty. :)
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11960931
inserted and deleted are read-only
you can't delete from "deleted"

you'll have to use a cursor

CREATE TRIGGER trigger1
FOR UPDATE ON employee
as
declare c1 cursor for select empid from employee where sal=100
declare @empid int
open c1
fetch c1 into @empid
while @@fetch_status = 0
begin
 -- do whatever you want here for this @empid
 fetch c1 into @empid
end
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 26

Expert Comment

by:Hilaire
ID: 11960934
Oops Cursr should be on inserted virtuak table, not employee

ALTER TRIGGER trigger1
FOR UPDATE ON employee
as
declare c1 cursor for select empid from inserted where sal=100
declare @empid int
open c1
fetch c1 into @empid
while @@fetch_status = 0
begin
 -- do whatever you want here for this @empid
 fetch c1 into @empid
end
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 11961846
IMHO,   I think you should look at the  -- some actions here --
and ask yourself "Does this really need to be in a cursor?"

cursors are best avoided unless you need them, expecially in triggers.
0
 

Author Comment

by:lenzge
ID: 11983388
What I'd done is as follow:

CREATE TRIGGER Trigger1
FOR UPDATE ON employee
AS

DECLARE @emp_id CHAR(4)
DECLARE @sal INT
SELECT @emp_id = emp_id FROM INSERTED
SELECT @sal = sal FROM INSERTED

IF @sal = 100
  UPDATE employee (sal) VALUES (120)

Basically, what I need to do is change a particular field value of newly inserted record if a criteria is fulfilled. Any comment to improve it?

? @_@ ?
0
 
LVL 18

Accepted Solution

by:
ShogunWade earned 30 total points
ID: 11988478
Basicallly this trigger will fail and /or cause big problems when someone performs an update on more than one record.

This is better:

CREATE TRIGGER Trigger1
FOR UPDATE ON employee
AS
UPDATE e
 SET sal=120
 FROM employee e
  INNER JOIN INSERTED i ON e.emp_id=i.emp_id
 WHERE i.sal=100
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 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

20 Experts available now in Live!

Get 1:1 Help Now