How do I add errror handling into this stored procedure? - sql-server

How can I print an error message from this procedure if the employee (server) hasn't served anyone? Are try - catch blocks the only way to handle this?
I was thinking that if/else condition test followed by Print message suits my requirement.
Stored procedure:
if OBJECT_ID('customers_served', 'u') is not null
drop procedure customers_served;
go
create procedure customers_served
#employee_id int
as
set nocount on;
select
c.customer_id, c.cust_lastname,
(sum(c.cust_total_guests)) as total_guests
from
customers c
join
seating s on c.customer_id = s.customer_id
join
table_assignment ta on s.table_id = ta.table_id
join
employees e on ta.employee_id = e.employee_id
where
#employee_id = e.employee_id
group by
c.customer_id, c.cust_lastname;
/* if total_guests = 0 print message the employee has served 0 guests */
Test procedure:
exec customers_served
#employee_id = 5;

I modified your script to this.
use dbServers;
if OBJECT_ID('customers_served', 'u') is not null
drop procedure customers_served;
go
create procedure customers_served
#employee_id int
as
set nocount on;
declare #totalGuests int;
set #totalGuests = (
select(sum(c.cust_total_guests))
from customers c
join seating s on c.customer_id = s.customer_id
join table_assignment ta on s.table_id = ta.table_id
join employees e on ta.employee_id = e.employee_id
where #employee_id = e.employee_id
)
if #totalGuests = 0 OR #totalGuests IS NULL
BEGIN
print 'This server did not serve any guests'
END
else
BEGIN
select #totalGuests AS 'total_quests'
END
/* test procedure*/
exec customers_served
#employee_id = 5;

Following snippet of code might help:
declare #r int
select #r = (sum(c.cust_total_guests)) as total_guests
from customers c
join seating s on c.customer_id = s.customer_id
join table_assignment ta on s.table_id = ta.table_id
join employees e on ta.employee_id = e.employee_id
where #employee_id = e.employee_id
group by c.customer_id, c.cust_lastname;
if #r = 0
begin
-- do what ever you wish
end
else
begin
select c.customer_id, c.cust_lastname, (sum(c.cust_total_guests)) as
total_guests
from customers c
join seating s on c.customer_id = s.customer_id
join table_assignment ta on s.table_id = ta.table_id
join employees e on ta.employee_id = e.employee_id
where #employee_id = e.employee_id
group by c.customer_id, c.cust_lastname;
end
end

Rather than double querying, you can simply test ##ROWCOUNT after your query to determine if any results were returned, and print your message if ##ROWCOUNT = 0.

Related

Stored procedure takes more than 30 seconds to execute

I am optimizing the stored procedure in which I get list of more than 10K user ids in one table variable as a parameter. I am setting flag #ContainsUserIds if there is any data into the table variable. In main query selecting the all the users that are present in table variable or all the users if no data found in table variable (There are more condition in where clause).
The problem is here that the statement in where clause takes more than 30 second which has OR condition. If I removed the first condition to check #ContainsUserIds = 0 then it will execute within a second. Can someone please help me to optimize this query.
This Stored procedure is called from different position so the User ids may not pass.
CREATE PROCEDURE [core].[spGetUsersByFilter]
(
#ClientId nvarchar(38) = NULL,
#UserIds [UserIdList] readonly
)
AS
BEGIN
SET NOCOUNT ON
DECLARE #ContainsUserIds BIT = 0,
SET #ContainsUserIds = CASE
WHEN EXISTS(SELECT TOP 1 1 FROM #UserIds)
THEN 1
ELSE 0
END;
SELECT DISTINCT ES.Id
FROM [db].[Users] E
JOIN [db].[UserDetails] ES ON ES.UserId = E.Id
WHERE E.[Active] = 1
AND (#ContainsUserIds = 0 OR E.UserId IN(SELECT Item FROM #UserIds))
AND ES.ClientId = #ClientId
END
SQL notoriously dislikes OR, the optimizer sometimes works around it but in most cases it's a good idea to convert things to a UNION [ALL] syntax. If there's just 1 OR that's often easy to do, if there are multiple things explode fast.
Anyway, you could thus convert your stored procedure to:
CREATE PROCEDURE [core].[spGetUsersByFilter]
(
#ClientId nvarchar(38) = NULL,
#UserIds [UserIdList] readonly
)
AS
BEGIN
SET NOCOUNT ON
DECLARE #ContainsUserIds BIT = 0,
SET #ContainsUserIds = CASE
WHEN EXISTS(SELECT * FROM #UserIds)
THEN 1
ELSE 0
END;
SELECT DISTINCT ES.Id
FROM [db].[Users] E
JOIN [db].[UserDetails] ES ON ES.UserId = E.Id
WHERE E.[Active] = 1
AND (#ContainsUserIds = 0)
AND ES.ClientId = #ClientId
UNION ALL
SELECT DISTINCT ES.Id
FROM [db].[Users] E
JOIN [db].[UserDetails] ES ON ES.UserId = E.Id
WHERE E.[Active] = 1
AND (#ContainsUserIds = 1)
AND E.UserId IN (SELECT Item FROM #UserIds))
AND ES.ClientId = #ClientId
END
That said, you might just as well split things up here and do 2 separate SELECTs. Also, if the table-variable contains more than a couple of records you may prefer to use a temp-table as the latter allows for indexing and most importantly has statistics handling which WILL result in a better execution plan. I also prefer JOIN over IN (), just make sure there are no doubled values when JOINing.
CREATE PROCEDURE [core].[spGetUsersByFilter]
(
#ClientId nvarchar(38) = NULL,
#UserIds [UserIdList] readonly
)
AS
BEGIN
SET NOCOUNT ON
SELECT DISTINCT Item
INTO #UserIds
FROM #UserIds
IF ##ROWCOUNT = 0
BEGIN
SELECT DISTINCT ES.Id
FROM [db].[Users] E
JOIN [db].[UserDetails] ES ON ES.UserId = E.Id
WHERE E.[Active] = 1
AND ES.ClientId = #ClientId;
END
ELSE
BEGIN
CREATE UNIQUE INDEX uq0_UserIds ON #UserIds ( Item ) WITH (FILLFACTOR = 100);
SELECT DISTINCT ES.Id
FROM [db].[Users] E
JOIN [db].[UserDetails] ES ON ES.UserId = E.Id
JOIN #UserIds T ON T.Item = E.UserId
WHERE E.[Active] = 1
AND ES.ClientId = #ClientId
END
END
PS: all above written in notepad and untested, some assembly may be required =)

data not Showing when execute the stored procedure in Sql

I write this code in my stored procedure:
ALTER PROC sp_ShowComplaintStatus (#pid bigint,
#compid int)
AS
BEGIN
SET NOCOUNT ON;
SELECT PId,
C.Compld,
CC.ComplCategory,
CSC.ComplSubCategName,
S.Status
FROM TN_Complaint C
INNER JOIN TN_ComplStatus CS ON C.Compld = CS.CompId
INNER JOIN Z_Status S ON CS.StatusId = S.StatusId
INNER JOIN Z_ComplCategory CC ON C.CompCategId = CC.ComplCategoryId
INNER JOIN Z_ComplSubCategory CSC ON C.ComplSubCategoryId = CSC.ComplSubCategoryId
WHERE PId = #pid
AND C.Compld = #compid;
END;
sp_ShowComplaintStatus 10001, 1000;
But when I execute my stored procedure, it doesn't show any records. It only show column name.

Stored procedure always returns same query

I tried to write a SQL query to get results with some parameters, but when I send parameters, it doesn't work. It always returns the last query in if else condition statement.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_RealEstatesList]
#TransCode NVARCHAR(5),
#CatUrl NVARCHAR(255) = NULL,
#ForSaleItem BIT = NULL,
#NewItem BIT = NULL,
#ItemOfDay BIT = NULL,
#Random BIT = NULL,
#Top INT = NULL
AS
SET NOCOUNT ON
SET XACT_ABORT ON
SET FMTONLY OFF
BEGIN TRAN
IF (OBJECT_ID('tempdb..#TempTable') IS NOT NULL)
BEGIN
DROP TABLE #TempTable
END
SELECT
R.[ID],
RT.[Title],
R.[Price],
R.[NewItem],
R.[ForSaleItem],
R.[ItemOfDay],
R.[Url],
(SELECT TOP 1 CC.Url
FROM [dbo].[Category] CC
JOIN [dbo].[LinkTypes] LLT ON LLT.[MainID] = CC.ID
JOIN [dbo].[Links] LL ON LL.[LinkTypeID] = LLT.[ID]
JOIN [dbo].[RealEstates] RR ON RR.[ID] = LL.LinkID
WHERE LLT.[LinkedTypeID] = 17
AND LLT.[MainTypeID] = 1
AND RR.ID = R.[ID]) AS 'CatUrl',
(SELECT TOP 1
(SELECT TOP 1 XC.Url
FROM Category XC
JOIN [dbo].[LinkTypes] LLT ON LLT.[MainID] = XC.ID
JOIN [dbo].[Links] LL ON LL.[LinkTypeID] = LLT.[ID]
WHERE LLT.[LinkedTypeID] = 1
AND LLT.[MainTypeID] = 1
AND LL.LinkID = CC.ID)
FROM [dbo].[Category] CC
JOIN [dbo].[LinkTypes] LLT ON LLT.[MainID] = CC.ID
JOIN [dbo].[Links] LL ON LL.[LinkTypeID] = LLT.[ID]
JOIN [dbo].[RealEstates] RR ON RR.[ID] = LL.LinkID
WHERE LLT.[LinkedTypeID] = 17
AND LLT.[MainTypeID] = 1
AND RR.ID = R.[ID]) AS 'PCatUrl'
INTO
#TempTable
FROM
[dbo].[RealEstates] R
JOIN
[dbo].[RealEstatesT] RT ON R.[ID] = RT.[RealEsID]
JOIN
[dbo].[LinkTypes] LT ON LT.[MainTypeID] = 17
JOIN
[dbo].[Links] L ON L.[LinkTypeID] = LT.[ID]
JOIN
[dbo].[Translation] TR ON TR.[ID] = RT.[TransID]
WHERE
R.[Active] = 1
AND R.[Deleted] = 0
AND RT.[Deleted] = 0
AND LT.[LinkedTypeID] = 5
AND LT.[MainID] = R.[ID]
AND TR.[ShortName] = #TransCode
GROUP BY
R.[ID], RT.[Title], R.[Price], R.[NewItem], R.[ForSaleItem], R.[ItemOfDay], R.[Url]
if (#Top = null or #Top = 0)
begin
set #Top = 100000;
end
if(#NewItem != null)
begin
select TOP (#Top) [ID],[Title],[Price],[NewItem],[ForSaleItem],[CatUrl],[PCatUrl],[ItemOfDay],[Url]
from #TempTable
where NewItem = #NewItem
order by case when #Random = 1 then NEWID() end
end
else if(#ForSaleItem != null)
begin
select TOP (#Top) [ID],[Title],[Price],[NewItem],[ForSaleItem],[CatUrl],[PCatUrl],[ItemOfDay],[Url]
from #TempTable
where ForSaleItem = #ForSaleItem
order by case when #Random = 1 then NEWID() end
end
else if(#CatUrl != null)
begin
select TOP (#Top) [ID],[Title],[Price],[NewItem],[ForSaleItem],[CatUrl],[PCatUrl],[ItemOfDay],[Url]
from #TempTable
where CatUrl = #CatUrl or PCatUrl = #CatUrl
order by case when #Random = 1 then NEWID() end
end
else if(#ItemOfDay != null)
begin
select TOP (#Top) [ID],[Title],[Price],[NewItem],[ForSaleItem],[CatUrl],[PCatUrl],[ItemOfDay],[Url]
from #TempTable
where ItemOfDay = #ItemOfDay
order by case when #Random = 1 then NEWID() end
end
else
begin
select TOP (#Top) [ID],[Title],[Price],[NewItem],[ForSaleItem],[CatUrl],[PCatUrl],[ItemOfDay],[Url]
from #TempTable
order by case when #Random = 1 then NEWID() end
end
COMMIT
Transcode and Random parameters work well but CatUrl, ForSaleItem, NewItem, ItemOfDay parameters does not work. It always returns the last query in if else condition statement.
Hope you can help. I'm waiting for your answers.
edit :
I changed all my null checks like "NULLIF(#Top, '') IS NULL" and now if statements works well.

How to optimise this stored procedure?

I have the stored procedure below and I am having two issues with it
It is running very slowly, and
It is returning a blank result set
The idea for the stored procedure is to do a sequence check to find if any account numbers have not been assigned.
Step 1 loops through all the branches and builds up the tempdetails table.
After that it creates the list of all the numbers that have been used and uses that to delete everything that exists to leave a list of those accountnumbers that do not exist,
But een though I know there are missing account numbers, aside from the exceptionally long tun time it is returning a blank result set.
Anyone have any ideas what is going wrong with it?
Thanks
ALTER PROCEDURE [dbo].[PracticeFindMissingSequenceDetail]
#pracId VARCHAR(128),
#Prefix VARCHAR(256)
AS
BEGIN
DECLARE #TempDetails TABLE (SequenceCheck VARCHAR(24),
Prefix VARCHAR(4),
BranchName VARCHAR(256),
RisStatus VARCHAR(256),
Rislink VARCHAR(256)
);
DECLARE #Branchlist TABLE (BranchId INTEGER,
BranchName VARCHAR(256),
BranchPrefix VARCHAR(4),
PrefixLength INT,
SequenceLength INT
);
DECLARE #TempPatNo TABLE (Patno VARCHAR(24));
DECLARE #BranchName VARCHAR(256),
#BranchPrefix VARCHAR(256),
#PrefixLength INT,
#BranchId INT,
#SequenceLength INT,
#rangestart INTEGER,
#rangeend INTEGER,
#rangenow INTEGER,
#startDate DATETIME,
#Patno VARCHAR(128),
#FormatZeroes VARCHAR(3),
#CurrentLength INT,
#RangeString VARCHAR(256);
INSERT INTO #Branchlist (BranchId, BranchName, BranchPrefix, PrefixLength, SequenceLength)
SELECT
b.id, b.name, b.prefix, PrefixLength, SequenceLength
FROM
Branch b
INNER JOIN
Practice pr ON pr.id = b.practiceid
INNER JOIN
[Sequence] s ON s.id = b.id
WHERE
pr.APIKey = #pracID
AND b.inactive = 0
AND b.prefix = #Prefix
/* insert values for each branch into table*/
DECLARE BranchPointer CURSOR FOR
SELECT BranchID FROM #Branchlist
OPEN BranchPointer
FETCH NEXT FROM BranchPointer INTO #BranchId
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #BranchPrefix = (SELECT BranchPrefix
FROM #Branchlist
WHERE BranchId = #BranchId)
SELECT #PrefixLength = (SELECT PrefixLength
FROM #Branchlist
WHERE BranchId = #BranchId)
SELECT #SequenceLength = (SELEct SequenceLength
FROM #Branchlist
WHERE BranchId = #BranchId)
/* Set the starting date from the sequence */
SELECT #startDate = (SELECT MIN(MinimumSequenceDate)
FROM [Sequence] s
WHERE s.id = #BranchId)
/*get the earliest number in the sequence from the startdate*/
SELECT #rangestart = (SELECT MIN(SUBSTRING(v.bookingnumber, 3, LEN(bookingnumber)))
FROM Visit v
INNER join Branch b ON b.id = v.branchid
INNER join Practice pr ON pr.id = b.practiceid
WHERE pr.APIKey = #pracId
AND LEFT(v.bookingnumber, 2) = #Prefix
AND v.date >= #startDate
AND v.branchid = #BranchId);
/*get the latest number in the sequence from the startdate*/
SELECT #rangeend = (SELECT MAX(SUBSTRING(v.bookingnumber, 3, LEN(bookingnumber)))
FROM Visit v
INNER JOIN Branch b ON b.id = v.branchid
INNER JOIN Practice pr ON pr.id = b.practiceid
WHERE pr.APIKey = #pracId
AND LEFT(v.bookingnumber, 2) = #Prefix
AND v.date >= #startDate
AND v.branchid = #BranchId);
SET #RangeNow = #rangestart
WHILE #rangenow < #rangeend
BEGIN
/*check if leading zeroes are needed in the number and add them if needed*/
SET #RangeString = CAST(#RangeNow AS VARCHAR(256))
SET #CurrentLength = LEN(#rangenow)
IF #prefixlength + #currentlength < #SequenceLength
WHILE #CurrentLength + #PrefixLength < #SequenceLength
BEGIN
SET #RangeString = '0' + #RangeString;
SET #currentlength = LEN(#RangeString);
END;
/*Insert full sequence into temporary table*/
INSERT INTO #TempDetails (SequenceCheck, Prefix, BranchName)
SELECT #Prefix + #RangeString, #Prefix, #BranchName
SET #rangenow =#rangenow+1
END;
FETCH NEXT FROM BranchPointer INTO #BranchName
END
CLOSE BranchPointer
DEALLOCATE BranchPointer
/*delete existing sequence numbers from table*/
INSERT INTO #TempPatNo (PatNo)
SELECT BookingNumber
FROM Visit v1
INNER JOIN Branch b1 ON b1.id = v1.branchid
INNER JOIN Practice pr1 ON pr1.id = b1.practiceid
WHERE pr1.APIKey = #pracId
DELETE #TempDetails
WHERE sequencecheck IN (SELECT patNo FROM #TempPatNo)
/*Insert the status and link for error messages*/
UPDATE #tempDetails
SET RisStatus = (SELECT Status
FROM RISErrors r
INNER JOIN Practice pr ON pr.id = r.PracticeId
WHERE pr.APIKey = #pracId
AND VisitNumber = SequenceCheck
AND r.id = (SELECT MAX(r1.id)
FROM RISErrors r1
INNER JOIN Practice pr1 ON pr1.id = r1.PracticeId
WHERE pr1.APIKey = #pracId
AND VisitNumber = SequenceCheck)),
RisLink = 'http://billing.cryanic.co.za/Clinton/RISErrors?searchquery=' + SequenceCheck
/*return missing numbers into sequence control callong procedure*/
SELECT DISTINCT SequenceCheck, RisStatus, Rislink
FROM #TempDetails
END
After reading your procedure, I made some assumptions:
there is a one-to-one relationsship between sequence and branch
the visits are the details of a sequence
the BranchName and BranchPrefix aren't needed.
I recommend to not do lookups by BranchID but instead retrieve the values from the cursor query. The table BranchList isn't needed, you can base the cursor on the query directly. Also, the TempPatNo table can be avoided.
Here's what I have come up with:
ALTER PROCEDURE [dbo].[PracticeFindMissingSequenceDetail]
#pracId VARCHAR(128),
#Prefix VARCHAR(256)
AS
BEGIN
DECLARE #TempDetails TABLE (
BranchID INT,
SequenceCheck VARCHAR(24),
RisStatus VARCHAR(256),
Rislink VARCHAR(256)
);
DECLARE
#PrefixLength INT,
#BranchId INT,
#SequenceLength INT,
#rangestart INTEGER,
#rangeend INTEGER,
#rangenow INTEGER,
#startDate DATETIME;
/* insert values for each branch into table*/
DECLARE BranchPointer CURSOR FOR
SELECT b.id, PrefixLength, SequenceLength, s.MinimumSequenceDate
FROM Branch b
INNER JOIN Practice pr ON pr.id = b.practiceid
INNER JOIN [Sequence] s ON s.id = b.id
WHERE pr.APIKey = #pracID
AND b.prefix = #Prefix
AND b.inactive = 0
OPEN BranchPointer
FETCH NEXT FROM BranchPointer INTO #BranchId, #PrefixLength, #SequenceLength, #startDate
WHILE ##FETCH_STATUS = 0
BEGIN
/*get the earliest and latest number in the sequence from the startdate*/
SELECT
#rangestart = MIN(SUBSTRING(v.bookingnumber, 3, LEN(v.bookingnumber))),
#rangeend = MAX(SUBSTRING(v.bookingnumber, 3, LEN(v.bookingnumber)))
FROM Visit v
WHERE v.branchid = #BranchId
AND v.date >= #startDate
AND LEFT(v.bookingnumber, 2) = #Prefix;
SET #RangeNow = #rangestart
WHILE #rangenow < #rangeend
BEGIN
/*Insert full sequence into temporary table*/
INSERT INTO #TempDetails (BranchID, SequenceCheck)
SELECT #BranchId,
#Prefix + REPLICATE('0', #SequenceLength-#PrefixLength-LEN(#rangenow)) + CAST(#RangeNow AS VARCHAR(256));
SET #rangenow =#rangenow+1
END;
FETCH NEXT FROM BranchPointer INTO #BranchId, #PrefixLength, #SequenceLength, #startDate
END
CLOSE BranchPointer
DEALLOCATE BranchPointer
/*delete existing sequence numbers from table*/
DELETE FROM #TempDetails
FROM #TempDetails t
INNER JOIN Visit v ON t.BranchID = v.branchid
WHERE t.SequenceCheck = v.BookingNumber
/*Insert the status and link for error messages*/
UPDATE #tempDetails
SET RisStatus = (SELECT Status
FROM RISErrors r
INNER JOIN Practice pr ON pr.id = r.PracticeId
WHERE pr.APIKey = #pracId
AND VisitNumber = SequenceCheck
AND r.id = (SELECT MAX(r1.id)
FROM RISErrors r1
INNER JOIN Practice pr1 ON pr1.id = r1.PracticeId
WHERE pr1.APIKey = #pracId
AND VisitNumber = SequenceCheck)),
RisLink = 'http://billing.cryanic.co.za/Clinton/RISErrors?searchquery=' + SequenceCheck
/*return missing numbers into sequence control callong procedure*/
SELECT DISTINCT SequenceCheck, RisStatus, Rislink
FROM #TempDetails
END

select a row after inner joining two tables and use that row in if condition in same stored procedure

i have to select a row and a boolean from a stored procedure, i have tried as below but not happening:
CREATE PROCEDURE [dbo].[usp_SubcampaignAndStatusByOrgID]
#OrgID INT
AS
BEGIN
SET NOCOUNT ON
select sa.sci
from (
select
sub.SubCampaignID as sci,
sub.SubCampaignName
from SubCampaign sub
INNER JOIN Campaign camp
on sub.CampaignID = camp.CampaignID
WHERE
camp.CompanyName = (select orgName FROM Company WHERE #OrgID= 59)
)as sa
if exists(
select *
from users
where
agentstate in (10,30)
and userID = (select userID from tbl_UsersInSkills where SubcampaignId = sa.sci)
)
select 1
Else
select 0
End
It seems you are wanting to use the column value sa.sci in the second query of your stored procedure. If the first query is going to return a single row and therefore a single value of sa.sci then the code below should work. I am assuming that sa.sci is of integer type.
Note how a variable by the name of #sci is declared at the top of stored procedure and its value is set in the first query; then, this variable is used in the second query.
If using a single value of sa.sci
CREATE PROCEDURE [dbo].[usp_SubcampaignAndStatusByOrgID]
#OrgID INT
AS
BEGIN
SET NOCOUNT ON
declare #sci int;
select #sci = sa.sci
from (
select
sub.SubCampaignID as sci,
sub.SubCampaignName
from SubCampaign sub
INNER JOIN Campaign camp
on sub.CampaignID = camp.CampaignID
WHERE
camp.CompanyName = (select orgName FROM Company WHERE #OrgID= 59)
)as sa
if exists(
select *
from users
where
agentstate in (10,30)
and userID = (select userID from tbl_UsersInSkills where SubcampaignId = #sci)
)
select 1
Else
select 0
End
To make the above query work for multiple values of sa.sci you would need to create table variable called #sciValues and then use this table variable in your second query as shown below.
If using multiple values of sa.sci
CREATE PROCEDURE [dbo].[usp_SubcampaignAndStatusByOrgID]
#OrgID INT
AS
BEGIN
SET NOCOUNT ON;
declare #sciValues TABLE ( sciValue int);
insert into #sciValues (sciValue)
select sa.sci
from (
select
sub.SubCampaignID as sci,
sub.SubCampaignName
from SubCampaign sub
INNER JOIN Campaign camp
on sub.CampaignID = camp.CampaignID
WHERE
camp.CompanyName = (select orgName FROM Company WHERE #OrgID= 59)
)as sa
if exists(
select *
from users
where
agentstate in (10,30)
and userID in (select userID from tbl_UsersInSkills
where SubcampaignId in (select sciValue from #sciValues))
)
select 1
Else
select 0
End
CREATE PROCEDURE [dbo].[usp_SubcampaignAndStatusByOrgID]
#OrgID INT
AS
BEGIN
SET NOCOUNT ON
select ROW_NUMBER() OVER (ORDER BY sa.sci) AS SRNO,sa.sci
INTO #tmp
from (
select
sub.SubCampaignID as sci,
sub.SubCampaignName
from SubCampaign sub
INNER JOIN Campaign camp
on sub.CampaignID = camp.CampaignID
WHERE
camp.CompanyName = (select orgName FROM Company WHERE #OrgID= 59)
)as sa
DECLARE #sci AS INT
DECLARE #CNT AS SMALLINT
DECLARE #SRNO AS SMALLINT = 1
SELECT #CNT = COUNT(*) FROM #tmp
WHILE(#CNT >= #SRNO)
BEGIN
SELECT #sci = sci
FROM #tmp
WHERE SRNO = #SRNO
if exists(select 1
from users
where agentstate in (10,30)
and userID = (select userID from tbl_UsersInSkills where SubcampaignId = #sci)
)
select 1
Else
select 0
End
SET #SRNO = #SRNO + 1
END
Try This .....
You either need to store sa.sci in variable or temp table and then you need to use this in your next query of stored procedure.
I believe you need boolean value along with each row. Then following query might help you:
CREATE PROCEDURE [dbo].[usp_SubcampaignAndStatusByOrgID]
#OrgID INT
AS
BEGIN
SET NOCOUNT ON
select sa.sci,
case when exists(
select *
from users
where
agentstate in (10,30)
and userID = (select userID from tbl_UsersInSkills where SubcampaignId = sa.sci)
) then 1 else 0 end as boolValue
from (
select
sub.SubCampaignID as sci,
sub.SubCampaignName
from SubCampaign sub
INNER JOIN Campaign camp
on sub.CampaignID = camp.CampaignID
WHERE
camp.CompanyName = (select orgName FROM Company WHERE #OrgID= 59)
)as sa
End

Resources