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

  • SEARCH
  • Home
  • 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 7543695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:29:41+00:00 2026-05-30T08:29:41+00:00

I m trying to search database values through the views. I m stuck at

  • 0

I m trying to search database values through the views.

I m stuck at the below error.

USE AdventureWorks
GO

--EXEC Customer.sp_FindInViews Stephen, Sales

ALTER PROCEDURE Customer.sp_FindInViews @stringToFind VARCHAR(100), @schema sysname
AS
SET NOCOUNT ON

DECLARE   
      @ViewName AS nVarChar(128)
      , @TmpQuery AS nVarChar(500)
      , @Out3 as int

    , @sqlCommand VARCHAR(8000)
    , @where VARCHAR(8000)
    , @columnName sysname
    , @cursor VARCHAR(8000)

DECLARE Outer_Cursor CURSOR FOR
    SELECT schema_name(schema_id)+'.'+name as "View_Name",schema_id FROM [sys].[all_views] 
    where schema_id  in (@schema)

    OPEN Cur_Views
        FETCH NEXT FROM Cur_Views INTO @ViewName
        WHILE @@Fetch_Status = 0 
        BEGIN
            SET @sqlCommand = 'SELECT * FROM ' + @ViewName + ' WHERE'
            SET @where = ''

        DECLARE col_cursor CURSOR FOR
            SELECT syscolumns.name FROM sys.sysobjects "sysobjects"
            INNER JOIN sys.syscolumns "syscolumns"
            on syscolumns.id = sysobjects.id
            WHERE (sysobjects.type = 'V'    and SCHEMA_NAME(sysobjects.uid) + '.' +sysobjects.name = @ViewName)

            OPEN col_cursor   
                FETCH NEXT FROM col_cursor INTO @columnName   
                WHILE @@FETCH_STATUS = 0   
                BEGIN   
                    IF @where <> ''
                        SET @where = @where + ' OR'
                        ---------------------------------------------------------------------------
                        SET @where = @where + ' ' + @columnName + ' LIKE ''' + @stringToFind + ''''
                        SET @sqlCommand = @sqlCommand +   @where

                        CREATE TABLE #Data (var varchar)
                        SELECT  @TmpQuery = @sqlCommand
                        INSERT #Data exec (@TmpQuery)
                        SELECT @Out3 = var from #Data
                        PRINT @Out3
                        DROP TABLE #Data
                    FETCH NEXT FROM col_cursor INTO   @columnName   
                END   
            CLOSE col_cursor   
            DEALLOCATE col_cursor       
    CLOSE Outer_Cursor
    DEALLOCATE Outer_Cursor             
END
GO

The code compiles , but it does give the error when executed as below :
EXEC Customer.sp_FindInViews Stephen, Sales

Msg 16915, Level 16, State 1, Procedure sp_FindInViews, Line 19
A cursor with the name 'Outer_Cursor' already exists.
Msg 16905, Level 16, State 1, Procedure sp_FindInViews, Line 22
The cursor is already open.
Msg 16924, Level 16, State 1, Procedure sp_FindInViews, Line 23
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.

I m not sure , why I m getting this error. I feel i m handling them. Any input on this , would be helpful.

Thanks.

  • 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-30T08:29:42+00:00Added an answer on May 30, 2026 at 8:29 am

    Looks to me that you’ve changed cursor names. You start by declaring Outer_Cursor then open a cursor called Cur_Views.

    Also when you fetch from the cursor you are only putting the cursor values in to 1 variable, in the cursor declaration you list 2 fields (View_Name and schema_id).

    DECLARE Outer_Cursor CURSOR FOR 
        SELECT schema_name(schema_id)+'.'+name as "View_Name",schema_id FROM [sys].[all_views]  
        where schema_id  in (@schema) 
    
        OPEN Cur_Views
           FETCH NEXT FROM Cur_Views INTO @ViewName 
    

    The “cursor is already open” errors occur when you run the procedure a second time becuase the original cursors are still open (as the first attempt errored before being able to close them).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to basically do a search through the database with Perl to tell
I am trying to filter/search a database with ajax $.ajax({ type: POST, url: filterSearch.php,
In JavaScript, I'm trying using the user's input to search my database. For example,
I'm trying to build a search for our internal support database - each support
I'm trying to search Netflix through their API, but without logging anyone in (because
I am basically trying to create a new database and enter the database values
We're a couple of students trying to implement a design to search for customer-information
I am using solr to make search on my database, and am trying to
I am trying to search my database to see if a number exists in
Hi I am trying to use Django to make a page that will search

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.