2 SQL Updates in 1 statement - sql

Im trying to do something like the following:
UPDATE table SET fieldA='valueA' WHERE id='id1', SET fieldB='valueB' WHERE id='id2';
But cant seem to get it to work. Is there a way of doing this, or will I need to use two separate SQL commands?

If you really need to do it in one statement, you can use this:
UPDATE table
SET fieldA = case when id = 'id1' then 'valueA' else fieldA end,
fieldB = case when id = 'id2' then 'valueB' else fieldB end
WHERE id in ('id1', 'id2');
But for clarity (and thus maintainability), it would be much better to use two statements.

This is best done as 2 different UPDATE statements, because you have 2 different WHERE clauses:
UPDATE table SET fieldA='valueA' WHERE id='id1'
UPDATE table SET fieldB='valueB' WHERE id='id2'

MERGE INTO YourTable
USING ( VALUES ( 'id1', 'valueA', NULL ),
( 'id2', NULL, 'valueB' ) )
AS source ( id, fieldA , fieldB )
ON YourTable.id = source.id
WHEN MATCHED THEN
UPDATE
SET fieldA = COALESCE(source.fieldA, YourTable.fieldA),
fieldB = COALESCE(source.fieldB, YourTable.fieldB);

You'll need to use 2 separate SQL-statements because the set operator works on the whole resultset that's filtered by the where-clause.
UPDATE table SET fieldA='valueA' WHERE id='id1'; UPDATE table SET fieldB='valueB' WHERE id='id2';

UPDATE table SET fieldA='valueA',fieldB='valueB' WHERE id in ('id1','id2')

Related

How to do an update on a query having union in postgrsql?

I have 2 tables name test and test_snapshop. in test table i am saving the current data and in test_snapshot i am saving older data. Now i need to do an update against some id values.theer is a chance that some ids exist in test and some in snapshot table.
I tried like this
update test
set column1=value,
column2=value,
column3=value
where column4 in(1,2,3)
union
update test_snapshot
set column1=value,
column2=value,
column3=value
where column4 in(1,2,3)
Both tables have same no and name of column.
The above query is not as expected. what I am doing wrong in this query. can any one help me in this.
I am new to postgresql.
Although I voted to close as a typo, you can express this in Postgres using a single statement. I might go for:
with ids as (
select *
from (values (1), (2), (3)) v(id)
),
t as (
update test
set column1 = value,
column2 = value,
column3 = value
where column4 in (select i.id from ids i)
)
update test_snapshot
set column1 = value,
column2 = value,
column3 = value
where column4 in (select i.id from ids i);
With this structure, you only need to list the id values once.

updating for several columns with distinct conditions

I have INSERT statement where values are provided through SELECT from other table. ON CONFLICT I'm updating several columns. I'm just wondering if is possible to SET each column matching unique condition
Now I have solution which work, however it isn't ideal.
Basically something like this would match my desired result..
WITH table_a (
--joining two tables
)
INSERT INTO table_b
SELECT * FROM table_a
ON CONFLICT
ON CONSTRAINT table_b_pkey DO UPDATE
SET column_a = EXCLUDED.column_a
WHERE table_b.column_a < EXCLUDED.column_a
OR
SET column_b = EXCLUDED.column_b
WHERE table_b.column_b < EXCLUDED.column_b
Use CASE, e.g.:
INSERT INTO table_b
SELECT * FROM table_a
ON CONFLICT
ON CONSTRAINT table_b_pkey DO UPDATE
SET
column_a = CASE
WHEN table_b.column_a < EXCLUDED.column_a
THEN EXCLUDED.column_a
ELSE table_b.column_a END,
column_b = CASE
WHEN table_b.column_b < EXCLUDED.column_b
THEN EXCLUDED.column_b
ELSE table_b.column_b END

insert data where all rows equals to null

Is there a way to insert data where all rows are equals to null? I know it looks like
select login from Users
where login is null
When it return me
1. null
2. null
3. null
and so on...
How can I fill data to all this null rows?
To just update the null values in one column then do this;
UPDATE TableName
SET FieldName = ISNULL(FieldName,'NewValue')
If you want to update all columns when they ALL are NULL then you can do something like this
UPDATE TableName
SET
Field1 = 'Value1'
,Field2 = 'Value2'
,Field3 = 'Value3'
WHERE
Field1 IS NULL
AND Field2 IS NULL
AND Field3 IS NULL
If you want to replace all NULL values in the table regardless of whether the whole row is NULL then you can do this;
UPDATE TableName
SET
Field1 = ISNULL(Field1,'Value1')
,Field2 = ISNULL(Field2,'Value2')
,Field3 = ISNULL(Field3,'Value3')
If you want to update the values to different values for each row you will need a way of linking a user to their login. A basic version would be something like this (I'm referring to this as LookupTable)
ID Login
1 User1
2 User2
3 User3
Your query will be something like this;
UPDATE a
SET a.login = ISNULL(a.login,b.login)
FROM TableName a
JOIN LookupTable b
ON a.id = b.id
This will only update values with a NULL but you'd probably want to just set a.login = b.login to ensure that all of your data is correct.
UPDATE Users
SET login = 'value'
WHERE login is NULL
This would update the login column with the value 'value', if the current value is NULL.
Assuming that all columns in a table can take NULL values and do not have defaults, you can insert a row into a table with all NULL values by doing:
insert into t(col)
select NULL;
insert should have at least a single column; any column will do.

SQL conditionally update based on 2 different values

I want to update a field based on a where IN () clause but i also want the update the rest of the values using WHERE NOT IN()..For example:
UPDATE TABLE SET COLUMN1 = X WHERE COLUMN2 IN (1,2,3)
UPDATE TABLE SET COLUMN1 = Y WHERE COLUMN2 NOT IN (1,2,3)
To simply put it ,is there a way to combine these two queries?
UPDATE
TABLE
SET
COLUMN1 = CASE WHEN COLUMN2 IN (1,2,3) THEN X ELSE Y END

update query with insertion?

i need a query which have to do both update and insert..
first i have to check this condition
SELECT TOP 1 * FROM NEC_Customer_DB_Map where DB_AvailabilityFlag = 'Y'
and if DB_AvailabilityFlag = 'Y' i have to update this 'Y' as 'ASSIGNED' and also i
have to insert by using
INSERT INTO NEC_Customer_DB_Map(NEC_CustomerCode,NEC_CustomerName) VALUES(#NEC_CustomerCode,#NEC_CustomerName)
can anyone combine these into one query..Any Suggestion?
Since your question is tagged with sql-server-2008 you could consider usinge the MERGE-statement:
http://technet.microsoft.com/en-us/library/bb510625.aspx
You could use a where clause to make the insert conditional:
INSERT INTO NEC_Customer_DB_Map(NEC_CustomerCode,NEC_CustomerName)
SELECT #NEC_CustomerCode,#NEC_CustomerName
WHERE EXISTS (SELECT * FROM NEC_Customer_DB_Map where DB_AvailabilityFlag = 'Y')