why does this inline count() condition return an error? - sql

The following t-sql works as expected:
--test1
DECLARE #IntValCount INT;
SELECT #IntValCount = COUNT(IntVal) FROM #AppIdList;
IF(#IntValCount = 0)
BEGIN
print 'test1'
END
However, if I try to write the count() condition as an inline expression with the following sql then ssms returns the error "Incorrect syntax near '='":
--test2
IF(SELECT COUNT(IntVal) FROM #AppIdList = 0)
BEGIN
print 'test2'
END
Am I writing the sql for test2 incorrectly or dos ss not allow this particular approach for some reason?

Your parens are in the wrong place:
IF ( (SELECT COUNT(IntVal) FROM #AppIdList) = 0)
BEGIN
print 'test2'
END;
However, it is better to use NOT EXISTS:
IF ( NOT EXISTS (SELECT 1 FROM #AppIdList WHERE IntVal IS NOT NULL) )
BEGIN
print 'test2'
END;
Why? NOT EXISTS can stop at the first matching value. SELECT COUNT() has to read all the potential values.

Related

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

SQL Server - Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. ErrorCode HY000

I'm trying to execute the query bellow on a SQL server database
but it throws the following error:
Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS.
Code:
IF #status = 1
BEGIN
..... -- execute something
END
ELSE
BEGIN
select top #URL.nof# *
from lead_feed
where
lead_tstamp > '2017-09-01'
and egoiID is not null
and rtrim(ltrim(lead_name)) <> ''
and rtrim(ltrim(lead_surname)) <> ''
and rtrim(ltrim(lead_gender)) <> ''
and rtrim(ltrim(lead_interest)) <> ''
and lead_country in (select campaign_country
from client_campaigns
where campaignID = '#URL.ccampaignID#')
and leadID not in (select leadID
from lead_integration
where campaignID = '#URL.ccampaignID#')
order by
lead_tstamp desc
END
It points to the leadID not in ( .... part, if I delete this part it points the same error to and lead_country in ( ... part.
Can you please explain me whats happening?
Thanks.

SQL IF AND ELSE STATEMENT

I have been trying to execute this if and else statement. However every execution indicates error such as either functional error or syntax related errors.
CREATE trigger [dbo].[TRIAL]
on [dbo].[TBL1]
after INSERT
AS
BEGIN
SET NOCOUNT ON;
IF TBL1.NUMBER = TBL2.NUMBER THEN
insert into TBL3 (NAME,HEIGHT)
select NAME,HEIGHT
from TBL1,TBL2
ELSE
PRINT 'WRONG NUMBER'
end
Would you please be able to help me to correct this?
To expand a bit on Alex K's comment:
declare #Flag bit = 1;
-- ERROR: There is no THEN keyword.
if #Flag = 1 then
select 'A';
-- CORRECT: Omit THEN and this works as expected.
if #Flag = 1
select 'A';
-- ERROR: Only the first SELECT is associated with the IF, so the ELSE is unmatched.
if #Flag = 2
select 'B1';
select 'B2';
else
select 'C';
-- CORRECT: If each branch of the IF has only one statement, then this construction is okay.
if #Flag = 2
select 'B1';
else
select 'C';
-- CORRECT: If you want multiple statements in either branch of the IF, make them into a block using BEGIN/END.
if #Flag = 2
begin
select 'B1';
select 'B2';
end
else
select 'C';
-- CORRECT: You can also use BEGIN/END with single statements if you like.
if #Flag = 2
begin
select 'B1';
select 'B2';
end
else
begin
select 'C';
end

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 ...

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

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.