Solved

sql sub procedure returns correct results but also an error message due to a sub query violation

Posted on 2007-11-28
4
513 Views
Last Modified: 2010-04-21
Hi, running sql server 2005 stored procedure using a while loop, with if else statement, i try to populate a temporary returnable table with values for a booking, slotid, time and username/availability.

However, despite the correct results returning, i get the following error message

Msg 512, Level 16, State 1, Procedure test2, Line 63
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 512, Level 16, State 1, Procedure test2, Line 63
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

What could be wrong?

Thanks

Julia
WHILE
	(Select Max (sa.SlotID)
	From SlotAvailability as sa join AvailabilityType as at on sa.availabilityid = at.availabilityID
	WHERE sa.MachineID = @MachineID and sa.availabilityid = 1) >= @counter
		
		Begin
 
--checks to see if the counter matches a slotId that exists for the selected machine, or null in this case
--else moves on to the next slotID before any processing gets done.
		If 
			(
			select slotid
			from slotavailability
			where exists
			(select slotID
			from slotavailability
			Where @Counter   in (Select SlotID from slotavailability where machineID = @machineID and availabilityid = 1))) is null
 
 
			Begin
			SET @Counter = @Counter + 1
 
			End
 
		Else
 
			Begin
			Set @SlotTimeCounter	= (select slottime	from slot where slotId = @Counter)
 
			Insert Into @DateResults
			Values (@Counter,@SlotTimeCounter, 'Available')
			
			
	
 
 
			SET @Counter = @Counter + 1
 
		
			End
		END
 
	END

Open in new window

0
Comment
Question by:Juliafrazer
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 400 total points
ID: 20366919
>Subquery returned more than 1 value.
Here are your sub-queries.  Run both of them to see if one returns more than one record, then fix.

select slotID from slotavailability Where @Counter in (Select SlotID from slotavailability where machineID = @machineID and availabilityid = 1))) is

Select SlotID from slotavailability where machineID = @machineID and availabilityid = 1
0
 
LVL 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 100 total points
ID: 20367001
Can you simplify your query to something like:
WHILE
	(Select Max (sa.SlotID)
	From SlotAvailability as sa join AvailabilityType as at on sa.availabilityid = at.availabilityID
	WHERE sa.MachineID = @MachineID and sa.availabilityid = 1) >= @counter
		
		Begin
 
--checks to see if the counter matches a slotId that exists for the selected machine, or null in this case
--else moves on to the next slotID before any processing gets done.
		If 
			(
			select slotID
			from slotavailability
			Where SlotID = @Counter and machineID = @machineID and availabilityid = 1) is null
 
 
			Begin
			SET @Counter = @Counter + 1
 
			End
 
		Else
 
			Begin
			Set @SlotTimeCounter	= (select slottime	from slot where slotId = @Counter)
 
			Insert Into @DateResults
			Values (@Counter,@SlotTimeCounter, 'Available')
			
			
	
 
 
			SET @Counter = @Counter + 1
 
		
			End
		END
 
	END

Open in new window

0
 

Author Closing Comment

by:Juliafrazer
ID: 31411460
thanks guys, This helped me.  The select statement in question was returning multiple slotID's.  It had to trim the query as you selected and add an an additional and slotID = @counter statement
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 20367165
Thanks for the grade.  Good luck with your project.  -Jim

btw What casino do you work for?  I spent some time at Mystic Lake Casino in Prior Lake, Minnesota, and nearly accepted an offer at Greektown Casino in Detroit.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
optimize stored procedure 6 29
SSRS Enable Remote Errors 4 26
replace \ by - in select 4 21
SQL Server Connection String through a VPN 8 29
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question