列名“USER_SOURCE”无效

发布于 2024-12-25 20:36:53 字数 594 浏览 2 评论 0原文

我有一个存储过程,它在 xml @Data 输出参数中返回结果。

要在 xml 代码中分配数据,

SELECT @data= (
    SELECT DISTINCT 
        CONVERT(varchar(2),u.USER_SOURCE ) + '~' + 
        CONVERT(varchar(20), u.[USER_ID]) + '-' +
        CONVERT(varchar(10), u.DEALER_ID) as USER_DEALER_ID
    FROM #users u FOR XML RAW('users')) 

当我在 SQL Server Mgmt Studio 中执行该过程时,我可以看到结果正常。

该过程是从另一个过程调用的,并且该父过程在 SSRS 中使用。

在 SSRS 中我收到错误

数据集“DataSet1”的查询执行失败。列名无效 “用户源”。列名“USER_ID”无效。列名无效 “DEALER_ID”。

你能帮忙吗?

谢谢, 切坦

I have a stored procedure which is returning a result in an xml @Data output parameter

To assign data in xml code is

SELECT @data= (
    SELECT DISTINCT 
        CONVERT(varchar(2),u.USER_SOURCE ) + '~' + 
        CONVERT(varchar(20), u.[USER_ID]) + '-' +
        CONVERT(varchar(10), u.DEALER_ID) as USER_DEALER_ID
    FROM #users u FOR XML RAW('users')) 

When I exec the procedure in SQL Server Mgmt Studio, I can see the result OK.

This procedure is been called from another procedure, and that parent procedure is used in SSRS.

In SSRS I am getting error

Query execution failed for dataset 'DataSet1'. Invalid column name
'USER_SOURCE'. Invalid column name 'USER_ID'. Invalid column name
'DEALER_ID'.

Could you please help?

Thanks,
Chetan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2025-01-01 20:36:53

这是一个脚本,我认为它重现了与您的问题相同的问题:

CREATE PROCEDURE TestTmpTable
  @value varchar(20)
AS
BEGIN
  CREATE TABLE #test (id int IDENTITY, value varchar(20));
  INSERT INTO #test (value) VALUES (@value)
  SELECT * FROM #test;
  DROP TABLE #test;
END

GO

CREATE TABLE #test (id int IDENTITY, value2 varchar(20));
EXEC TestTmpTable 'some text';
SELECT * FROM #test;
DROP TABLE #test;

GO

DROP PROCEDURE TestTmpTable

如您所见,这里有两个 #test 表,一个是在存储过程中创建的,另一个是在调用存储过程。它们具有不同的结构:一个具有名为 value 的列,另一个具有名为 value2 的列。如果运行该脚本,您将看到此错误:

Msg 207, Level 16, State 1, Procedure TestTmpTable, Line 6
Invalid column name 'value'.

我目前无法向您指出相关文档文章,但对我来说,很明显,在执行 SP 之前立即进行了一些初步名称检查。在此阶段,存储过程中引用的列名与现有表中实际存在的列名之间的差异就会显现出来,这使得执行变得不可能。

如果将 value2 更改为 value,脚本将正常运行,并且输出中将有两个行集,其中一个带有 'some text'值,其他为空。当然,如果您删除与外部 #test 表相关的所有部分,该脚本将起作用。

因此,请检查调用您的过程的位置,看看当时是否存在任何其他 #users 表,如果存在,请根据您的情况修改问题。

Here's a script which I think reproduces a problem identical to yours:

CREATE PROCEDURE TestTmpTable
  @value varchar(20)
AS
BEGIN
  CREATE TABLE #test (id int IDENTITY, value varchar(20));
  INSERT INTO #test (value) VALUES (@value)
  SELECT * FROM #test;
  DROP TABLE #test;
END

GO

CREATE TABLE #test (id int IDENTITY, value2 varchar(20));
EXEC TestTmpTable 'some text';
SELECT * FROM #test;
DROP TABLE #test;

GO

DROP PROCEDURE TestTmpTable

As you can see, there are two #test tables here, one is created in the stored procedure, the other one in the batch that invokes the stored procedure. They have different structures: one has a column named value, the other a column named value2. If you run the script, you'll see this error:

Msg 207, Level 16, State 1, Procedure TestTmpTable, Line 6
Invalid column name 'value'.

I can't point you to a relevant documentation article at the moment, but to me it is evident enough that some preliminary name checking is taking place immediately before the execution of the SP. At that stage, a discrepancy between the column names referenced in the stored procedure and those actually present in the already existing table is revealed, which renders the execution impossible.

If you change value2 to value, the script will work without any problem, and there will be two row sets in the output, one with the 'some text' value, the other empty. And of course the script will work if you remove all parts related to the external #test table.

So, check the places where your procedure is called to see if any other #users table can be existing by that moment, and if so, amend the issue according to your situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文