0

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

我被困在下面的错误。

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斯蒂芬,销售

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. 

我不敢肯定,为什么我米收到这个错误。我觉得我在处理他们。任何关于此的输入都会有所帮助。

谢谢。

+0

我有没有提到我讨厌游标有多少?!? :) – 2012-02-21 07:48:25

回答

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 

“光标已经打开”当您运行的程序进行第二次监守原来光标仍然是开放的(如第一次尝试能够关闭之前出错)发生错误。

+0

是的,这确实解决了我关于游标的错误....但不是目的....谢谢! – user1141584 2012-02-21 23:25:05

0

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

+0

是的,类似于...但我仍然想要得到一个SQL解决方案...谢谢! – user1141584 2012-02-21 23:24:14