Simple SQL if/else clause fails - sql

I am trying to do a simple if/else clause in sql (Running with mysql/phpmyadmin). But I get
1064 - You have an error in your SQL syntax
SET #age = 50;
IF #age > 60
THEN
Select `table1`.`Name` FROM table1 WHERE `Age` = 'Old'
ELSE
Select `table1`.`Name` FROM tablel WHERE `Age` = 'NotSoOld'
END IF;
What is wrong?

tablel (L minus) instead of table1 (the number one) ?

I figured it out with using the case statement instead. Thank for the clue Martin!
SQL: IF clause within WHERE clause

Related

Merge statement for first time insert

I did some research and couldn't find any solution and hence posting it here.
We have a dataload job which runs on a daily basis. We have separate DML statements to insert, update etc. We wanted to avoid insert statements to run multiple times.
Is there an option to use merge statement in this scenario to update if the record is present or insert if not present?
Please give me an example if possible as I am quite new to sql statements.
We are using Oracle db2 for dbms
Thanks in advance!
Use an IF EXISTS statement, like this:
IF EXISTS (SELECT 1 FROM #mytable# WHERE #column1# = #this# AND #column2# = #that)
BEGIN
#update statement#
END
ELSE
BEGIN
#insert statement#
END
EDIT:
the syntax for this can be found here: https://www.toadworld.com/platforms/ibmdb2/w/wiki/7777.ifthenelse
this means that my general case would become this in DB2:
IF (#count of value# = 1) THEN
#updatestatement#;
ELSEIF (#count of value# > 1) THEN
#throw an error, as you cannot update uniquely#
ELSE
#insertstatement#;
END IF;
I also noticed that you can run SQL commands on DB2, which might be useful for this task as well
finally, have a look here, might give you further ideas :)
DB2 for IBM iSeries: IF EXISTS statement syntax
Got it done throught Merge statements itself :)
MERGE INTO table1(storeent_id, name, value) AS t1
USING (VALUES ((SELECT STOREENT_ID FROM STOREENT WHERE IDENTIFIER='test'), 'SOLR_SERVICEABILITY_URL',
'Sample')) AS t2(storeent_id, name, value)
ON t1.name = t2.name AND t1.storeent_id = t2.storeent_id
WHEN MATCHED THEN
UPDATE SET
t1.value = t2.value
WHEN NOT MATCHED THEN
INSERT
(name, storeent_id, value)
VALUES (t2.name, t2.storeent_id, t2.value);

UNNEST function in DB2 is throwing an error

I'm working on DB2 database and using TOAD to execute the below statement. STRINGARRAY is already created in MYSCHEMA as Arraytype.
BEGIN
DECLARE CASE_ID_LIST MYSCHEMA.STRINGARRAY;
SET CASE_ID_LIST = ARRAY['A001','A002','A003','A004'];
SELECT T.ID,T.NUM FROM UNNEST(CASE_ID_LIST) AS T(ID,NUM);
END
This statement is throwing an error saying "SQL0104N An unexpected token "UNNEST" was found following ....."
My actual intention is to pass arraylist in IN clause of the where condition. I'm trying this select statement to test the use of UNNEST function.
My final query will look something like this:
BEGIN
DECLARE CASE_ID_LIST MYSCHEMA.STRINGARRAY;
SET CASE_ID_LIST = ARRAY['A001','A002','A003','A004'];
SELECT * FROM MYSCHEMA.TABLENAME WHERE CASE_ID IN (SELECT T.NUM FROM UNNEST(CASE_ID_LIST) AS T(NUM));
END
Please advice how I can get this work. Thank you in advance!!

SQL IF EXISTS with OR Condition

I am facing a performance issue with a SQL statement. I have noticed a performance degradation in one SQL statement in one of a procedure.
SQL Statement:
IF EXISTS(SELECT TOP 1 FROM TABLE1 WHERE COLUMN1 = 'XYZ') OR #ISALLOWED = 1
BEGIN
-- SQL Statements
END
I couldn't able to understand why OR statement with IF Exists statement is causing performance issue in above query. Because if I rewrite the above statement like this:
DECLARE #ISVALUEEXISTS BIT
SET #ISVALUEEXISTS = 0
IF EXISTS(SELECT TOP 1 FROM TABLE1 WHERE COLUMN1 = 'XYZ')
SET #ISVALUEEXISTS = 1
IF (#ISVALUEEXISTS = 1 OR #ISALLOWED = 1 )
BEGIN
--SQL Statements
END
Then performance issue is gone. So, I'm couldn't able to understand how and why OR condition with IF Exists statement is causing problem.
Anyone has any idea about this?
If you have this query inside stored procedure this could happen because of parameter sniffing.
Try something like this to check it:
declare #ISALLOWED_internal
select #ISALLOWED_internal = #ISALLOWED
IF EXISTS(SELECT TOP 1 FROM TABLE1 WHERE COLUMN1 = 'XYZ') OR #ISALLOWED_internal = 1
BEGIN
-- SQL Statements
END

MISSING RIGHT PARENTHESIS ERROR WITH PARENTHESIS CORRECT

I am running an SQL query and have just two parenthesis but I still get the error "missing parenthesis". The code is shown below:
Update ALEKWE_CUSTOMER C
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE C.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
If this is for sql-server, then this might be the answer:
Update ALEKWE_CUSTOMER
set recently_purchased = (CASE WHEN EXISTS(
SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D WHERE customer_id=D.customer_id) THEN 'Y'
ELSE 'N' END);
You need to put the whole CASE structure in parentheses too.
Next to that, you can't use an alias for the table which will be updated.
You cannot have the alias C at this position, and neither do you need it:
Update ALEKWE_CUSTOMER
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE ALEKWE_CUSTOMER.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
You don't need to put the case into paranteses either.
You would need:
Update ALEKWE_CUSTOMER
set recently_purchased = CASE WHEN EXISTS(SELECT * FROM ALEKWE_CUSTOMER_PRODUCT AS D
WHERE C.customer_id=D.customer_id)
THEN 'Y' ELSE 'N' END;
FROM ALEKWE_CUSTOMER C
for your alias to work
Edit:
My altered statements work fine on Microsoft SQL-Server.
Apparently the problem is specific to Oracle SQL.
After reading your comments, I added Oracle to your tags, since you say this nowhere.

Sql short circuit OR or conditional exists in where clause

I'm trying to force sql server to make a short circuit OR comparison on some fields. On the left side of the Or I have a simple variable comparison and on the right side I have a pretty 'heavy' subquery.
WHERE
(#Var = 'DefaultValue' ) OR
Exists(select * from atable)
Is there any way to only execute the right side of the or statement if the first statement is false.
I've tried case and if else statements but can't find any working syntax.
I'm using a MS SQL server
You can't do what you want in a single SQL statement. You can do something like this however in a stored proc
If #Var = 'DefaultValue' then
BEGIN
SELECT * FROM table
END
ELSE
BEGIN
SELECT * FROM table
WHERE Exists(select * from atable)
END
If you've got a lot of inputs you should consider Dynamic SQL
How about this?
WHERE 1 = CASE #Var
WHEN 'DefaultValue' THEN 1
ELSE (SELECT TOP 1 1 FROM atable)
END