简体   繁体   English

SQL Server 2012存储过程输出被包含过程的结果集中断

[英]SQL Server 2012 stored procedure output being interrupted by contained procedure's result set

long time reader, first time poster. 长期的读者,第一次的海报。

We're observing something strange in our production instance of SQL Server 2012 after we migrated our application's databases from dev, to test, to production for our recent go-live. 在我们将应用程序的数据库从开发阶段迁移到测试阶段,再迁移到生产阶段之后,我们在SQL Server 2012的生产实例中观察到一些奇怪的事情。

We have a number of stored procedures are called from a WCF web service to do various operations; 我们有许多从WCF Web服务调用的存储过程来执行各种操作; some return result sets, others do not. 有些返回结果集,有些则没有。

Some of these procedures call other (sub) procedures within them. 其中一些过程会调用其中的其他(子)过程。 These sub procedures have defined OUTPUT parameters. 这些子过程定义了OUTPUT参数。 When the parent procedures are called in dev and test, they execute as expected and the returned result set is the final select statement. 当在开发和测试中调用父过程时,它们将按预期执行,并且返回的结果集是最终的select语句。

But in our production environment, the parent procedures ARE running to completion when called, but instead of returning the expected result set like before, they return the sub procedure's OUTPUT parameter. 但是在我们的生产环境中,父过程在被调用时正在运行完成,但是它们没有返回像以前一样的预期结果集,而是返回了子过程的 OUTPUT参数。

Below is an example excerpt from one of our procedures that demonstrates the issue: 以下是我们演示其中一个过程的示例摘录:

CREATE PROCEDURE [dbo].[CHANGE_USER_DEPTID]

@USERID VARCHAR(10),
@DEPTID VARCHAR(10)

AS
BEGIN

SET NOCOUNT ON;

DECLARE @ALTERNATEUSERID VARCHAR(10)
DECLARE @EMPLID INT

EXEC GET_ALTERNATEUSERID_SP @USERID, @ALTERNATEUSERID OUTPUT

SELECT @EMPLID = EMPLID FROM ACTIVEDIRECTORY_VW WHERE ALTID = @ALTERNATEUSERID

...DO SOME VARIOUS PROCESSING...

UPDATE DEPARTMENT_TABLE
SET EMPLID = ...
WHERE ...

END

Ok, so this procedure's final statement is an update, and in our dev and test environments, when this CHANGE_USER_DEPTID procedure is called, it simply returns "Command(s) Completed Successfully". 好的,因此此过程的最终声明是更新,并且在我们的开发和测试环境中,当调用此CHANGE_USER_DEPTID过程时,它仅返回“命令已成功完成”。 But in our production environment, the procedure returns a result set; 但是在我们的生产环境中,该过程返回结果集; the @ALTERNATEUSERID which is the output parameter of the sub procedure. @ALTERNATEUSERID ,它是子过程的输出参数。 How is that possible? 那怎么可能? The main procedure is never even selecting that variable, and the main procedure doesn't even HAVE an output parameter defined. 主过程永远不会选择该变量,并且主过程甚至都没有定义输出参数。

This isn't a big deal on some of the calls in the WCF service, because the .NET method uses them in a cmd.ExecuteNonQuery() statement. 对于WCF服务中的某些调用,这并不是什么大问题,因为.NET方法在cmd.ExecuteNonQuery()语句中使用它们。 But it IS causing problems for some others where we're expecting the final select statement to return a certain result set at the end of the procedure (an integer, for example), but instead it returns the output parameter of the sub procedure (say, a string), which is causing various things to blow up further down the line. 但这会给其他一些人带来问题,我们期望最终的select语句在过程结束时返回某个结果集(例如,整数),但会返回子过程的输出参数(例如,是一个字符串),这会导致各种事情进一步恶化。

Has anyone ever experienced, or even heard of this issue? 有没有人经历过,甚至没有听说过这个问题?

To wrap this up: 总结一下:

It turned out that one of the child stored procedures inside the parent procedure (about three levels deep) had an extra SELECT statement at the end which someone had uncommented for testing purposes. 事实证明,父过程中的一个子存储过程(大约三层)在末尾有一个额外的SELECT语句,有人出于测试目的对此未加注释。 That extra select statement was being returned all the way up the stack in the topmost stored procedure. 在最顶层的存储过程中,该额外的select语句一直返回到堆栈的最上方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM