Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 More then one record returning...

Author  Topic 

dyckwal
Yak Posting Veteran

58 Posts

Posted - 2005-11-15 : 09:56:59
Hello,

I have a trigger which does an update of a table and it seems
not able to handle more then one record...

What I wanted to do is whenever there is an update on the table INFO_SSCC, then take a look at the value of LOCKED_NR and SSCC_STATUS.
Depending on the value, do something.
If the Status is > 0 insert some values into the table INFO_LOCKED_OUT.
The two other cases is to remove the record data from the table INFO_LOCKED_OUT.

Probebly should use some joins, but I don't know where and how...

So can someone give me a tip...

Here the code :

IF UPDATE(LOCKED_NR) OR UPDATE (SSCC_STATUS)
BEGIN
IF (SELECT LOCKED_NR FROM INSERTED) > 0
BEGIN
INSERT INTO INFO_LOCKED_OUT(SSCC, LOCKED_NR)
SELECT INS.SSCC, INS.LOCKED_NR
FROM INSERTED INS
END

IF (SELECT LOCKED_NR FROM INSERTED) = 0
BEGIN
DELETE FROM INFO_LOCKED_OUT
WHERE SSCC = (SELECT SSCC FROM INSERTED)
AND LOCKED_NR = (SELECT LOCKED_NR FROM DELETED)
END

IF (SELECT SSCC_STATUS FROM INSERTED) = 9
BEGIN
DELETE FROM INFO_LOCKED_OUT
WHERE SSCC = (SELECT SSCC FROM INSERTED)
END
END

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-11-15 : 10:22:14
quote:
Originally posted by dyckwal

Hello,

I have a trigger which does an update of a table and it seems
not able to handle more then one record...

What I wanted to do is whenever there is an update on the table INFO_SSCC, then take a look at the value of LOCKED_NR and SSCC_STATUS.
Depending on the value, do something.
If the Status is > 0 insert some values into the table INFO_LOCKED_OUT.
The two other cases is to remove the record data from the table INFO_LOCKED_OUT.

Probebly should use some joins, but I don't know where and how...

So can someone give me a tip...

Here the code :

IF UPDATE(LOCKED_NR) OR UPDATE (SSCC_STATUS)
BEGIN
IF (SELECT LOCKED_NR FROM INSERTED) > 0
BEGIN
INSERT INTO INFO_LOCKED_OUT(SSCC, LOCKED_NR)
SELECT INS.SSCC, INS.LOCKED_NR
FROM INSERTED INS
END

IF (SELECT LOCKED_NR FROM INSERTED) = 0
BEGIN
DELETE FROM INFO_LOCKED_OUT
WHERE SSCC = (SELECT SSCC FROM INSERTED)
AND LOCKED_NR = (SELECT LOCKED_NR FROM DELETED)
END
Are you sure of that your select from inserted will get only value if its getting more then one value then i guess u should use in clause.

IF (SELECT SSCC_STATUS FROM INSERTED) = 9-- Are you sure this 9 or 0 Check out this.
BEGIN
DELETE FROM INFO_LOCKED_OUT
WHERE SSCC = (SELECT SSCC FROM INSERTED)
END
END



Can you also post the error.. what you are getting ????

Complicated things can be done by simple thinking
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-11-15 : 10:25:08
IF (SELECT LOCKED_NR FROM INSERTED) > 0
probably should be
if exists (select * from inserted)
or maybe
if exists (select * from inserted where LOCKED_NR > 0)

Your query would only work if there was a single row.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dyckwal
Yak Posting Veteran

58 Posts

Posted - 2005-11-15 : 10:38:26
yes I know that it only works with a single row. That's the problem. It should also work on multiple rows..

Could you help or give me an example or something..

Thanks
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-11-15 : 10:50:36
I've given two examples for the statement above. Depends what it is meant to do.

You will also need to change
DELETE FROM INFO_LOCKED_OUT
WHERE SSCC = (SELECT SSCC FROM INSERTED)
AND LOCKED_NR = (SELECT LOCKED_NR FROM DELETED)
to
DELETE INFO_LOCKED_OUT
fromINFO_LOCKED_OUT t
join insertde i
on t.SSCC = i.SSCC
join deleted d
on t.LOCKED_NR = d.LOCKED_NR

(again not sure what that is meant to do)


DELETE FROM INFO_LOCKED_OUT
WHERE SSCC = (SELECT SSCC FROM INSERTED)
to
DELETE FROM INFO_LOCKED_OUT
WHERE SSCC in (SELECT SSCC FROM INSERTED)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dyckwal
Yak Posting Veteran

58 Posts

Posted - 2005-11-15 : 13:27:50
The first is to delete the sscc from the table INFO_LOCKED_OUT, but since it can occur more then once the second key is the LOCKED_NR. So you're first solution will work on that one.
The second one is the case when all records from that SSCC need to be deleted from the table INFO_LOCKED_OUT.
But how do I solve the

IF (SELECT LOCKED_NR FROM INSERTED) > 0
BEGIN

AND

IF (SELECT LOCKED_NR FROM INSERTED) = 0
BEGIN

AND

IF (SELECT SSCC_STATUS FROM INSERTED) = 9
BEGIN

to work with multiple records??
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-11-15 : 13:45:03
I think you want
if exists (select * from inserted where LOCKED_NR > 0)
if exists (select * from inserted where SSCC_STATUS = 9)

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -