简体   繁体   English

存储过程和SELECT语句返回不同的结果

[英]Stored Procedure and SELECT statement return different results

I have a stored procedure on a SQL Server 2008 database. 我在SQL Server 2008数据库上有一个存储过程。 The stored procedure is very simple, just a SELECT statement. 存储过程非常简单,只需一条SELECT语句即可。 When I run it, it returns 422 rows. 当我运行它时,它返回422行。 However, when I run the SELECT statement from the stored procedure, it returns 467 rows. 但是,当我从存储过程中运行SELECT语句时,它返回467行。 I've tried this by running both the stored procedure and the SELECT statement in the same SSMS window at the same time, and the behavior is the same. 我已经通过在同一SSMS窗口中同时运行存储过程和SELECT语句来尝试此操作,并且行为是相同的。 The stored procedure is: 存储过程为:

USE [REMS]
GO
/****** Object:  StoredProcedure [mobile].[GetAllMobileDeviceUsers]    Script Date: 12/04/2014 */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [mobile].[GetAllMobileDeviceUsers]
AS
SET NOCOUNT ON
SELECT 
ee.EmployeeID, 
EmployeeName = LastName + ', ' + FirstName
FROM EmployeeInvData ee
--UNION
--SELECT
--m.EmployeeID,
--EmployeeName = LastName + ', ' + FirstName
--FROM mobile.MiscPersonnel m
INNER JOIN Employee e
ON ee.EmployeeID = e.EmployeeID
UNION
SELECT
'-1',
'- Select An Employee -'
ORDER BY EmployeeName

When I do this in the same SSMS window: 当我在同一SSMS窗口中执行此操作时:

exec mobile.GetAllMobileDeviceUsers

SELECT 
ee.EmployeeID, 
EmployeeName = LastName + ', ' + FirstName
FROM EmployeeInvData ee
--UNION
--SELECT
--m.EmployeeID,
--EmployeeName = LastName + ', ' + FirstName
--FROM mobile.MiscPersonnel m
INNER JOIN Employee e
ON ee.EmployeeID = e.EmployeeID
UNION
SELECT
'-1',
'- Select An Employee -'
ORDER BY EmployeeName

I get two result sets. 我得到两个结果集。 The first is 422 rows; 第一是422行。 the second is 467 rows. 第二个是467行。 Why? 为什么?

This could have been caused by not qualifying your object names. 这可能是由于未限定对象名称引起的。 If objects are not fully qualified, SQL will assume they fall under the default schema. 如果对象不完全合格,则SQL将假定它们属于默认架构。

Like this 像这样

SELECT col1 FROM dbname.schemaname.tablename

Not this 不是这个

SELECT col1 FROM tablename

Check out this blog post by Aaron Bertrand for an in-depth explanation and example of this topic. 请查看Aaron Bertrand的博客文章,以获取有关此主题的深入说明和示例。

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

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