|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 7:16 AM
Points: 2,030,
Visits: 2,869
|
|
It seems logical that you can't update a record that isn't physically written, yet.
Thanks for the question.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232,
Visits: 1,046
|
|
| Nice question. I also did not quite understand if the question meant could this be done with a trigger, or could you actually update the inserted table to change the inserted data.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 3:32 PM
Points: 8,980,
Visits: 8,540
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 7,185,
Visits: 7,285
|
|
Koen (da-zero) (1/9/2011) A nice and easy one. Thanks! I though so too when I read it; and then saw that more than half the people so far had got it wrong, and found myself seriously wondering how they could get this one wrong
Sean Lange (1/10/2011) Nice straight forward question.
It seems that once again there are people who will complain about the wording of the question no matter how straight forward it is. That certainly appears to be true
For those of you who complain about the wording. Stop splitting hairs, answer the question in the very clear and obvious context it was intended. Then write your own "perfect" question that will be 100% clear and unambiguous to every person on the planet regardless of language or cultural differences. My third QoTD is due tomorrow. I expect to be told it's unclear/ambiguous, although I don't think it is. It just goes with writing questions, I guess. 
Edit: and I have to point out that what is and what isn't splitting hairs isn't something on which everyone will agree. Here I agree with you. In the context of a trigger "the inserted table" is a thoroughly well-defined and unambiguous term - or more accuratelu you and I believe that, while others clearly don't.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
http://es.linkedin.com/in/tomthomsonsoftware
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 3:33 PM
Points: 18,858,
Visits: 12,443
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 10:13 AM
Points: 1,573,
Visits: 1,732
|
|
In my previous life as an Oracle DBA / Developer, I think I understand this question better than people with just a SQL Server background. Other database systems have the concept of a "BEFORE" trigger that occurrs before the actual statement being executed applies changes to the table. SQL Server only has AFTER triggers
Other database systems also allow row level triggers instead of just statement level triggers like SQL Server does, and a row level trigger typically gives you OLD and NEW context variables for each column in the table the trigger is on. Using those context variables in a BEFORE trigger you could actually change the values of data using the trigger. A common use of this would be in a table that has audit columns in it such as LastModifiedDateTime. Setting the NEW context variable for LastModifiedDateTime in a BEFORE trigger would keep the value the way you want it but with only 1 modification of the row in the database. A table with a SQL Server AFTER trigger will actually cause the row to be modified twice, and in SSMS you actually see the message (1 row(s) affected) twice.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 7:45 PM
Points: 3,250,
Visits: 65,570
|
|
Chris Harshman (1/10/2011) In my previous life as an Oracle DBA / Developer, I think I understand this question better than people with just a SQL Server background. Other database systems have the concept of a "BEFORE" trigger that occurrs before the actual statement being executed applies changes to the table. SQL Server only has AFTER triggers
No SQL Server has AFTER, FOR and INSTEAD OF triggers. Instead of is your BEFORE trigger which is a silly concept with the term BEFORE. Before I insert to a table or update a table, I haven't done anything. INSTEAD of makes more sense. INSTEAD of INSERTING or UPDATING a table, do xyz, which likely includes an INSERT or an UPDATE statement.
Books Online see "CREATE TRIGGER"
FOR | AFTER AFTER specifies that the DML trigger is fired only when all operations specified in the triggering SQL statement have executed successfully. All referential cascade actions and constraint checks also must succeed before this trigger fires.
AFTER is the default when FOR is the only keyword specified.
AFTER triggers cannot be defined on views.
INSTEAD OF Specifies that the DML trigger is executed instead of the triggering SQL statement, therefore, overriding the actions of the triggering statements. INSTEAD OF cannot be specified for DDL or logon triggers.
At most, one INSTEAD OF trigger per INSERT, UPDATE, or DELETE statement can be defined on a table or view. However, you can define views on views where each view has its own INSTEAD OF trigger.
INSTEAD OF triggers are not allowed on updatable views that use WITH CHECK OPTION. SQL Server raises an error when an INSTEAD OF trigger is added to an updatable view WITH CHECK OPTION specified. The user must remove that option by using ALTER VIEW before defining the INSTEAD OF trigger.
--Mark Tassin MCITP - SQL Server DBA Proud member of the Anti-RBAR alliance. For help with Performance click this link For tips on how to post your problems
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Yesterday @ 4:47 AM
Points: 366,
Visits: 707
|
|
Just arrived in work and surprised by the reaction to this one! Apologies if the wording was confusing to some. I tried to make the question has clear as possible but sometimes what can make sense in your head doesnt make sense to everyone. 
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 5:35 PM
Points: 5,722,
Visits: 6,194
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:45 PM
Points: 5,297,
Visits: 7,240
|
|
mtassin (1/10/2011)
Chris Harshman (1/10/2011) In my previous life as an Oracle DBA / Developer, I think I understand this question better than people with just a SQL Server background. Other database systems have the concept of a "BEFORE" trigger that occurrs before the actual statement being executed applies changes to the table. SQL Server only has AFTER triggers
No SQL Server has AFTER, FOR and INSTEAD OF triggers. Instead of is your BEFORE trigger which is a silly concept with the term BEFORE. Before I insert to a table or update a table, I haven't done anything. INSTEAD of makes more sense. INSTEAD of INSERTING or UPDATING a table, do xyz, which likely includes an INSERT or an UPDATE statement. The term BEFORE trigger is, in my opinion, not silly. ANd it is defintely not the same as an INSTEAD OF trigger.
A BEFORE trigger (which does not exist in SQL Server, but is implemented in severl other DBMS's) will fire after the rows to be updated (or inserted or deleted) are determined, but before the modification is actually carried out. In a BEFORE trigger, the data in the inserted pseudo-table can be changed. After the BEFORE trigger has executed, the database will carry out the changes that were determined before the trigger was fired - but it will also respect modification made by the trigger to the changed data.
An INSTEAD OF trigger also fires after determining the rows to be affected and before actually affecting them, but this type of trigger runs (as the name implies) instead of actually changing the data. If the INSTEAD OF trigger does nothing, then the changes will just be silently ignored.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|