Returning something like error-codes from query - sql

I want to check conditions in query. If condition is false, then query must returns error code, else query must to execute another query.
Something like that:
WITH error_code_get AS (
SELECT
CASE
WHEN NOT EXISTS (
SELECT 1
FROM users
WHERE id = '1a4b...'
) THEN 1
WHEN NOT EXISTS (
SELECT 1
FROM workspaces
WHERE id = '353...'
) THEN 2
WHEN (
SELECT settings
FROM workspace_roles
WHERE workspace_id = '353...'
AND id IN (
SELECT role_id
FROM m2m_users_to_workspace_or_projects_roles
WHERE role_type='1'
AND user_id='1a4b...'
)
) < 2 THEN 3
ELSE 0
END error_code
RETURNING error_code
)
// WRONG PART
CASE
WHEN (SELECT error_code FROM error_code_get) = 0 THEN (INSERT INTO x(a) VALUES('some_value'))
ELSE (SELECT error_code FROM error_code_get)
END

You can't do something like that in pure SQL, you'd need to write code in Stored Proc or Function

Related

single query that defines 2 tables has same row

query 1
(select count(*) from CALENDAR)
it returns 15
query 2
(select value from PARAMETER where name = 'PLAN_HORIZON')
it returns 15 too only when my programs runs without error. if error occurs,
it returns 10 or other values.
this↓ is wrong sql, but i want a single query which returns True or False.
select if (query1 == query2)
How can I define 2 sql has same result in a query?
The following SQL statement returns 0 or 1. It runs with SQL Server
SELECT CASE WHEN (select count(*) from CALENDAR) = (select value from PARAMETER where name = 'PLAN_HORIZON') THEN 1 ELSE 0 END
Something like this:
select count(*) = 0
from (
select count(*)
from calendar
except
select value
from parameter
where name = 'PLAN_HORIZON'
) t
You didn't specify your DBMS, but the above is standard SQL.
Try this query !
SELECT
CASE
WHEN (select count(*) from CALENDAR) = (select value from PARAMETER
where name = 'PLAN_HORIZON')
THEN true
ELSE false
END ;

ORACLE: USE RESULT OF CASE-WHEN-STATEMENT

I have a huge query and I am wondering if it is in Oracle possible
to get the result of a case-when-statement and use it for comparison? My CASE-STATEMENT is declared in the Select-Statement and it looks like this.
SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST;
Now I want to get the result of this case-statement and use it in the where part? Is it possible? (Sry this may be a dumb question)
If you define your CASE statement in either an inline-view or a common table expression (aka WITH clause), you can refer to it by whatever alias you give it.
For example (inline-view):
SELECT ...
FROM ( SELECT .....
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM...
) v
WHERE v.test = 'TEST2';
As a common table expression, it would be:
WITH cte AS ( SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM ... )
SELECT ...
FROM cte
WHERE test = 'TEST2';
You can use a case statement in the where clause, for eg.:
select * from table
where table.field = (CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END)
This will compare the value returned from the case statement with the table field.

SQL - Select statements within case

How do I achieve this using SQL case statement?
select count(*) x from t_table where file_id = 310012;
if x<>0 : select distinct status from t_table where file_id = 310012
else : return x
See the code below:
SELECT CASE COUNT(*)
WHEN COUNT(*) > 0 THEN
(SELECT status FROM t_table WHERE file_id = 310012)
ELSE null END AS x
FROM t_table WHERE file_id = 310012;
You can do what you want with a union all:
select distinct status
from t_table
where file_id = 310012
union all
select 0
from dual
where not exists (select 1 from t_table where tile_id = 320023);
However, returning a single row with 0 seems like a bad idea, because it could be confused with a value status.
Note: You should use '0' if status is a string.
You don't need to count records. Just do only one query:
select distinct status from t_table where file_id = 310012
When the query return some rows, then x must be <> 0
If the query returns an empty resultset, then x must be equal to 0, and you get the second part of if x <> 0 then ..... else return x
As a bonus you get a higher processing speed, because you run only one query against the table, not two.

Sql Case Statement Error Cannot select More Than one Column

I need to get the Supplier ID from the SQL Select statement inside Case statement. Once I put the A.SUPPLIER_ID to Select Statement I get an Error. How to do this?
Select
CASE
WHEN TYPE = 1 THEN
(
SELECT A.name
from BIZZXE_V2_SCH.SUPPLIERS A
where A.SUPPLIER_ID = 30
)
ELSE
(
SELECT A.name
from BIZZXE_V2_SCH.STOCK_SUPPLIER A
where A.SUPPLIER_ID = 31
)
END name
from DUAL;
You can't put complete queries into a case statement. But this should work
SELECT name
from BIZZXE_V2_SCH.SUPPLIERS
where SUPPLIER_ID = 30 and TYPE = 1
union all
SELECT name
from BIZZXE_V2_SCH.STOCK_SUPPLIER
where SUPPLIER_ID = 31 and TYPE <> 1
Using IF/ELSE statement
DECLARE #type NUMBER;
SELECT TYPE INTO #type FROM DUAL; -- Make sure it always returns one row
IF #type = 1 THEN
SELECT A.name
FROM BIZZXE_V2_SCH.SUPPLIERS A
WHERE A.SUPPLIER_ID = 30;
ELSE
SELECT A.name
FROM BIZZXE_V2_SCH.STOCK_SUPPLIER A
WHERE A.SUPPLIER_ID = 31
END IF;
You should be able to handle this in your WHERE statement, like this.
SELECT A.name
FROM BIZZXE_V2_SCH.STOCK_SUPPLIER A
WHERE (A.SUPPLIER_ID = 30 AND TYPE = 1) OR
A.SUPPLIER_ID = 31

CASE statement with IN in WHERE clause

I'm trying to create the following WHERE clause:
AND CASE #SomePRarmeter
WHEN 'this' THEN
user_id IN (SELECT * FROM dbo.func_Id1(#User))
WHEN 'that' THEN
user_id IN (SELECT user_id from dbo.func_Ids2(#OrgsForReporter)
END
But I'm getting an error: Incorrect syntax near the keyword 'IN' (in the first condition) , although separately both of those conditions work. What would be the correct way to make such a statement work?
Thanks!
Try
AND (
(#SomePRarmeter = 'this' AND user_id IN (SELECT * FROM dbo.func_Id1(#User)))
OR
(#SomePRarmeter = 'that' AND user_id IN user_id IN (SELECT user_id from dbo.func_Ids2(#OrgsForReporter)))
)
You are doing select * in a subquery. You need to return only one column:
(SELECT * FROM dbo.func_Id1(#User))
to this:
(SELECT YOUR_USER_ID_COLUMN FROM dbo.func_Id1(#User))
A case statement must result in a value, not an expression. So this won't work:
select case when 1=1 then 1 in (1,2,3) end
But this will work;
select case when 1=1 then 1 end
The value can be the result of a subquery. So one solution would be to rewrite the where clause like:
CASE #SomePRarmeter
WHEN 'this' THEN
(SELECT count() FROM dbo.func_Id1(#User) f where f.user_id = t.user_id))
WHEN 'that' THEN
(SELECT count() from dbo.func_Ids2(#OrgsForReporter) f where f.user_id = t.user_id))
END > 1
Now it returns the number of matching rows. You can then filter with case ... end > 1.
I'd break this out:
IF 'this'
SELECT
...
WHERE user_id IN (SELECT * FROM dbo.func_Id1(#User))
ELSE IF 'that'
SELECT
...
WHERE user_id IN (SELECT user_id from dbo.func_Ids2(#OrgsForReporter))
CASE ... END returns an expression, not a piece of literal SQL code. Rather than:
AND CASE foo WHEN bar THEN bla=ble END -- Wrong
... you have to use this:
AND bla = CASE foo WHEN bar THEN ble END -- Right
In your case, you can do something on this line:
-- Untested
AND (
(#SomePRarmeter='this' AND user_id IN (SELECT * FROM dbo.func_Id1(#User)))
OR (#SomePRarmeter='that' AND user_id IN (SELECT user_id from bo.func_Ids2(#OrgsForReporter))
)