Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamic Query Problem
Message
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Dynamic Query Problem
Miscellaneous
Thread ID:
00709214
Message ID:
00709214
Views:
45
I am using a vfp user dialog to build a query string. The user has a list box where they can select multiple departments to use in the query. Then I build the dynamic query. Right now I am pasting it to the clipboard and running it in the query analyser for testing. The problem arises when I select more than one department. If I just select one, I get the desired results (Number of cases by speciality for each month). But if I choose more than one department, it get the following error:
Server: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. 
This is not permitted when the subquery follows =, !=, <, <= , >, >= 
or when the subquery is used as an expression.
I understand what it is telling me, but I don't know how to fix it.
Here is the query that is generated when multiple departments are selected:
Select c.SpecDesc, isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=1) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as JAN, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=2) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as FEB, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=3) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as MAR, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=4) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as APR, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=5) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as MAY, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=6) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as JUN, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=7) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as JUL, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=8) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as AUG, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=9) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as SEP, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=10) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as OCT, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=11) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as NOV, 

isnull((select COUNT(ct.SpecDesc) 
FROM dbo.meetings m1 INNER JOIN 
coSpec ct ON m1.specid = ct.specid 
where (datepart(mm,m1.begintime)=12) and m1.specid=m.specid and datepart(yyyy,m1.begintime)=2002 
and m1.iscompleted=1 and m1.iscancelled=0 
and deptid = 1 or deptid = 23 
GROUP BY ct.SpecDesc),0) as DEC

FROM    dbo.meetings m INNER JOIN 
        coSpec c ON m.specid = c.specid 
where datepart(yyyy,m.begintime)=2002 
and m.iscompleted=1 
and deptid = 1 or deptid = 23 
and m.iscancelled=0 
and m.isblock=0 
GROUP BY m.specid, c.SpecDesc	
Thanks in advance for any help.
Kirk
Next
Reply
Map
View

Click here to load this message in the networking platform