-
UPDATE or INSERT in a trigger?
In a SQL Server trigger for UPDATE INSERT, how can I determine which of the two is happening? The IF UPDATE() function is run in both cases.
-
That would be based on something external to the trigger.
What do you have that would fire the trigger off?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
you can SELECT and use "CASE" to insert or update ... better if you have support for PL/SQL .. or any high level language for example ..
-
I'm not an expert on triggers by any means, but are you checking the "inserted" and "updated" tables in your code?
It's not what you don't know that gets you in trouble. It's what you know that just ain't so. -Mark Twain
-
UPDATE or INSERT in a trigger?
When inserting records in the table OR updating a column to a certain value (eg. "TRUE") inside a record, I want to automatically insert some records into other tables as well. In any other case, I don't want to do anything.
The inserted table is used both in update and insert.
-
 Originally Posted by Hack
That would be based on something external to the trigger.
What do you have that would fire the trigger off?
What do you mean? A trigger can't read or get passed any variable outside it. (if understand you correctly)
-
You can set up a trigger to fire off on an update, or an insert, or a delete, or any other kind of database change.
http://www.sqlteam.com/article/an-in...riggers-part-i
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
 Originally Posted by iordanis
When inserting records in the table OR updating a column to a certain value (eg. "TRUE") inside a record, I want to automatically insert some records into other tables as well. In any other case, I don't want to do anything.
The inserted table is used both in update and insert.
It sounds like you need to use the INSTEAD OF clause and then do whatever you need to do to all tables you need to touch.
http://msdn2.microsoft.com/en-us/lib...4(SQL.80).aspx
What version of SQL Server? It might be helpful if you were to post some code.
It's not what you don't know that gets you in trouble. It's what you know that just ain't so. -Mark Twain
-
UPDATE or INSERT in a trigger?
I just came up with this idea. If the count of records in the deleted table is >0 then an update must have been taken place, otherwise an insert. Could this be feasible (I haven't tried this yet, but I will shortly)
-
Actually, it sounds like a classic trigger situation and solution.
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
EDIT: My bad. There I go getting myself in trouble again. 
Okay. I finally got curious enough to start playing with this. I see what you mean now about how the IF UPDATE() returns True for both inserts and updates.
There may be a better way to do this, but what seems to work is -
a) Check for records in the inserted table that already exist, then do the update procedures for those.
b) Check for records in the inserted table that do not exist and do the insert procedures for those.
An interesting thing about doing this is that it causes INSERTs to behave like the MERGE statement in Oracle (and SQL 2008). The trigger just handles updates for records that already exist and then inserts the rest.
Here is my test code.
Code:
CREATE TRIGGER trg_myTriggerTest
ON myTriggerTest
INSTEAD OF INSERT, UPDATE
AS
BEGIN
DECLARE @recs INT
-- Look for records in the inserted table that already exist...
SELECT @recs = COUNT(*)
FROM myTriggerTest a
JOIN inserted i ON i.myID = a.myID
IF @Recs > 0
UPDATE myTriggerTest SET
myVal = i.myVal,
myComment = 'UPDATED'
FROM myTriggerTest a
JOIN inserted i ON i.myID = a.myID
-- Now look for records in the inserted table that don't exist...
SELECT @recs = COUNT(*)
FROM inserted i
LEFT JOIN myTriggerTest a ON a.myID = i.myID
WHERE a.myID IS NULL
IF @Recs > 0
INSERT INTO myTriggerTest(myID, myVal, myComment)
SELECT i.myID, i.myVal, 'INSERTED'
FROM inserted i
LEFT JOIN myTriggerTest a ON a.myID = i.myID
WHERE a.myID IS NULL
END
Code:
INSERT INTO myTriggerTest(myID, myVal)
SELECT 1, 'Test' UNION
SELECT 2, 'Test' UNION
SELECT 3, 'Test' UNION
SELECT 4, 'Test' UNION
SELECT 5, 'Test'
INSERT INTO myTriggerTest(myID, myVal)
SELECT 1, 'Test' UNION
SELECT 2, 'Test' UNION
SELECT 3, 'Test' UNION
SELECT 4, 'Test' UNION
SELECT 5, 'Test' UNION
SELECT 6, 'Test' UNION
SELECT 7, 'Test' UNION
SELECT 8, 'Test' UNION
SELECT 9, 'Test' UNION
SELECT 10, 'Test' UNION
SELECT 11, 'Test' UNION
SELECT 12, 'Test' UNION
SELECT 13, 'Test' UNION
SELECT 14, 'Test' UNION
SELECT 15, 'Test' UNION
SELECT 16, 'Test' UNION
SELECT 17, 'Test' UNION
SELECT 18, 'Test' UNION
SELECT 19, 'Test' UNION
SELECT 20, 'Test'
Last edited by Slope; 03-18-2008 at 05:32 PM.
It's not what you don't know that gets you in trouble. It's what you know that just ain't so. -Mark Twain
Similar Threads
-
By carbon_13 in forum Database
Replies: 14
Last Post: 03-14-2008, 04:03 PM
-
By zarien in forum Database
Replies: 1
Last Post: 05-17-2006, 03:27 AM
-
By Steve in forum Database
Replies: 0
Last Post: 02-19-2003, 04:51 AM
-
By Steve in forum Database
Replies: 0
Last Post: 02-19-2003, 02:14 AM
-
By bigbastard4 in forum Database
Replies: 2
Last Post: 05-16-2001, 06:24 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks