sub query with parameter? - sql

I'd like to ask help on this small query of mine. Im doing a query and a sub query on my sub query i want to have it parameterized. Is there a way to do it?
Please see my query script.
select sum(issue) as [Issue], sum(nonissue) as [NonIssue]
from
(
AS
select
case when isissue = 1 then 1 else 0 end as 'issue',
case when isissue = 0 then 1 else 0 end as 'nonissue',
LastTicketStatusID
from
vw_Tickets
where
LastTicketStatusID = #LastTicketStatusID
)
as Tickets
I always got an error Must declare the table variable "#LastTicketStatusID". where should i declare the parameter?
Thanks,
Nhoyti

If this is for a stored procedure...
CREATE PROCEDURE [dbo].[ProcedureName]
#LastTicketStatusID INT
AS
select
sum(issue) as [Issue],
sum(nonissue) as [NonIssue]
from (
select
case when isissue = 1
then 1 else 0 end as 'issue',
case when isissue = 0
then 1 else 0 end as 'nonissue',
LastTicketStatusID
from vw_Tickets
where LastTicketStatusID = #LastTicketStatusID ) as Tickets
Otherwise
DECLARE #LastTicketStatusID INT
SELECT #LastTicketStatusID = yourDesiredID

Not pertinant to your question, but assuming that vw_Tickets.isissue is a bit field (or otherwise constrained to values of zero or one.) the inline query can be removed to simplify Launchy's answer:
select sum(isissue) as [Issue],
sum(1 - isissue) as [NonIssue]
from vw_Tickets
where LastTicketStatusID = #LastTicketStatusID

at the very top of the query
Declare #LastTicketStatusID int
set #lastTicketStatusID = ####

Related

input parameter used in case statement in where clause

I have the following simplified stored procedure where based on on the input parameter, I need to then do a case in the where clause. It will not execute as it says: Incorrect syntax near '='
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
Select * from tbl1 1
where (CASE #Allowactive
WHEN 0 then (t.Isactive = 1) END
AND isSubmitted = 1
END
your where clause will be like below
where
CASE #Allowactive
WHEN 0 then t.Isactive END =1
AND isSubmitted = 1
You shouldn't use parameters in a query like this, as it messes up the query plan. When the right plan to use changes depending on the parameter, you need separate queries, or to force SQL to always recompile.
So do this instead:
create or alter procedure [dbo].[DataInfo] #Allowactive bit = 1
as
begin
if #Allowactive = 0
begin
Select * from tbl1 1
where Isactive = 1
AND isSubmitted = 1
end
else
begin
select * from tbl1 1
where isSubmitted = 1
end
end
Instead run separate queries.
Try to run the following and see the results:
SELECT *
FROM Tbl1 AS T
WHERE CASE #Allowactive
WHEN 0 THEN 1 ELSE #Allowactive END = T.Isactive
AND
isSubmitted = 1;
If you have 2012+ version then you could also do:
SELECT *
FROM Tbl1 AS T
WHERE IIF(#Allowactive = 0, 1, #Allowactive) = T.Isactive;
It seems Zaynul Abadin Tuhin directly answers your question.
But, I believe a case statement complicates what you want to achieve.
I think a query like this satisfies your desired outcome:
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
SELECT * FROM tbl1 t
WHERE (#Allowactive = 1 OR (#Allowactive = 0 AND t.Isactive = 1))
AND t.isSubmitted = 1
END

how to use declared variable to select data from another table in case when condition?

I made select query in which i want to select data based on condition.For this i declared one variable and set value of that variable in else part.I want to use that variable for further select in same else part how can i achieve this?Please Help
declare #stateid int
select CASE WHEN MstCustomerAddressInfo.StateId is null
THEN 24
ELSE set #stateid = MstCustomerAddressInfo.StateId
select mststate.statecode from mststate where MstState.StateId = #stateid
END AS StateCode
No, you can't have SET inside a CASE expression. Even you can't have multiple statements.
Same query you can write as following.
declare #stateid int
select CASE
WHEN MstCustomerAddressInfo.StateId is null THEN 24
ELSE
-- set #stateid = MstCustomerAddressInfo.StateId
(select mststate.statecode
from mststate
where MstState.StateId = MstCustomerAddressInfo.StateId)
END AS StateCode
from [Your_Table]

How do I use an if statement inside the where clause in this sql statement?

select *
from mytable
where (if #key=0
pkey>=0
else
pkey = #key)
#key is the value passed in to the stored procedure and pkey is a column in mytable.
How about this:
select *
from mytable
where ((#key=0 AND pkey>=0) OR (#key<>0 AND pkey = #key))
You use CASE (bit like the way you are trying with if) as below. (DEMO)
select *
from mytable
where pkey = case when #key <> 0 then #key
else Abs(pkey) end
This will work for you . by using case statement you can apply dynamic filter ..
select *
from mytable
where 1 = case
when #key=0 then case when pkey>=0 then 1 else 0 end
else case when pkey = #key then 1 else 0 end
end

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.

If..Else or Case in WHERE Clause - something different

I've read loads of q&a's on here but none fit the bill for what I'm trying to achieve.
I don't even know if this is possible! Any help/suggestions would be greatly appreciated.
I'm trying to build a conditional WHERE clause based on
CustID (int) = #CustomerID
If #CustomerID = 0 then I want the result to be WHERE CustID > 0 (i.e. return all customers) otherwise I only want certain customers CustID = #CustomerID
Is this possible?
Just do
WHERE (#CustomerID = 0 OR CustID = #CustomerID)
something like this?
SELECT *
FROM YOURTABLE
WHERE
( CASE
WHEN #CustomerID = 0 THEN CustID
ELSE 1
END
) > 0
AND
( CASE
WHEN #CustomerID <> 0 THEN #CustomerID
ELSE CUSTID
END
) = CUSTID
The most common way of simulating an IF-ELSE statement in sql are CASE and DECODE (in oracle at least):
Ex pseudocode:
if (x == 1) then y; else if (x == 2) then z; end if
CASE:
select
case x
when 1 then y
when 2 then z
end example
from dual;
DECODE:
select decode(x,1,y,2,z) example from dual;