通过视图搜索数据库

时间:2012-02-21 05:30:44

标签: sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2

我试图通过视图搜索数据库值。

我坚持以下错误。

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

代码编译,但它确实在执行时给出错误,如下所示:     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.

我不确定,为什么我会收到此错误。我觉得我正在处理它们。对此有任何意见,都会有所帮助。

感谢。

2 个答案:

答案 0 :(得分:0)

在我看来你已经改变了游标名称。首先声明Outer_Cursor,然后打开一个名为Cur_Views的游标。

当您从游标中获取时,您只将游标值放入1变量,在游标声明中列出2个字段(View_Name和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 

当您第二次运行该过程时,“游标已经打开”错误,因为原始游标仍然打开(因为第一次尝试在关闭它们之前出错)。

答案 1 :(得分:0)

不确定这是否是您一直在寻找的答案,但SQLSearch(http://www.red-gate.com/products/sql-development/sql-search/)是搜索数据库的绝佳工具(当然,您可以将其设置为仅搜索视图)并且它是免费的......