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.
Related
I need to update newly created column in my oracle table. To do so I need to use existing values in row to decide how to populate this column, I am getting error:
java.lang.NullPointerException -> See Debug Output for details
This is my query:
UPDATE
SCHEMA_NAME.TABLE_NAME
SET
OCO= IF CO= 'Y' AND COM='Y' THEN
{
'Y'
} ELSE
{
'N'
}
END IF;
Any suggestions on syntax?
You could use CASE expression in the SET clause.
For example,
UPDATE table
SET schema.column = CASE
WHEN CO= 'Y' AND COM='Y' THEN
'Y'
ELSE
'N'
END
I want to update a column 'XYZ_RCVD' to 'Y' if there is a value in the row, if not i want to update column 'XYZ_RCVD' to 'N'. I started with this simple script that worked but I had to modify it for the 'N' case. How would i merge both of them into one? I tried some IF-THEN-else but that didn't work too well with the INSERT statement.
Here is what i have so far.
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'Y'
WHERE XYZ_NAME IS NOT NULL;
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'N'
WHERE XYZ_NAME IS NULL;
The solution below by Habib worked perfectly.
I believe you are looking for CASE WHEN like:
UPDATE TEST_SURVEY
SET XYZ_RCVD = CASE WHEN XYZ_NAME IS NOT NULL THEN 'Y' ELSE 'N' END;
I have a check box on front end.
If the check box is checked: data with only checked chk1 should appear on front end.
If the check box is not checked: full data should appear on front end.
Please suggest how should I proceed with the same in SQL Using If else / Case statement.
I am using:
SELECT *
FROM table1
where (some conditions) AND
CASE #a.Id_Oem_Irm
WHEN 0 THEN (a.id_oem_irm in(0,1))
WHEN 1 THEN (a.id_oem_irm in (1))
END
PS: a.Id_Oem_Irm: a is the table name, Id_oem_irm is the column name for check box.
I would recommend writing this as:
where (some conditions) AND
((#a.Id_Oem_Irm = 0 and a.id_oem_irm in(0, 1)) OR
(#a.Id_Oem_Irm = 1 and a.id_oem_irm in (1) )
)
I am not sure what #a.Id_Oem_Irm is supposed to be. I suspect you want a variable there.
Or you could tune it a bit like this:
SELECT *
FROM table1
where (some conditions) AND
(a.id_oem_irm = 1 OR
#a.Id_Oem_Irm = 0)
This is only valid if #a.Id_Oem_Irm is always 0 or 1. Otherwise, you obviously should add a #a.Id_Oem_Irm IN (0,1) or <= 1 (if it can't be negative) condition.
What is a? The alias for another table you didn't include here?
The simpler way to do this is create a storedprocedure which will take the input "#a.Id_Oem_Irm"
Example:
CREATE PROCEDURE p_get_a_data(
IN Id INT(1)
)
BEGIN
IF (Id = 0) BEGIN
--YOur query to retrieve partiular row/ column
END;
IF(Id = 1) BEGIN
--YOur query to retrieve all data
SELECT * FROM table1
where (some conditions)
END;
END
The usage of stored procedure will give you selected data and the query is precompiled in the database so it faster.
Note: I didn't quite understand what your query is returning so replace mine with yours
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
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