Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:52:16+00:00 2026-05-25T11:52:16+00:00

I am getting the following errors but can’t figure out why.. Msg 16915, Level

  • 0

I am getting the following errors but can’t figure out why..

Msg 16915, Level 16, State 1, Procedure client_myClientsProc, Line
46
A cursor with the name ‘cur_keywords’ already exists.
Msg 16905, Level 16, State 1, Procedure client_myClientsProc, Line
47
The cursor is already open.

And then if I try to run it again it says

Msg 208, Level 16, State 0, Procedure client_myClientsProc Line 49
Invalid object name ‘##CLIENTS_KEYWORD.

Now this is old code that I am trying to fix, so please bear with me…

ALTER PROCEDURE [dbo].[client_myclientsproc]  
   @Keywords varchar(max),  
   @Delimiter varchar(10) = ' '  
AS  
BEGIN  
  SET NOCOUNT ON;  

  DECLARE @MYQUERY NVARCHAR(MAX);    
  DECLARE @tempkeyword varchar(4000)    
  DECLARE @TempCount INT   

  IF OBJECT_ID('TempDB..##CLIENTS_KEYWORD') IS NOT NULL
  BEGIN
     DROP TABLE ##CLIENTS_KEYWORD
  END
  ELSE
  BEGIN
    CREATE TABLE ##CLIENTS_KEYWORD(client_id int)  
  END

  IF OBJECT_ID('TempDB..##TEMP_CLIENTS_KEYWORD') IS NOT NULL
  BEGIN
    DROP TABLE ##TEMP_CLIENTS_KEYWORD
  END
  ELSE
  BEGIN
     CREATE TABLE ##TEMP_CLIENTS_KEYWORD(productid int)
  END  

  SET @MYQUERY = 'SELECT clientID, Client_Name FROM MYCLIENTS WHERE  ClientID IN ';                    

 IF(@Delimiter<>'none')
 BEGIN   
  DECLARE cur_keywords CURSOR FOR  
    select value from SC_Split(@Keywords,@Delimiter)     

  OPEN cur_keywords  

  FETCH NEXT FROM cur_keywords into @tempkeyword  
  INSERT ##CLIENTS_KEYWORD(client_id) exec getClientsByKeyword @tempkeyword  

   WHILE @@FETCH_STATUS = 0     

    FETCH NEXT FROM cur_keywords into @tempkeyword  

     INSERT ##TEMP_CLIENTS_KEYWORD(client_id) exec getClientsByKeyword @tempkeyword 
          select @TempCount=COUNT(client_id) from  ##TEMP_CLIENTS_KEYWORD     
            IF(@TempCount<>0)    
                BEGIN    
                    DELETE FROM ##CLIENTS_KEYWORD WHERE client_id NOT IN(SELECT client_id from ##TEMP_CLIENTS_KEYWORD)     
       INSERT  ##CLIENTS_KEYWORD (client_id) (select client_id from ##TEMP_CLIENTS_KEYWORD)                       
                END  
       CLOSE cur_keywords                              
       DEALLOCATE cur_keywords                                  
 END
 ELSE 
 BEGIN  
   print(@Keywords)
   INSERT ##CLIENTS_KEYWORD(client_id) exec getClientsByKeyword @Keywords      
 END  

 SET @MYQUERY = @MYQUERY + '(SELECT * FROM ##CLIENTS_KEYWORD)'                           
 SET @MYQUERY = @MYQUERY + ' ORDER BY NAME'                    

 print    @MYQUERY   

 EXEC SP_EXECUTESQL @MYQUERY                                           
END  
GO

====================

get clients by keyword code

CREATE PROCEDURE [dbo].[getClientsByKeyword]  
 @Keyword varchar(max)  

AS  
BEGIN  
SET NOCOUNT ON;  


 select                     
  DISTINCT(clients.clientID)
 from                     
  Clients_Table clients                               
  left join clientNumber cn on cn.clientid=clients.clientid                    
 where                     
  clients.activeind = 1                     
  and (clients.Name like '%' + @Keyword + '%'                     
  or clients.clientNum LIKE '%' + @Keyword + '%'           
  or cn.clientN like   '%' + @Keyword + '%' )      

END  



GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:52:17+00:00Added an answer on May 25, 2026 at 11:52 am
    • Your OPEN, FETCH, WHILE, CLOSE and DEALLOCATE are misplaced.
    • Your temp tables need creating every time.

    Try this refactored script. Modify as you need:

    DECLARE @tempkeyword varchar(4000)    
    DECLARE @TempCount INT   
    
    IF OBJECT_ID('TempDB..##CLIENTS_KEYWORD') IS NOT NULL
        DROP TABLE ##CLIENTS_KEYWORD
    
    CREATE TABLE ##CLIENTS_KEYWORD(client_id int)  
    
    IF OBJECT_ID('TempDB..##TEMP_CLIENTS_KEYWORD') IS NOT NULL
        DROP TABLE ##TEMP_CLIENTS_KEYWORD
    
    CREATE TABLE ##TEMP_CLIENTS_KEYWORD(productid int)
    
    SET @MYQUERY = 'SELECT clientID, Client_Name FROM MYCLIENTS WHERE  ClientID IN ';                    
    
    IF(@Delimiter<>'none')
    BEGIN       
         DECLARE cur_keywords CURSOR FOR  
            SELECT value FROM SC_Split(@Keywords,@Delimiter)     
    
          OPEN cur_keywords  
          FETCH NEXT FROM cur_keywords into @tempkeyword  
          WHILE @@FETCH_STATUS = 0     
          BEGIN
             FETCH NEXT FROM cur_keywords into @tempkeyword  
    
             INSERT ##CLIENTS_KEYWORD(client_id) exec getClientsByKeyword @tempkeyword               
             INSERT ##TEMP_CLIENTS_KEYWORD(client_id) exec getClientsByKeyword @tempkeyword 
             SELECT @TempCount=COUNT(client_id) from  ##TEMP_CLIENTS_KEYWORD     
             IF(@TempCount<>0)    
             BEGIN    
                  DELETE FROM ##CLIENTS_KEYWORD 
                      WHERE client_id NOT IN(SELECT client_id from ##TEMP_CLIENTS_KEYWORD);
    
                      INSERT  ##CLIENTS_KEYWORD (client_id) 
                      SELECT  client_id from ##TEMP_CLIENTS_KEYWORD;
             END  
          END
    
         CLOSE cur_keywords                              
         DEALLOCATE cur_keywords                                  
    END
    ELSE 
    BEGIN  
       print(@Keywords)
       INSERT ##CLIENTS_KEYWORD(client_id) exec getClientsByKeyword @Keywords      
    END  
    
    SELECT @MYQUERY=@MYQUERY + '(SELECT * FROM ##CLIENTS_KEYWORD) ORDER BY NAME'                           
    print    @MYQUERY   
    EXEC SP_EXECUTESQL @MYQUERY                                           
    END  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be really easy but I can't figure out how to make it
I keep getting the following errors: 2011-04-02 14:55:23.350 AppName[42430:207] nested push animation can result
I am getting the following errors: PHP Warning: Module 'ldap' already loaded in Unknown
I am getting the following error when I get to the line that invokes
I'm getting the following errors from MSBuild while trying to build a solution: C:\dev\MySln.sln
When i am trying to enable the shard... I am getting following errors........ use
With Mockito I can do the following: verify(someService).process(any(Person.class)); But how do I write this
I am trying to create an SD card on emulator, but am getting following
I getting the following error when I try to connect to my server app
I am getting the following error: Access denied for user 'apache'@'localhost' (using password: NO)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.