我有一個簡單的SQL查詢是這樣的:將普通SQL放入存儲過程時出現奇怪的SQL錯誤。錯誤:無效的對象名稱
SELECT
Users.userEmail AS registredEmail,
Status.statusName AS userActiveStatus,
Roles.roleName AS roleAssigned,
Users.userID AS uID
FROM
Users
INNER JOIN
Status ON Users.userStatus = Status.statusID
INNER JOIN
Roles ON Users.userRole = Roles.roleID
WHERE
(Users.companyID = 'D77VD')
由於where
條件是一個變量,把這個SQL存儲過程是這樣的:
CREATE PROCEDURE [usp_Users_12_CompanyUserGridview]
@companyID varchar(20)
AS
SELECT
Users.userEmail AS registredEmail,
Status.statusName AS userActiveStatus,
Roles.roleName AS roleAssigned,
Users.userID AS uID
FROM
Users
INNER JOIN
Status ON Users.userStatus = Status.statusID
INNER JOIN
Roles ON Users.userRole = Roles.roleID
WHERE
(Users.companyID = @companyID)
但奇怪的是與where
值「D77VD」平原SQL運行得很好,但是當我運行此存儲過程,並通過相同的值參數,它給我一個錯誤
Invalid object name 'Users'
+1對你來說,有同樣的問題,花了很多小時試圖得到什麼是錯的。 – avb