SQL update query - sql

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;

Related

How to add text in the beginning of each record in a row in sql

i have the following data set in the leads column:
**leads**
1234567
1234534
1234543
i want it to be updated as
**leads**
LEA-1234567
LEA-1234534
LEA-1234543
You can use concat.
select
concat('LEA-', leads) as lead
from yourTable
Maybe you can try to use update script directly
update table set table.leads = concat('LEA-', table.leads)
UPDATE yourTable
SET leads = "LEA-" || leads
Update tablename
set leads='LEA-'+leads

How does SQL Server handle an update with no change?

I want to run an update statement using a case statement. 99% of time there will be no change. So I was wondering about the performance.
For example if I run
update <table>
set field1 = field1
does it copy field1 and then write it into field1 or does it do nothing?
That depends on the database and in SQL Server, the answer is yes.
You should filter the values instead. Instead of:
update t
set x = (case when a = b then c else x end);
You should do:
update t
set x = c
where a = b;

Oracle Trigger Update with Where Clause

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.

Append column's data to another column in SQL Server

I want to append everything in Field1 to Field2 and then make Field1 values NULL for all records in the table. I do not want to merge these fields into a single field, I still want both fields to remain.
Take the following example:
Field 1 Field 2
Test Value NULL
NULL Another Value
My Value Current Value
I want to end up with:
Field 1 Field 2
NULL Test Value
NULL Another Value
NULL Current ValueMyValue
Thanks in advance!
How about:
UPDATE table
SET Field2 = isnull(Field2,'') + isnull(Field1,''), Field1 = NULL
What I would suggest if you are not sure about it is to wrap the update in a BEGIN TRAN, ROLLBACK like so:
BEGIN TRAN
SELECT * FROM thistable
UPDATE thistable
SET Field2 = isnull(Field2,'') + isnull(Field1,'')
, Field1 = NULL
SELECT * FROM thistable
ROLLBACK
That way you will get a view of what the query will do before it makes the change 'permanent'. Once you are happy change ROLLBACK to COMMIT and run again.
VIEW DEMO HERE
How about:
UPDATE MyTable
SET Field2 = ISNULL(Field2, '') + ISNULL(Field1, '')
To join the values into Field2.
And
UPDATE MyTable
SET Field1 = NULL
To clear out Field1 once you are sure the first script worked.
How about a little something like this:
UPDATE <Table name> SET field_2 = CONCAT(field_2, field_1);
UPDATE <Table name> SET field_1 = NULL;

updating a field where field cannot be less than zero

im using the following query to update my field
UPDATE tableName SET fieldName = fieldName-10
WHERE id=1;
and this is working fine in my case but i want that if the result is less than zero than query should not execute.
i've tried this
UPDATE tableName SET fieldname = fieldName-10
WHERE id=1 and fieldName>0;
but it returns with an error syntax error in your UPDATE statement.
thanx in advance
did you mean this? if pqty is the field of your table.
UPDATE tableName SET fieldname = pqty-10 WHERE id=1 and pqty>10;