Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have an SQL server procedure which draws randomly teams to groups (A,B,C,...).
I have compiled it successfuly, but when I run it, it throws an error: Invalid object name 'Beach_Group_Names'. Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

And I wonder why...
At first: Beach_Group_Names is a Cursor.
At second: I have counted begins and ends and the count was the same. Begin Transaction and Commit Transaction should be at the same level.

Does anyone help me solve this error?
Error at:
SQL
Set @Group_Name = (SELECT TOP (1) Group_Name FROM Beach_Group_Names ORDER BY NEWID())


Whole code:
SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Pepin z Hane
-- Create date: 
-- Description:	
-- =============================================
CREATE PROCEDURE Draw_Groups 
	-- Add the parameters for the stored procedure here
	@Tour_ID int
AS
	Declare Team_IDs Cursor for Select Team_ID from Registration where Tournament_ID = @Tour_ID and Group_Name = 'RP' order by Datum_Registrace
	Declare Beach_Group_Names Cursor for Select Group_Name from Beach_Group where Tournament_ID = @Tour_ID and Group_Name not like 'R%'
	Declare @Group_Counter int
	Set @Group_Counter = (select count (Group_Name) from Beach_Group where Tournament_ID = @Tour_ID)-4
	Declare @Team_ID int
	Declare @Celkovy_Pocet_Tymu int 
	Set @Celkovy_Pocet_Tymu = (select count(*) from Registration where Tournament_ID = @Tour_ID and Group_Name = 'RP')
	Declare @Pocet_Tymu_Ve_Skupine int
	Declare @Group_Name Varchar (5)
	Declare @Datum_Registrace Datetime
BEGIN
	Begin try
		begin transaction
			open Team_IDs
			Fetch NEXT from Team_IDs into @Team_ID 
			While @@Fetch_Status = 0 
			begin
				Set @Group_Name = (SELECT TOP (1) Group_Name FROM Beach_Group_Names ORDER BY NEWID())
				--
				while @Pocet_Tymu_Ve_Skupine >= @Celkovy_Pocet_Tymu/@Group_Counter begin
					if @Group_Name = 'A' begin
						Set @Group_Name = 'B'
					end
					else if @Group_Name = 'B' begin
					
						if @Group_Counter > 2 begin
							Set @Group_Name = 'C'
						end
						else begin
							Set @Group_Name = 'A'
						end
					end
					else if @Group_Name = 'C' begin
					
						if @Group_Counter > 3 begin
							Set @Group_Name = 'D'
						end
						else begin
							Set @Group_Name = 'A'
						end
					end
					else if @Group_Name = 'D' begin
					
						if @Group_Counter > 4 begin
							Set @Group_Name = 'E'
						end
						else begin
							Set @Group_Name = 'A'
						end
					end
					else if @Group_Name = 'E' begin
					
						if @Group_Counter > 5 begin
							Set @Group_Name = 'F'
						end
						else begin
							Set @Group_Name = 'A'
						end
					end
					else if @Group_Name = 'F' begin
						Set @Group_Name = 'A'
					end
				end
				--
				Set @Datum_Registrace = (select Datum_Registrace from Registration where Tournament_ID = @Tour_ID and Team_ID = @Team_ID)	
				Insert into Registration values (@Tour_ID, @Group_Name, @Team_ID, 1, @Datum_Registrace, 0,0,0,0,0,0,0,0)
			end
		commit transaction
		return 1
	End Try
	begin Catch
		rollback
		return 0
	end Catch
END
GO


Thanks!

-Pepin z Hané
Posted
Updated 21-Oct-12 10:53am
v2
Comments
Sergey Alexandrovich Kryukov 22-Oct-12 0:38am    
Is there any specific reason to use a cursor? Cursors have critical disadvantages and they use may indicate bad code design.
--SA
Pepin z Hane 22-Oct-12 13:15pm    
You're right it's better without it, it does not throws this error now, thanks!
Sergey Alexandrovich Kryukov 22-Oct-12 14:38pm    
You are welcome.
--SA

1 solution

I can see now what the problem is. Originally I thought that the problem was with Implicit Transactions[^].

Now, I can see that you are trying to execute a SQL statement on a cursor object. You can't do this. The whole purpose of a cursor is to scan it row by row.

Instead of writing:

SQL
Set @Group_Name = (SELECT TOP (1) Group_Name FROM Beach_Group_Names ORDER BY NEWID())


You need to open Beach_Group_Names cursor and include it to the FETCH NEXT, like this:

SQL
Fetch NEXT from Beach_Group_Names into @Group_Name 


I don't see where you open the Beach_Group_Names cursor, so I guess it should be opened inside the top cursor Team_IDs
 
Share this answer
 
v2
Comments
Pepin z Hane 21-Oct-12 16:58pm    
Your Solution didn't help :-(
chaau 21-Oct-12 17:30pm    
I have updated the solution above
Pepin z Hane 22-Oct-12 13:16pm    
Thanks, finnaly I haven't used the cursor, I am selecting Group_Name without cursor as well!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900