SQL Replace an empty SQL SELECT with word else post the result in Oracle - sql

I'm trying to solve the following problem:
I would like to make a select, when the result is empty it should be replaced with 'empty' Else all lines of the result should be there.
Like:
if (select has no result)
{
One Line as result with 'no result'
}
else
{
post every line of the select result
}
That is my try:
select case when count(*) = 0
then 'no Entry'
else Members --all Members should be here
END as Member
from tableMember where Membergroup = 'testgroup';
Thanks to everybody who can help me!
PLEASE show me the COMPLETE code that is needed to use the query in Oracle

Try this:
DECLARE #counter int
SET #counter = (select count(*) from tableMember where Membergroup = 'testgroup');
IF (#counter > 0)
BEGIN
SELECT * FROM Members
END
ELSE
BEGIN
SELECT 'No results!'
END
Regards

FOR ORACLE
DECLARE C INTEGER;
SELECT COUNT(*) INTO C FROM tableMember WHERE Membergroup = 'testgroup';
IF C > 0
THEN
SELECT * FROM tableMember;
ELSE
SELECT 'No results!' FROM tableMember;
END IF;
MS-SQL ONLY
IF EXISTS(SELECT * FROM tableMember WHERE Membergroup = 'testgroup')
BEGIN
SELECT * FROM tableMember
END
ELSE
BEGIN
SELECT 'No results!'
END

I would rather do this in the application code rather than stored procedures. What you are trying to do here is user experience related work which IMHO is something database should not care about.

Related

Multiple conditions without using multiple if else in SQL

I have to write a program in SQL that has multiple conditions but I shouldn't write it with multiple if else (because of the cost that if has). I'm new in this field and I have no idea how else could I write it which would be better
here is my sample code:
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= 'dbo' AND TABLE_NAME = 'Food_tbl')
BEGIN
IF NOT EXISTS (SELECT TOP 1 FID FROM dbo.Food_tbl)
BEGIN
INSERT INTO dbo.Food_tbl
SELECT *
FROM DataFoodView
END
ELSE IF (SELECT COUNT(*) FROM dbo.Food_tbl)=20
BEGIN
PRINT N'Table Exists'
SELECT *
FROM Food_tbl
END
ELSE IF (SELECT COUNT(*) FROM dbo.FoodSara_tbl)<>20
BEGIN
print N'there isnt 20'
INSERT INTO Food_tbl (Food_tbl.FID, Food_tbl.Fname, Food_tbl.Ftype, Food_tbl.Fcount, Food_tbl.Datetype, Food_tbl.Fdescription)
SELECT DataFoodView.*
FROM DataFoodView
LEFT JOIN FoodSara_tbl ON Food_tbl.FID = DataFoodView.FID
WHERE Food_tbl.FID IS NULL;
END
END
PS: I have at first check if the table is exits and if it hasn't any record insert all the data, if it has 20 records show the table, if the table doesn't have 20 records find the missing data then insert that.
First, the condition in the ELSE part isn't needed :
change
ELSE IF (SELECT COUNT(*) FROM dbo.FoodSara_tbl)<>20
BEGIN
to
ELSE
BEGIN
Next, you may regroup the two INSERT parts because having no element is also having else than 20 elements.
The result will be something like that :
IF EXISTS
(
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= 'dbo' AND TABLE_NAME = 'Food_tbl'
)
BEGIN
IF (SELECT COUNT(*) FROM dbo.Food_tbl)=20
BEGIN
PRINT N'Table Exists'
SELECT *
FROM Food_tbl
END
ELSE
BEGIN
print N'there isnt 20'
INSERT INTO Food_tbl (Food_tbl.FID, Food_tbl.Fname, Food_tbl.Ftype,
Food_tbl.Fcount, Food_tbl.Datetype, Food_tbl.Fdescription)
SELECT DataFoodView.*
FROM DataFoodView
LEFT JOIN FoodSara_tbl ON Food_tbl.FID = DataFoodView.FID
WHERE Food_tbl.FID IS NULL;
END
END
Not understood the exact requirement from the description what you have provided but just review the following logic
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= 'dbo' AND TABLE_NAME = 'Food_tbl')
BEGIN
DECLARE #RCount INT
SELECT #RCount= COUNT(FID) FROM dbo.Food_tbl
IF #RCount=0
BEGIN
//Your Logic 1 (for inserting data since no records found)
END
ELSE
BEGIN
PRINT N'Table Exists'
IF #RCount=20
BEGIN
//Your Logic 2
END
ELSE
BEGIN
//Your Logic 3
END
END
END
Please explain the requirement in details or build your queries based on the requirement and add in place of Logic 1,2,3

ELSE SELECT nothing/blank

I need to have an 'ELSE' statement for every conditional statement in my stored procedures, how do I return make a procedure return nothing?
IF EXISTS(SELECT 1 FROM Account WHERE Account.username=#Username)
BEGIN
SELECT Status
FROM Account
WHERE Account.username=#Username
END
ELSE
BEGIN
SELECT --nothing/blank
END
Thanks!
You can assign a variable:
IF EXISTS(SELECT 1 FROM Account WHERE Account.username=#Username)
BEGIN
SELECT Status
FROM Account
WHERE Account.username = #Username;
END;
ELSE
BEGIN
DECLARE #X int;
SELECT #X = 0;
END;
Something like this:
IF EXISTS(SELECT 1 FROM Account WHERE Account.username=#Username)
BEGIN
SELECT Status
FROM Account
WHERE Account.username=#Username
END
ELSE
BEGIN
SELECT "No rows found" as Status
END
IF EXISTS(SELECT 1 FROM Account WHERE Account.username=#Username)
BEGIN
SELECT Status
FROM Account
WHERE Account.username=#Username
END
ELSE
BEGIN
SELECT NULL--nothing/blank
END
Try this query
IF EXISTS(SELECT 1 FROM Account WHERE Account.username=#Username)
BEGIN
SELECT Status
FROM Account
WHERE Account.username=#Username
END
ELSE
BEGIN
SELECT NULL as 'Status'--nothing/blank
END
If you want to retrieve the Status value or NULL if there is no matching row you can use a subquery:
select ( select Status from Account where username = #Username ) as Status;
The outer select will always return a result. If the subquery doesn't supply a value then the result will be NULL.
If you want different, i.e. non-NULL, default value you can use Coalesce and a subquery thusly:
select Coalesce(
( select Status from Account where username = #Username ), -- If found.
0 -- Default value if not found.
) as Status;
NB: It is assumed that there is either a single matching row or no matching row. The code will fail if the subquery returns more than one value.
Note that by querying the table only once either solution avoids the possibility of a race condition with another session updating or deleting the row for a user between the exists check and the select that returns the Status value.
if you want to return nothing then no need to write else part.
It will shorten your code and easy to understand.
IF EXISTS(SELECT 1 FROM Account WHERE Account.username=#Username)
BEGIN
SELECT Status
FROM Account
WHERE Account.username=#Username
END
Else part is required when you want to return something else above condition failed.

SQL IF/ELSE Statement

I'm struggling to understand why the below SQL will not work. I've put a comment (----------Fine up to Here), which is where SQL Server will accept the code when I parse/save the Store proceedure.
The bit below that, it will not take. Still new to SQL so any help would be great.
The error I receive is, Incorrect syntax near the keyword 'ELSE'. The "ELSE" being the one under the comment I mentioned above.
What I also don't understand is, If I change the IF and the BEGIN round, SQL accepts it (below)? I thought ELSE IF was not possible.
----------Fine up to Here
ELSE
IF (#GrabTypeID = '')
BEGIN
****************************************
Code below
**************************************
IF (#GrabtypeID NOT IN (SELECT GRABTYPEID FROM Mytable) AND #GrabtypeID != '') OR
(#Variable1 NOT IN (SELECT ID FROM Mytable) AND #Variable1 !='')
BEGIN
SELECT #ErrorMessage ='The GrabTypeID or the ID is an invalid value'
RAISERROR (#ErrorMessage, 16, 1)
PRINT 'Invalid Parameters passed Through'
RETURN
END
ELSE
BEGIN
IF (#GrabtypeID ! ='')
TRUNCATE TABLE Datatable1
TRUNCATE TABLE Datatable2
INSERT Datatable1
SELECT * FROM Referencetable1
INSERT Datatable2
SELECT * FROM REFERENCETABLE2
END
----------Fine up to Here
ELSE
BEGIN
IF (#GrabTypeID = '')
TRUNCATE TABLE Datatable1
TRUNCATE TABLE Datatable2
INSERT Datatable1
SELECT * FROM REFERENCETABLE1 WHERE CATEGORY = 4
INSERT Datatable2
SELECT * FROM REFERENCETABLE2 WHERE CATEGORY = 4
END
GO
Your format is a little weird. You could make it work the way you have it, but I think it would be better to use this format:
IF expression
BEGIN
SELECT
END
ELSE IF expression
BEGIN
SELECT
END
ELSE IF expression
BEGIN
SELECT
END
Specifically, change this:
ELSE
BEGIN
IF (#GrabtypeID ! ='')
To this (in both places):
ELSE IF (#GrabtypeID ! ='')
BEGIN

Querying different table based on a parameter

I have a stored procedure that I would like to query either the production or the "work in progress" table, based on the parameter I am passing in. I could write two separate stored procedures, but I thought this was worth a try.
something along the lines of:
create procedure getUserDetails
#userID int,
#prod varchar(5)
as
begin
select * from
if (#prod = 'true')
Begin
userprod_table
else
userwip_table
end
where something = 'something'
END
Is this at all possible? I wouldn't want to write 2 SP that are almost identical :-/
Why not use a simple if(#prod = 'true') statement like below:
if (#prod = 'true')
begin
select * from userprod_table where something = 'something'
end else
begin
select * from userwip_table where something = 'something'
end
You could use a CTE so that your main query isn't repeated
with usertable as
(
select * from userprod_table where 1 = #flag
union
select * from userwip_table where 0 = #flag
)
select ... from usertable ...

Can i have multiple select but only return one result set

If I have multiple selects like so:
select * from A where A.name = 'linköping'
IF ##ROWCOUNT = 0
begin
select * from A where A.amount = 45
end
...I get 1 result set if the first select returns stuff. But if it runs the second, I get two result sets; the first with no rows and the second with some rows.
Is there a way to only return the second result set if the second select is run ?
I write code like this because of Andrey Gordeev's answer to this post: Can you have if-then-else logic in SQL?
(MSSQL 2000)
Thanks!
You will need to prevent the first select by checking if you will get any results back before running the select.
For example:
IF EXISTS (SELECT 1 FROM A WHERE A.name = 'linköping')
BEGIN
SELECT * FROM A WHERE A.name = 'linköping'
END
ELSE
BEGIN
SELECT * FROM A WHERE A.amount = 45
END
No it is not possible The returning results are depending on number of select statements.
If you want to do the single select follow #RB or #Jwalin Shah solution.
This will work with IF-ELSE Logic. [Tested]
DECLARE #count as int
set #count = (SELECT count(1) from A where A.name = 'linköping')
if (#count = 0)
BEGIN
select * from A where A.amount = 45
END
ELSE
BEGIN
select * from A where A.name = 'linköping'
END
Try this
IF ##ROWCOUNT = 0
BEGIN
select * from A where A.amount = 45
END
ELSE
BEGIN
select * from A where A.name = 'linköping'
END