Solved

Translate Following PL-SQL trigger to T-SQL trigger

Posted on 2004-09-01
8
585 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
[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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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
 
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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.

635 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