Reputation: 3111
I have a stored procedure on a SQL Server 2008 database. The stored procedure is very simple, just a SELECT statement. When I run it, it returns 422 rows. However, when I run the SELECT statement from the stored procedure, it returns 467 rows. 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. 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:
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; the second is 467 rows. Why?
Upvotes: 1
Views: 1305
Reputation: 6771
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.
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.
Upvotes: 4