Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Code fails - Query Analyzer works (?)

Author  Topic 

jp2code
Posting Yak Master

175 Posts

Posted - 2009-01-26 : 14:27:39
Below is code that returns a nice table for me in Query Analyzer; however, if I try to run this in my Visual Studio 2005 project, I get this message:
quote:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <-, >, >- or when the subquery is used as an expression.


Here is the code:
CREATE PROCEDURE AcpReport_OpByMachine(@StartDate DateTime, @EndDate DateTime) AS
/* Created by Joe Pool - searches all records for employee records on a machine
This query is to fill a tree view control with Operator names */
DECLARE @DATE1 DateTime, @DATE2 DateTime, @space char(1)
SET @DATE1=@StartDate
SET @DATE2=@EndDate
SET @space=' '

SELECT DISTINCT T1.SYSTEM_ID as 'System_ID', (SELECT FIRSTNAME+@space+LASTNAME FROM EmployeeInfo WHERE NUM=T1.OP_ID) AS 'Operator' FROM ACP_PARTS T1
WHERE (T1.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
UNION
SELECT DISTINCT 'Acp_Packout_'+SubString(Cast(DatePart(Year, T2.[Date_Time]) as VarChar(4)), 3, 2) as 'System_ID', (SELECT FIRSTNAME+@space+LASTNAME FROM EmployeeInfo WHERE NUM=T2.OP_ID) AS 'Operator' FROM BOX_DATA T2
WHERE (T2.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
UNION
SELECT DISTINCT T3.SYSTEM_ID as 'System_ID', (SELECT FIRSTNAME+@space+LASTNAME FROM EmployeeInfo WHERE NUM=T3.OP_ID) AS 'Operator' FROM Final_Check T3
WHERE (T3.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
UNION
SELECT DISTINCT CASE WHEN 0 < (CharIndex(@space, T4.SYSTEM_ID)) THEN SubString(T4.SYSTEM_ID, 1, CharIndex(@space, T4.SYSTEM_ID)) ELSE T4.SYSTEM_ID END as 'System_ID', T4.OP_ID AS 'Operator' FROM TEST_RESULTS T4
WHERE (T4.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
GO



Avoid Sears Home Improvement

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-26 : 14:32:14
You must be passing different parameters to the stored procedure if it works in Query Analyzer and fails through application code.

The issue is with your "Operator" column. Could you just join to EmployeeInfo table to get that info?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2009-01-26 : 15:02:25
Thanks tkizer. Now, Visual Studio doesn't complain.

Now I have another problem: Only the last table is returning any data.

Do you see something incorrect in this newest version?
DECLARE @DATE1 DateTime, @DATE2 DateTime, @space char(1)
SET @DATE1=@StartDate
SET @DATE2=@EndDate
SET @space=' '

SELECT T1.SYSTEM_ID as 'System_ID', NUM AS 'Operator'
FROM ACP_PARTS T1 INNER JOIN EmployeeInfo ON NUM=T1.[OP_ID]
WHERE (T1.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
UNION
SELECT 'Acp_Packout_'+SubString(Cast(DatePart(Year, T2.[Date_Time]) as VarChar(4)), 3, 2) as 'System_ID', NUM AS 'Operator'
FROM BOX_DATA T2 INNER JOIN EmployeeInfo ON NUM=T2.[OP_ID]
WHERE (T2.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
UNION
SELECT T3.SYSTEM_ID as 'System_ID', NUM AS 'Operator'
FROM Final_Check T3 INNER JOIN EmployeeInfo ON NUM=T3.[OP_ID]
WHERE (T3.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
UNION
SELECT CASE WHEN 0 < (CharIndex(@space, T4.SYSTEM_ID)) THEN SubString(T4.SYSTEM_ID, 1, CharIndex(@space, T4.SYSTEM_ID)) ELSE T4.SYSTEM_ID END as 'System_ID', T4.OP_ID AS 'Operator'
FROM TEST_RESULTS T4
WHERE (T4.[DATE_TIME] BETWEEN @DATE1 AND @DATE2)
GO



Avoid Sears Home Improvement
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-26 : 15:03:47
I don't see anything wrong with it. You must not have data that matches the join criteria or the where clause. Perhaps you want an OUTER JOIN instead?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2009-01-26 : 15:34:17
Oh! Yes, I just found it and was coming here to update.

Thanks for your excellent help.


Avoid Sears Home Improvement
Go to Top of Page
   

- Advertisement -