Solved

A cursor with the name 'crsSelectParam' already exists.

Posted on 2007-04-01
2
4,413 Views
Last Modified: 2008-01-09
Dear Experts,
The following stored procedure generates the following error messages:
Msg 16915, Level 16, State 1, Procedure SearchMatchedResult, Line 20
A cursor with the name 'crsSelectParam' already exists.
Msg 16915, Level 16, State 1, Procedure SearchMatchedResult, Line 124
A cursor with the name 'crsMustParam' already exists.
Msg 16915, Level 16, State 1, Procedure SearchMatchedResult, Line 195
A cursor with the name 'crsTableName' already exists.
 
--Code of Stored procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter PROCEDURE SearchMatchedResult
      (@PostedJobID int)
AS
BEGIN
      SET NOCOUNT ON;
      
      DECLARE @Criteria VARCHAR(50)
      DECLARE @CriteriaValue VARCHAR(50)
      DECLARE @JobCategory VARCHAR(6)
      DECLARE @Sql VARCHAR(2000)
      DECLARE @WhereClause VARCHAR(1000)
      DECLARE @SelectClause VARCHAR(1000)
      
      SET @SelectClause =''
      SET @WhereClause =''
      SET @Sql =''

      DECLARE crsSelectParam CURSOR FOR
   (
      SELECT rtrim(ltrim(Criteria)) 'Criteria', rtrim(ltrim(CriteriaValue)) 'CriteriaValue'
            FROM PostedJobSearchParam WHERE PostedJobId = @PostedJobID
   )

      OPEN crsSelectParam
            FETCH NEXT FROM crsSelectParam INTO @Criteria, @CriteriaValue
            WHILE @@FETCH_STATUS = 0

      BEGIN
                  IF @Criteria = 'Experience'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + 'Experience'
                        END
                  IF @Criteria = 'Education'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + 'Education'
                        END
                  IF @Criteria = 'Major'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + 'Major'
                        END
                  IF @Criteria = 'Class'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + 'Class'
                        END
                  IF @Criteria = 'MinAge'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + 'Age'
                        END--MinAge is must for age input. MaxAge is optional.

                  IF @Criteria = 'Gender'      
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + 'Gender'
                        END
                  IF @Criteria = 'MustSkill1'      
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'MustSkill2'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'MustSkill3'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'MustSkill4'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'MustSkill5'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill6'      
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill7'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill8'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill9'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill10'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill11'      
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill12'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill13'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill14'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Skill15'
                        BEGIN
                              SET @SelectClause = ltrim(rtrim(@SelectClause)) + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @@FETCH_STATUS <> 0
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + ' , '
                        END
         FETCH NEXT FROM crsSelectParam INTO @Criteria, @CriteriaValue
      END
      CLOSE crsSelectParam

      DECLARE crsMustParam CURSOR FOR
   (
      SELECT rtrim(ltrim(Criteria)) 'Criteria', rtrim(ltrim(CriteriaValue)) 'CriteriaValue'
            FROM PostedJobSearchParam WHERE PostedJobId = @PostedJobID AND Must = 1            
   )
      --//
      --****Where Clause string generation***
      OPEN crsMustParam
            FETCH NEXT FROM crsMustParam INTO @Criteria, @CriteriaValue
            WHILE @@FETCH_STATUS = 0

      BEGIN
                  IF @Criteria = 'Experience'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + 'Experience = ' + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Education'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + 'Education = ' + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Major'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + 'Major = ' + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Class'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + 'Class = ' + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'MinAge'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + 'Age >= ' + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'MaxAge'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + 'Age <= ' + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'Gender'      
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + 'Gender = ' + ltrim(rtrim(@CriteriaValue))
                        END
                  IF @Criteria = 'MustSkill1'      
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + ltrim(rtrim(@CriteriaValue)) + ' = 1'
                        END
                  IF @Criteria = 'MustSkill2'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + ltrim(rtrim(@CriteriaValue)) + ' = 1'
                        END
                  IF @Criteria = 'MustSkill3'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + ltrim(rtrim(@CriteriaValue)) + ' = 1'
                        END
                  IF @Criteria = 'MustSkill4'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + ltrim(rtrim(@CriteriaValue)) + ' = 1'
                        END
                  IF @Criteria = 'MustSkill5'
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + ltrim(rtrim(@CriteriaValue)) + ' = 1'
                        END
                  IF @@FETCH_STATUS <> 0
                        BEGIN
                              SET @WhereClause = ltrim(rtrim(@WhereClause)) + ' AND '
                        END
         FETCH NEXT FROM crsMustParam INTO @Criteria, @CriteriaValue
      END
      CLOSE crsMustParam
      --//
      
      --//
      --***Table name of Job Category name***
      DECLARE crsTableName CURSOR FOR
   (
      SELECT JobCategory FROM PostedJob
            WHERE JobId = @PostedJobID
   )

      OPEN crsTableName
            FETCH NEXT FROM crsTableName INTO @JobCategory
            WHILE @@FETCH_STATUS = 0
            BEGIN
                  IF @@FETCH_STATUS <> 0
                        BEGIN
                              SET @Sql = 'SELECT ' + ltrim(rtrim(@SelectClause)) + ' FROM ' + ltrim(rtrim(@JobCategory)) + ' WHERE ' + @WhereClause + ' ORDER BY SeekerId'
                        END
                  FETCH NEXT FROM crsTableName INTO @JobCategory
      END
      CLOSE crsTableName
      --//
      print @Sql

      --//
      --***Table name of Job Category name***
      DECLARE crsResult CURSOR FOR
   (
      @Sql
   )

      OPEN crsResult
            FETCH NEXT FROM crsResult INTO @JobCategory
            WHILE @@FETCH_STATUS = 0
            BEGIN
                  
                  IF @@FETCH_STATUS <> 0
                        BEGIN
                              SET @Sql = 'SELECT ' + ltrim(rtrim(@SelectClause)) + ' FROM ' + ltrim(rtrim(@JobCategory)) + ' WHERE ' + @WhereClause + ' ORDER BY SeekerId'
                        END
                  FETCH NEXT FROM crsTableName INTO @JobCategory
      END
      CLOSE crsTableName
      --//
      
END
GO
0
Comment
Question by:raju1
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 50 total points
ID: 18831400
U must DEALLOCATE the cursors after closing

CLOSE crsTableName
DECALLOCATE crsTableName

do the same for all the others
0
 

Author Comment

by:raju1
ID: 18831502
Thanks.
See the following code, i don't know how many column there. Is it possible?

DECLARE crsResult CURSOR FOR
   (
      SELECT + @Sql
   )

      OPEN crsResult
            FETCH NEXT FROM crsResult INTO @JobCategory
            WHILE @@FETCH_STATUS = 0
            BEGIN
                  
                  IF @@FETCH_STATUS <> 0
                        BEGIN
                              --Here is processing
                        END
                  FETCH NEXT FROM crsResult INTO @JobCategory
      END
      CLOSE crsResult
      DEALLOCATE crsResult
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

947 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now