473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Trigger problem no rowlevel support, please help!

Hello everybody.

Unfortunately I am pretty new to sql-server 2000
I need some help with a Trigger I created. I created a trigger witch
takes the id of the affected row and does a update on a other table
with that ID.
The trigger works fine with one affected row. But when there are more
then one rows affected, i get an error.
I found out that SQL-server does not support row-level triggers.
I should probable make my own cursor and itterate through the deleted
table. but i don't know how to do that. since i'm new to sql-server
2000

What I want is to itterate through the deleted table, just like the
ORACLE FORE EACH ROW.
retrieving the ID's and using them to update the CHECKED table.

Is there anybody who has encountered the same problem and has a
workaround for it?

I would really appreciate some help with this.

CREATE TRIGGER TR_Customers_CHECKED_Update ON Customers FOR UPDATE
AS
Begin
DECLARE @CUSTID bigint
SET @CUSTID = (SELECT CustomerID FROM Deleted)
update CHECKED set approved = 'NO' where CHECKED.CustomerID = @CUSTID;
end

Feb 20 '06 #1
4 1924
SUKRU wrote:
Hello everybody.

Unfortunately I am pretty new to sql-server 2000
I need some help with a Trigger I created. I created a trigger witch
takes the id of the affected row and does a update on a other table
with that ID.
The trigger works fine with one affected row. But when there are more
then one rows affected, i get an error.
I found out that SQL-server does not support row-level triggers.
I should probable make my own cursor and itterate through the deleted
table. but i don't know how to do that. since i'm new to sql-server
2000

What I want is to itterate through the deleted table, just like the
ORACLE FORE EACH ROW.
retrieving the ID's and using them to update the CHECKED table.

Is there anybody who has encountered the same problem and has a
workaround for it?

I would really appreciate some help with this.

CREATE TRIGGER TR_Customers_CHECKED_Update ON Customers FOR UPDATE
AS
Begin
DECLARE @CUSTID bigint
SET @CUSTID = (SELECT CustomerID FROM Deleted)
update CHECKED set approved = 'NO' where CHECKED.CustomerID = @CUSTID;
end


Workaround for what? You don't need to do it for each row. Updates are
set based so triggers should be too. Always remember to account for
multiple row updates in troiggers. Do not use cursors in triggers.

BTW are you sure this is intended for an UPDATE trigger? The logic
looks more appropriate for a DELETE to me.

Try:

CREATE TRIGGER tr_customers_checked_update
ON customers FOR UPDATE
AS
BEGIN

UPDATE checked
SET approved = 'NO'
WHERE EXISTS
(SELECT *
FROM deleted
WHERE customerid = checked.customerid);

END

GO

(untested)

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Feb 20 '06 #2
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.

Your design sounds flawed. You have a customer_id which has a huge
size, and no way to validate it (check digit, anything??) Next, we
seldom use binary flags in SQL; instead we have a history with (start,
finish, status) triplets and/or an encodeing scheme for the status.

When I see a table named "CHECKED", I get scared. That is a status,
not an entity or relationship!! It means that you have split an
attribute out of an entity and made it into a separate table. If you
had a "MalePersonnel" and a "FemalePersonnel", you see the flaw of a
split on "sex" immediately.

Knowing that uppercase names are almost 10 times more often mis-read or
mis-typed, why did you use them? We had no choice in the days of
punchcards. In fact, you whoel design seems to be a punch card system
done in SQL.

You need to learn BASIC RDBMS, and how to use VIEWs.

Feb 20 '06 #3
Hello Celko,

Thanks for the reply!
Your right I'm a beginner, but the example above is ficticious!
In fact there is no checked table! I made that up. I choose the checked
name just for practice sake. The original tables are in Dutch, i
wouldn't want to bother you with Dutch ;).

Further more i'm not very familiar with sql-server coding conventions.
I will look it up on the internet.

the main issue is that I couldn't get the original trigger to work
because if my trigger returned more then one row i would get an error.
I've got an example in postgreSQL where the trigger works with the "for
each row function". but SQL-server 2000 doesn't support the "for each
row" statement!

But thank you for your advise, i will apply your advise the next time i
post an message!

--CELKO-- wrote:
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.

Your design sounds flawed. You have a customer_id which has a huge
size, and no way to validate it (check digit, anything??) Next, we
seldom use binary flags in SQL; instead we have a history with (start,
finish, status) triplets and/or an encodeing scheme for the status.

When I see a table named "CHECKED", I get scared. That is a status,
not an entity or relationship!! It means that you have split an
attribute out of an entity and made it into a separate table. If you
had a "MalePersonnel" and a "FemalePersonnel", you see the flaw of a
split on "sex" immediately.

Knowing that uppercase names are almost 10 times more often mis-read or
mis-typed, why did you use them? We had no choice in the days of
punchcards. In fact, you whoel design seems to be a punch card system
done in SQL.

You need to learn BASIC RDBMS, and how to use VIEWs.


Feb 21 '06 #4
SUKRU (as**********@hotmail.com) writes:
Thanks for the reply!
Your right I'm a beginner, but the example above is ficticious!
In fact there is no checked table! I made that up. I choose the checked
name just for practice sake. The original tables are in Dutch, i
wouldn't want to bother you with Dutch ;).


Heck why not? Most of Celko's posts are double-dutch anyway, so what's
the difference. :-)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: robert | last post by:
i've found the solution threads on changing a column on insert. works fine. question: - will one package serve for all such triggers, or does there need to be a package defined to support...
1
by: m3ckon | last post by:
Hi there, I'm a little stuck and would like some help I need to create an update trigger which will run an update query on another table. However, What I need to do is update the other...
8
by: SunshineGirl | last post by:
I'm trying to trigger an app with the following code from a message queue. It works with only the message box voilą! enabled. But it causes the exception below when it is triggered with the rest of...
33
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
7
by: Shane | last post by:
I have been instructed to write a trigger that effectively acts as a foreign key. The point (I think) is to get me used to writing triggers that dont use the primary key(s) I have created the...
5
by: Max2006 | last post by:
Hi, I am using Visual C# 2005 and I like to setup breakpoint that pause execution upon a class variable change. Is that possible?
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
4
by: Peter | last post by:
ASP.NET 3.5 I have a web page and this web page has a DataList, when user hovers the mouse over a picture in the datalist a popup appers, this popup is a User Control and it has a cancel button....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.