Solved

A cursor with the name 'crsSelectParam' already exists.

Posted on 2007-04-01
2
4,415 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

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…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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