SQL query to update list of records by looping through a table records and check some logic? - sql

I do not want to use cursors for performance reasons.
Input Parameters for stored procedure: arg1, arg2,arg3 & arg4
For example:
Table A > A1 Column and A2 Column,
Table B > B1 Column (A.A1 <=>B.B1) foreign n primary key relation and B2 Column.
I want to update A.A2 value based on the following if condition,
if(arg1 == B.B2 && arg2 == B.B2)
{
Update A set A.A2 = 1 where A.A1 = arg4
}
else{
if(arg1 == 1 && arg3 == B.B2){
Update A set A.A2 = 0 where A.A1 = arg4
}
}
this is simple for one record but the Table A has 1000's records that match A.A1 = arg4 so i have to apply the above logic or case for all records and want to avoid using cursors...how do i do it?

Try the below query.
UPDATE tmp
SET tmp.A2 =
(CASE WHEN (tmp1.B2 == arg1 && tmp1.B2 == arg2) THEN 1 WHEN (arg1 == 1 && tmp1.B2 == arg3) THEN 0 ELSE tmp.A2)
FROM
A tmp
INNER JOIN
B tmp1
ON tmp.A1 = tmp1.B1
WHERE
tmp.A1 = arg4
Hope this Helps!!

In general, non-specific SQL-92 you could do this:
UPDATE A
SET A.A2 = CASE WHEN B.B2 IN (#Arg1,#Arg2) THEN 1
WHEN #arg1 = 1 AND B.B2 = #arg3 THEN 0 END
FROM A
JOIN B ON A.A1=B.B1
WHERE A.A1 = #arg4
You may need an ELSE before END if you don't want any values falling through (without the ELSE it would set A.A2 to NULL).

Related

If, Then and Select Case

I'm writing VB Code and I see a question below
There are three positive integers A, B, C
If A is greater than B, C is equal to A+B
If A is less than or equal to B, then C is equal to A-B.
Please use IF...Then and Select/Switch Case to write a program, both of which are used in this program, and additional variables can be added by yourself.
I would like to ask how to write this question, as far as I know, the answer just need only IF Then or Select Case can be realized?
Dim A As Double = 3
Dim B As Double = 2
Dim C As Double = 1
Dim D As Double = 0
D = A - B
Select Case D
Case D > 0
C = A + B
Case D < 0
C = A - B
End Select
If D > 0 Then
C = A + B
ElseIf D < 0 Then
C = A - B
End If
In the real world you wouldn't need to use both an If/Then statement and a Select/Case statement. Since this appears to be homework, I believe the exercise is to see if you can use both conditional check.
I would setup two functions that accept two arguments (a and b) that return the value of c. In your first function use an If/Then and in your second function use a Select/Case.
E.g.
Private Function IfThenOption(a As Integer, b As Integer) As Integer
Dim c As Integer
If (a > b) Then
c = a + b
ElseIf (a < b) Then
c = a - b
Else
c = a
End If
Return c
End Function
Private Function SelectCaseOption(a As Integer, b As Integer) As Integer
Dim c As Integer
Select Case True
Case a > b
c = a + b
Case a < b
c = a - b
Case Else
c = a
End Select
Return c
End Function
Example: https://dotnetfiddle.net/kwcyWc

Determine type of triangle

I'm trying the hacker rank type of triangle below where based on the 3 sides it has to be determined if the triangle is equilateral, isosceles, scaelene, or not a triangle.
https://www.hackerrank.com/challenges/what-type-of-triangle/problem
I'm not sure why the code below isn't passing the test case. Unfortunately, I can't download the test case to see why it isn't working.
SELECT CASE WHEN A = B AND B = C AND A = C THEN 'Equilateral'
WHEN (A = B AND B != C AND A != C) OR (B = C AND A != B AND A != C) OR (A = C AND A != B AND B != C) THEN 'Isosceles'
WHEN ((A + B) < C) OR ((B + C) < A) OR ((C + A) < B) THEN 'Not a triangle'
ELSE 'Scalene' END
FROM Triangles
Try something like this:
SELECT
CASE
WHEN A + B > C AND A + C > B AND B + C > A THEN
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene' END
ELSE 'Not A Triangle' END
FROM TRIANGLES
Only test for the type of triangle when it is a triangle.
Try this :
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A THEN
CASE WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A != B OR B != C OR A != C THEN 'Scalene'
END
ELSE 'Not A Triangle' END FROM TRIANGLES;
The condition for scalene triangle does not include the following condition in your original query : Side 1 + Side 2 <= Side 3

Using IF Statement inside a Where Clause in SQL Server

I have two params that are passed in a function which passed down to the SQL string. Variables can be null or has a value (int). If x is not null, use "this" column else use "that" column. I'm using SQL Server.
// Inside a function with two variables passed, x and y
$sql = "
SELECT
[...]
FROM
[...]
WHERE
[...]
AND [...]
-- This is the tricky part
AND
--- if $x is not null, use foo column else use bar column
IF (x, R.column = 2, R.another_column = 3)
[...]
";
Is this possible to select a column based on the value of the variable passed in?
AND
(
($x is not null and R.column = 2) OR
($x is null and R.another_column = 3)
)
Unless I'm misunderstanding, you just need logic, or a case statement would work.
$sql = "
SELECT
[...]
FROM
[...]
WHERE
[...]
AND
[...]
-- This is the tricky part
AND
--- if $x is not null, use foo column else use bar column
-- IF (x, R.column = 2, R.another_column = 3)
(
(X IS NULL AND R.column = 2)
OR
(X IS NOT NULL AND R.another_column = 3)
)
";
AND
--- if $x is not null, use foo column else use bar column
case
when x is NULL then R.another_column = 3
else R.column = 2
end
--in SQL Server...
DECLARE #x INT = 3;
SELECT *
FROM sys.schemas
WHERE (#x IS NULL AND schema_id = 2)
OR (#x IS NOT NULL and schema_id = 3)
--optional depending on how much you execute this query, it may help
OPTION(RECOMPILE)

return string value from SQL Server tables where column type is integer

In my mssql tables i have a clomun that data type is integer. and when i was try thats you know the return value is int.
select h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi from hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
int this sql statement "h.durum"' data type is integer.
but i want to make thats.
when h.durum is 0 return string is Yönetici
when h.durum 1 string is Normal Kullanıcı
how can i do in sql?
thank you for patience
Not sure if I understood the question, but try replacing h.durum on your SELECT with the following:
CASE WHEN h.durum = 0 THEN 'Yönetici'
WHEN h.durum = 1 THEN 'Normal Kullanıcı'
ELSE '???' END AS some_column_alias
Try this:
select
CASE
WHEN h.durum = 0 THEN N'Yönetici'
WHEN h.durum = 1 THEN N'Normal Kullanıcı'
END CASE ColumnName,
h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi
FROM hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
;

Conditional operator in Transact-sql

Is there a way to do this shorter, for instance using some sort of conditional operator in Transact-sql?
IF #ParentBinaryAssetStructureId = -1
BEGIN
SET #ParentBinaryAssetStructureId = NULL
END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = #ParentBinaryAssetStructureId
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
USE NULLIF()
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId = NULLIF(#ParentBinaryAssetStructureId,-1)
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
The ternary (conditional) operator in c like languages:
x = doSomething ? 5 : 7
would be written like this in SQL:
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 ELSE 0 END
There can be multiple cases (when clauses):
SELECT #x = CASE WHEN #doSomething = 1 THEN 5 WHEN #somethingElse = 1 THEN 20 ELSE 0 END
UPDATE BinaryAssets.BinaryAssetStructures
SET ParentBinaryAssetStructureId =
CASE ParentBinaryAssetStructureId
WHEN -1 THEN NULL
ELSE ParentBinaryAssetStructureId
END
WHERE BinaryAssetStructureId = #OriginalBinaryAssetStructureId
Give that a whirl