How to send update batch with statements simultaneously in SQL - sql

BEGIN TRANSACTION
UPDATE table_x
SET Part = 8
WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'X'
UPDATE table_x
SET Part = 0
WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'Y'
UPDATE table_x
SET Part = 2
WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'Z'
COMMIT TRANSACTION
Basically we have three rows that together has to have a value total of 10 (as you can see now it is 8+0+2 = 10). I have a trigger that checks so the value always should be 10 when updating or inserting new values, but as for now it wont be able to update since it sends the first UPDATE "WHERE A = 2 AND B = 2 AND C = 1 AND Code = 'X' " to check, and the sum will in most cases not go through since it will be either more or less than 10.
What I want is to send all these UPDATES at the same time so the values changes together. Is there any way to make that happen?

SQL Server is actually a bit smarter about triggering, so one statement should work:
UPDATE table_x
SET Part = (CASE Code WHEN 'X' THEN 8 WHEN 'Y' THEN 0 ELSE 2 END)
WHERE A = 2 AND B = 2 AND C = 1 AND Code IN ('X', 'Y', 'Z');
The trigger itself has to be really smart about checking inserted values rather than values in the table, but this is doable.
Alternatively (and possibly the better solution under some circumstances) is to disable the trigger for the updates. This has the advantage that the trigger code does not need to change.

Is this what you want to do:
UPDATE table_x
SET Part = CASE WHEN Code = 'X' THEN 8
WHEN Code = 'Y' THEN 0
WHEN Code = 'Z' THEN 2
END
WHERE A = 2 AND B = 2 AND C = 1 AND Code IN ('X','Y','Z')

Related

Is there a way to create a new column and assign values based on criteria on an existing column in a SQL query?

I want to create a new column in my query output and assign values to it based on existing columns in my input. Let's say i have column A in my input and want to create column B in my query output. How do I add the following logic in a sql select statement?
If A = 1 then set B to x
Else if A = 2 then set B to y
Else if A = 3 then set B to z
Else set B to null
I have to do this for multiple new columns in my output within the same query.
This is a case expression:
select . . .,
(case A when 1 then 'x' when 2 then 'y' when 3 then 'z' end) as b
Something like this?
SELECT A,
CASE
WHEN A = 1 THEN 'x'
WHEN A = 2 THEN 'y'
ELSE 'z'
END AS B
FROM ...

Updating column B value case when conditional statement is true

I need to update Column C value to 1.5 when Column A = Column B Else it should be 4 in SQL Server.
I wrote something like this
UPDATE MyTable
SET Column C = 1.5 CASE WHEN Column A = Column B ELSE 4 END NewColumn.
I have never used UPDATE and CASE WHEN statement.
The correct syntax is:
UPDATE MyTable
SET C = (CASE WHEN A = B THEN 1.5 ELSE 4 END)
You can also use IIF() as
UPDATE MyTable
SET C = IIF(A=B, 1.5, 4);

Changing when to if statement in SQL server

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.
(Case when new_interestratevariability = 2
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6)
and new_crms_dt = #Curr_Date
then 0 else 1 end) as CIEDIT_VAL_96,
Currently, I am getting something like below:
Results Table
To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.
Select *
from table
Where new_interestratevariability = 2
and new_interestrateindex IN (1,2,3,4,6)
and new_crms_dt = #Curr_Date
Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone
(Case when new_interestratevariability = 2
and (new_interestrateindex not in(1,2,3,4,6))
and new_crms_dt = #Curr_Date
then 1 else 0 end) as CIEDIT_VAL_96,

Join Two Select Statements With Different Columns And Condition

I'm not good at asking question, so i'll give an example of what i want to have.
if i = 1 and xi = 0 then
select a,b,c,d,e,f,g where z = 1
elseif i=0 and xi = 1 then
select a,c,f,h,l,n where w = var
elseif i=1 and xi=1 then
select a,b,c,d,e,f,g, where z = 1
union all
select a,c,f,h,l,n where w = var
end if
How can I join the 2 select statement if their columns are not equal and they both have a unique condition?
Based on the conditions you can create derived tables to fetch desired columns and then to get a union of the two tables add null values in column list of derived tables which have less number of columns:
Pseudo code:
select * from
(select a,b,c,d,e,f,g
where z = 1
and 1 = case when i = 1 and xi = 0 then 1
when i = 1 and xi = 1 then 1
else 0
end) as T1
union all
(select a,c,f,h,l,n ,null -- add null value to equate number of columns
where w = var
and 1 = case when i=0 and xi = 1 then 1
when i=1 and xi = 1 then 1
else 0
end) as T2
Hope this helps!!!
If it is not a requirement not to use dynamic sql I will opt for that one.
Another idea will be to use user defined function returnin tables.
So you encapsulate there the logic...

SQL - "IF" in Where Clause

I'm trying to write a stored procedure that will have 6 bit value flags as parameters and a couple of other values.
The pseudo sql I want to write is something like:
SELECT *
FROM theTable
WHERE
IF #flagA = 1 THEN theTable.A = 1
IF #flagB = 1 THEN theTable.B = 1
IF #flagC = 1 THEN theTable.CValue = #cValue
etc
Any ideas how I can do this in SQL or am I best reverting to building the SQL in C# (where this SP will be called from)?
SELECT *
FROM theTable
WHERE
(#flagA = 0 or (#flagA = 1 AND theTable.A = 1 ))
and (#flagB = 0 or (#flagB = 1 AND theTable.B = 1 ))
and (#flagC = 0 or (#flagC = 1 AND theTable.CValue = #cValue ))
Note: I am assuming your bit flags are non-nullable. If that is not the case, you will need to use ISNULL.