搜索数据库中的文本和int返回错误的多部分标识符无法绑定

fruv7luv  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(302)

我试图修改和现有的搜索所有表脚本,我发现(在这个答案从2009年)包括int选项,但我得到一个错误:
消息4104,16级,状态1,行1
无法绑定多部分标识符“dbo.adacs\u maps”。
当我运行数据库时,为数据库中的每一列。

ALTER PROC [dbo].[SearchAllTables]
(
    @SearchStr nvarchar(100),
    @SearchInt int = 0
)
AS
BEGIN

-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT

DECLARE @Results TABLE(ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET  @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
    SET @ColumnName = ''
    SET @TableName = 
    (
        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
        FROM    INFORMATION_SCHEMA.TABLES
        WHERE       TABLE_TYPE = 'BASE TABLE'
            AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
            AND OBJECTPROPERTY(
                    OBJECT_ID(
                        QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                         ), 'IsMSShipped'
                           ) = 0
    )

    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
    BEGIN
        SET @ColumnName =
        (
            SELECT MIN(QUOTENAME(COLUMN_NAME))
            FROM    INFORMATION_SCHEMA.COLUMNS
            WHERE       TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                AND TABLE_NAME  = PARSENAME(@TableName, 1)
                AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int')
                AND QUOTENAME(COLUMN_NAME) > @ColumnName
        )

        IF @ColumnName IS NOT NULL
        BEGIN
            INSERT INTO @Results
            EXEC
            (
                'if exists (SELECT name FROM SYSOBJECTS WHERE xtype = ''U'' and name like trim('']'' from SUBSTRING(' + @TableName + ' , 8, 100))) BEGIN '
               +'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' = ' + @SearchInt
               +' END
               ELSE
                BEGIN '
               +
                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                +' END'
            )
        END
    END 
END

SELECT ColumnName, ColumnValue FROM @Results
END

有人能指出我做错了什么吗,因为这(我不羞于承认)超出了我的知识范围。
你应该能够在任何数据库上运行这个,所以我没有包括演示数据。
编辑1:
我只修改了以下部分:
添加了参数
更改了最终选择:

EXEC
   (
      'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
      FROM ' + @TableName + ' (NOLOCK) ' +
      ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
   )

变成现在的样子。
编辑2:
在这里找到了一个很好的解决方案,但我会继续编写我的脚本,看看为什么它不起作用。
感谢前两位评论员的帮助,我将使用建议的select更仔细地查看代码。

ig9co6j1

ig9co6j11#

当我调试/开发动态代码时,我倾向于使用print语句来查看代码哪里出错。我分解了这个过程,然后打印包含动态sql的变量。也可以使用调试器。
所发生的是,在动态代码中,您正在搜索一个不存在的列。这是因为缺少额外的引号来转换为字符串。下面是我在northwind数据库上使用您的过程并搜索“cat”时的第一位动态代码的打印。

if exists (SELECT name 
            FROM SYSOBJECTS 
            WHERE xtype = 'U' 
-- SQL server sees [dbo].[Categories] as a non existing column
            and name like trim(']' from SUBSTRING([dbo].[Categories] , 8, 100))) 
            BEGIN 
            SELECT '[dbo].[Categories].[CategoryID]', LEFT([CategoryID], 3630) 
                FROM [dbo].[Categories] (NOLOCK)  WHERE 
                [CategoryID] = 1 END
               ELSE
                BEGIN SELECT '[dbo].[Categories].[CategoryID]', LEFT([CategoryID], 3630) 
                FROM [dbo].[Categories] (NOLOCK)  WHERE [CategoryID] LIKE '%cat%' END

下面是对代码的两个修改,以使其正常工作。额外的引号和一个额外的变量,它去掉了模式,因为模式不在您要查找的表的name列中。

ALTER PROC [dbo].[SearchAllTables]
(
    @SearchStr nvarchar(100),
    @SearchInt int = 0
)
AS
BEGIN

-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT

DECLARE @Results TABLE(ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110), @TableNoSchema nvarchar(256)
SET  @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
    SET @ColumnName = ''
    SET @TableName = 
    (
        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
        FROM    INFORMATION_SCHEMA.TABLES
        WHERE       TABLE_TYPE = 'BASE TABLE'
            AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
            AND OBJECTPROPERTY(
                    OBJECT_ID(
                        QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                         ), 'IsMSShipped'
                           ) = 0
    )
    -- Since the table used below in the dynamic SQL does not have a prepended schema is strip it here
    SET @TableNoSchema = SUBSTRING(@TableName, CHARINDEX('.',@TableName) + 2, LEN(@TableName) - CHARINDEX('.',@TableName) -2 )

    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
    BEGIN
        SET @ColumnName =
        (
            SELECT MIN(QUOTENAME(COLUMN_NAME))
            FROM    INFORMATION_SCHEMA.COLUMNS
            WHERE       TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                AND TABLE_NAME  = PARSENAME(@TableName, 1)
                AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int')
                AND QUOTENAME(COLUMN_NAME) > @ColumnName
        )

        IF @ColumnName IS NOT NULL
        BEGIN
            INSERT INTO @Results
            EXEC -- only added extra quotes so SQL server sees a string instead of a column
            (
                'if exists (SELECT name FROM SYSOBJECTS WHERE xtype = ''U'' and name like trim('']'' from SUBSTRING(''' + @TableNoSchema + ''' , 8, 100))) BEGIN '
               +'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' = ' + @SearchInt
               +' END
               ELSE
                BEGIN '
               +
                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                +' END'
            )
        END
    END 
END

SELECT ColumnName, ColumnValue FROM @Results
END

相关问题