Say I have something like this :
UPDATE my_table set col2 = '5' where col1 = '111';
UPDATE my_table set col2 = '5' where col3 = '112';
And now I make a before update trigger and I want to know the columns used in UPDATE statement (i.e col1, col3). In other words, can I see the exact update statement that was used in trigger?. Is this possible ?
Thank you!
No you can't. The trigger has no knowledge of what statement led to its execution.
Related
I want to update columns of a table whose values are NULL and I want to do this for 5 rows, but I'm getting the error : missing SET keyword
I am running the query in oracle SQL developer
The query I'm using is
UPDATE top(5) table_name
set col1=value1,
col2=value2,
col3=value3 where col1=null;
Second query I used is
UPDATE table_name
set col1=value1,
col2=value2,
col3=value3 where col1=null and rownum<=5;
You can do this in below way:
UPDATE table_name
set (col1,col2,col3) = (select col1,col2,col3 from table_name where col1 is null and rownum<=5)
where col1 is null;
This question already has answers here:
T-SQL conditional UPDATE (v2)
(8 answers)
Closed 8 years ago.
I've been given a list of changes to make to a table. I was wondering how it would be possiblet to make all of the changes in one script...I've tried the following
UPDATE tableA
SET col1 = 'somedata' WHERE col2 = 'somereference'
SET col1 = 'someotherdata' WHERE col2 = 'someotherreference'
SET col1 = 'evenmoredata' WHERE col2 = 'anotherreference'
but this doesn't work. Is there a specific syntax I can use to achieve this, or am I stuck with doing it like this:-
UPDATE tableA
SET col1 = 'somedata' WHERE col2 = 'somereference'
UPDATE tableA
SET col1 = 'someotherdata' WHERE col2 = 'someotherreference'
for each change I want to make?
Use a case statement in the single set statement:
UPDATE tableA
SET col1 = case col2
when 'somereference' then 'somedata'
when 'someotherreference' then 'someotherdata'
when 'anotherreference' then 'evenmoredata'
else col1
end
Its a good idea to put the default in of the original value incase whatever where clause you're using mis-fires (and you should use the where clause anyway otherwise you'll update all rows)
What happens in this case, assuming col1 has initially the value of 10:
UPDATE myTable
SET col1 = 20,
col2 = col1 + 10
Will col2 be 20 or 30 after the update?
Too long for a comment.
It will be 20 according to the rules of standard SQL. The new values do not get committed until the end of the update statement. They are not committed row-by-row or column-by-column. Remember the ACID properties of databases -- all the changes take effect at the same time.
It is possible that some database out there does not behave this way. It is easy enough to check in practice.
The column will show 20
the Update function is running as a bulk.
you can do this quick test:
/* Create Test Table
select
10 as col1,
0 as col2
into TestTable
*/
/* update
update TestTable
set col1 = 20,
col2 = col1 + 10
*/
select * from TestTable
I want to update two fields in one sql query. How do I do that?
update tablename set field1= val1 where id=1
Now I want to update 2 fields as follows: How can I do that?
update tablename set field1 =val1 and set field2=val2 where id=1
Your syntax is almost correct, but you can't use AND.
UPDATE tablename SET field1=var1, field2=var2 WHERE id=1
Or to be safe, I like to write UPDATE statements like this:
UPDATE T
SET
T.Field1 = Value1
,T.Field2 = Value2
-- SELECT *
FROM TableName AS T
WHERE T.ID = 1
This way you can be sure of what you'll be updating.
UPDATE TableName
SET Field1=Value1
,Field2=Value2
WHERE id=id_value
Like the others, but this is how I like to indent and format it, on bigger complex queries, proper formating matters alot!
You almost had it:
update tablename
set field1=val1,
field2=val2
where id=1
UPDATE tablename SET field1 = var1, field2 = var2 WHERE id = 1;
COMMIT;
In a trigger, I have code like:
SET #var1 = (SELECT col1 FROM Inserted);
SET #var2 = (SELECT col2 FROM Inserted);
Is it possible to write the above in a single line? Something conceptually like:
SET (#var1,#var2) = (SELECT col1,col2 FROM Inserted);
Obviously I tried the above, without success; am I just stuck with the first method?
Even if possible, is that a good idea?
Thanks!
yes, use first method.
Or...
SELECT
#var1 = col1
,#var2 = col2
FROM
Inserted;
However, it is a major red flag if you are expecting to set variable values like that in a trigger. It generally means the trigger is poorly designed and needs revision. This code expects there will be only one record in inserted and this is something that is not going to be true in all cases. A multiple record insert or update will have multiple records in inserted and the trigger must account for that (please without using a trigger!!!). Triggers should under no circumstances be written to handle only one-record inserts/updates or deletes. They must be written to handle sets of data.
Example to insert the values from inserted to another table where the trigger is on table1:
CREATE TRIGGER mytrigger on table1
AFTER INSERT
AS
INSERT table2 (field1, field2, field3)
SELECT field1, 'test', CASE WHEN field3 >10 THEN field3 ELSE 0 END
FROM inserted
No, it is not possible. SET accepts a single target and value. AFAIK.