【问题标题】:Failed to initialize sqlcmd library with error number -2147467259. - XML Column无法初始化 sqlcmd 库,错误号为 -2147467259。 - XML 列
【发布时间】:2021-08-15 03:34:19
【问题描述】:

我不断收到此错误

初始化 sqlcmd 库失败,错误号为 -2147467259

当我尝试执行以下 SQL 代码时。这是一个普通查询,而不是通过 SQL Server 代理。

declare @SQL varchar(8000)
set @SQL = 'select a.b.value(''(../../@ID)'',''varchar(100)'') as [System-ID]
            from [DB].[Schema].[Configuration] R
            outer apply R.[Configuration].nodes(''root/System/Role/Authorization'') as a(b) 
            where R.[Report ID] = ''IT.00004'''

print @SQL

exec msdb.dbo.sp_send_dbmail
    @profile_name = 'servername',
    @recipients = 'info@company.com',
    @subject = 'Test B',
    @query = @SQL

但是,当我按照以下方式修改查询时,一切正常。

declare @SQL varchar(8000)
set @SQL = 'select *
            from [DB].[Schema].[Configuration] R
            where R.[Report ID] = ''IT.00004'''

print @SQL

exec msdb.dbo.sp_send_dbmail
    @profile_name = 'servername',
    @recipients = 'info@company.com',
    @subject = 'Test B',
    @query = @SQL

所以问题肯定出在语句的这一部分(我在“Configuration”表中引用了一个名为“Configuration”的XML列->列名和表名相同):

outer apply R.[Configuration].nodes('root/System/Role/Authorization') as a(b)

当我在msdb.dbo.sp_send_dbmail 语法之外运行这两个查询时,它们都运行得很好。

有人知道发生了什么吗?我怀疑它与权限相关,因为导致问题的部分使用的是不会导致任何问题的同一张表。

【问题讨论】:

标签: sql-server xml sqlcmd sp-send-dbmail outer-apply


【解决方案1】:

为了解决这个问题,我必须在查询中添加以下内容:

set @SQL = 'SET QUOTED_IDENTIFIER ON '
set @SQL = @SQL + char(13)
set @SQL = @SQL + 'select a.b.value(''(../../@ID)'',''varchar(100)'') as [System-ID]
            from [DB].[Schema].[Configuration] R
            outer apply R.[Configuration].nodes(''root/System/Role/Authorization'') as a(b) 
            where R.[Report ID] = ''IT.00004'''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    相关资源
    最近更新 更多