How do I update multiple tables with multiple where clauses in one query - sql-server-2012

Is there a way to do multiple updates with one sql query?
Something like:
UPDATE Table1
SET Column1 = Expression1 WHERE Conditions 1
SET Column2 = Expression2 WHERE Conditions 2
SET Column3 = Expression3 WHERE Conditions 3
...;
Thank you!

Yes, it looks like an anti pattern, but, syntactically, you can do it using Case expression:
UPDATE Table1
SET
Column1 = ( case when condition1 then Expression1 else Column1 end ),
Column2 = ( case when condition2 then Expression2 else Column2 end )
By the way, I guess you mixed "multiple tables" by "multiple columns".

Related

Which condition satisfies among multiple conditions in SQL query

This is my SQL query :
select *
from table1
where Column1 less than Condition1
|| Column2 less than Condition2
|| Column3 Less than Condition3
Now I am trying to find which condition out of above 3 conditions satisfies for a particular record in a table.
Are you sure it's SQL SERVER? If yes, then correcting the syntax, and adding your logic.
Like Karras mentioned, you already have * in the select which was already making you sure which column was satisfying the WHERE clause, but this code may help you further.
SELECT *,
CASE
WHEN Column1 < Condition1
THEN 'Column 1 satisfies'
WHEN Column2 < Condition2
THEN 'Column 2 satisfies'
WHEN Column3 < Condition3
THEN 'Column 3 satisfies'
END as ConditionFlag
FROM table1
WHERE Column1 < Condition1 OR Column2 < Condition2 OR Column3 < Condition3;
As long as you use * to return the full result set, you should be able to see the values for Column1, Column2, Column3 and understand which of the conditions evaluated to true.

Select statement subquery, multiple conditions

I am trying to create a query to select a certain condition then within that condition select two other conditions.
Breaking it down.
SELECT condition 1 FROM column 2, if this condition is not met return nothing.
SELECT condition 2 FROM column 3, SELECT condition 3 FROM column 4, if either of these two conditions are met return the respective column value from that rows value.
My feeble attempt which gives an obvious syntax error,
SELECT Column_1
FROM Data_TBL
WHERE Column_2 = 'Condition_1'
GROUP BY(WHERE Column_3 = 'Condition_2' OR Column_4 = 'Condition_3')
ORDER BY Column_1 ASC
Still very new to SQL statements and I am struggling with the syntax.
I think you just need a where clause. For the filtering:
select t.*
from data_tbl t
where (column2 = 'Condition_1') and
(column3 = 'Condition_2' or column4 = 'Condition_3);
I'm not sure what you want to return when both column3 and column4 meet the respective conditions, but I think this is what you want:
select (case when column3 = 'Condition_2' then column3 else column4 end)
from data_tbl t
where (column2 = 'Condition_1') and
(column3 = 'Condition_2' or column4 = 'Condition_3);

Case when statement in SQL

I am using the following query. In this query I want to apply the where clause based on passed parameter. But the issue is that where clause is like 'value = if parameterVal = 'I' than NULL else NOT NULL'
I've build a query like this
SELECT * FROM MASTER
WHERE
Column1 IS (CASE WHEN :Filter = 'I' THEN 'NULL' ELSE 'NOT NULL' END)
but it's not working. Help me solve this.
UPDATE
Updating question to elaborate question more clearly.
I've one table MASTER. Now I am passing one parameter in query that is Filter (indicated by :Filter in query).
Now when the Filter parameter's value is 'I' than it should return the following result.
SELECT * FROM MASTER WHERE Column1 IS NULL
but if the passed argument is not equal to 'I' than,
SELECT * FROM MASTER WHERE Column1 IS NOT NULL
SELECT * FROM MASTER
WHERE (Filter = 'I' AND Column1 IS NULL)
OR
(Filter <> 'I' AND Column1 IS NOT NULL)
If you really insist on using a CASE the SELECT could be rewritten as:
SELECT *
FROM MASTER
WHERE CASE
WHEN COLUMN1 IS NULL AND FILTER = 'I' THEN 1
WHEN COLUMN1 IS NOT NULL AND FILTER <> 'I' THEN 1
ELSE 0
END = 1
SQLFiddle here
Frankly, though, I think that this is very difficult to interpret, and I suggest that #MAli's version is better.
Your case has assignment not equality check

"IN" condition at CASE WHEN on WHERE CLAUSE?

I have an complex situation. I want to write an sql query including "case when" condition on "where clause".
Just like that:
SELECT *
FROM <table>
WHERE
<Column1> in
CASE <Column2>
WHEN 1 THEN ('OP', 'CL')
WHEN 0 THEN ('RE', 'ST')
END
Column1 must be "in", not "=". Because there is multiple value at condition for Column1. That query returns "Incorrect syntax near ','." error.
Can you give me any suggestion? (Sorry for my bad English.)
EDIT : I think I misunderstood. If Column2 is 1, condition must like that "IN ('OP', 'CL')" else Column1 is 2, condition must like that "IN ('RE', 'ST')".
You don't need a CASE expression for that, you can just use OR like this:
SELECT *
FROM <table>
WHERE (Column2 = 1 AND Column1 IN ('OP', 'CL')) OR
(Column2 = 0 AND Column1 IN ('RE', 'ST'))
Select * from table where <Column1> in (Select case <Column2> when 1 then ('OP','CL') when
0 then ('RE','ST'))

Selecting filtered rows with SQL

I am constructing an SQL statement with some parameters. Finally, an SQL statement is created like
"select * from table where column1 = "xyz"".
But I also need the rows which are filtered with this statement. In this case they're rows which are not "xyz" valued in column1. More specifically, I am looking for something like INVERSE(select * from table where ...). Is it possible?
Edit: My bad, I know I can do it with != or operator. Here the case is, select statement may be more complex (with some ANDs and equal, greater operators). Let's assume a table has A,B,C and my SQL statement brings only A as result. But I need B and C while I only have the statement which brings A.
select * from table where column1 != 'xyz' or column1 is null;
If you want the other ones, do it like this:
select * from table where column1 <> "xyz"
column1 <> (differs from) "xyz"
To check if something is no equal you can use <> or even !=
SELECT *
FROM yourTable
WHERE <> 'xyz'
OR
SELECT *
FROM yourTable
WHERE != 'xyz'
Many database vendors support (see list) both versions of the syntax.
If you're retrieving both result sets at about the same time, and just want to process the xyz ones first, you could do:
select *,CASE WHEN column1 = "xyz" THEN 1 ELSE 0 END as xyz from table
order by CASE WHEN column1 = "xyz" THEN 1 ELSE 0 END desc
This will return all of the rows in one result set. Whilst xyz = 1, these were the rows with column1 = 'xyz'.
It was :
"select * from table where rowId NOT IN (select rowId from table where column1 = "xyz")
I needed a unique rowId column to achieve this.