下面是我的存储过程:
ALTER PROCEDURE [dbo].[IsRegionNameAllowed] (@RegionName VARCHAR(200),
@RegionId INT)
AS
BEGIN
--Region Name Duplicate Check
DECLARE @IsExist BIT
SET @IsExist=0
IF EXISTS(SELECT *
FROM (SELECT DISTINCT EntityID,
ResultObject.value('
declare default element namespace "http://schemas.datacontract.org/2004/07/LPSFS.Entity";
(/RegionQueueEntity/Region/RegionName)[1]', 'varchar(200)') AS RegionName
FROM TransactionLPS t
JOIN servistreammyqueue my
ON t.servistreamtransactionid = my.servistreammyqueueid
JOIN lkpqueuestatus lkp
ON my.queuestatusid = lkp.queuestatusid
WHERE SubContextID IN (SELECT ContextID
FROM lkpcontext
WHERE ContextCode = 'RGMCTX0002')
AND EntityID <> @RegionId) AS RegName
WHERE ( lower(RegName.RegionName) = lower(@RegionName)
AND lkp.queuestatuscode = 'QSTAPP' ))
SET @IsExist=1
END
但它在lkp.queuestatuscode中给出了一个错误(多部分标识符无法绑定)。 请帮忙
答案 0 :(得分:3)
lkp
是子查询中的别名。外部查询的WHERE
子句不再存在。如果要在外部WHERE
子句中查询它,请将其包含在子选择的SELECT
子句中,并观察子查询的别名是RegName
而不是{{1 }}:
lkp
此外,由于所有这些都在Select * from (
select distinct EntityID,
ResultObject.value('
declare default element namespace "http://schemas.datacontract.org/2004/07/LPSFS.Entity";
(/RegionQueueEntity/Region/RegionName)[1]','varchar(200)') as RegionName
,lkp.queuestatuscode
from TransactionLPS t
join servistreammyqueue my on t.servistreamtransactionid = my.servistreammyqueueid
join lkpqueuestatus lkp on my.queuestatusid = lkp.queuestatusid
where SubContextID in (Select ContextID from lkpcontext where ContextCode = 'RGMCTX0002')
And EntityID <> @RegionId) as RegName
Where (lower(RegName.RegionName)=lower(@RegionName) And RegName.queuestatuscode = 'QSTAPP'))
测试中使用,我会删除EXISTS
- 充其量,它是多余的,在最坏的情况下,它可能会导致系统产生不够优化计划。
答案 1 :(得分:0)
试试这个 -
ALTER PROCEDURE [dbo].[IsRegionNameAllowed]
(
@RegionName varchar(200)
, @RegionId int
)
AS BEGIN
DECLARE @IsExist BIT
SELECT @IsExist = 0
IF EXISTS(
SELECT 1
FROM (
SELECT DISTINCT
EntityID,
ResultObject.value('
declare default element namespace "http://schemas.datacontract.org/2004/07/LPSFS.Entity";
(/RegionQueueEntity/Region/RegionName)[1]', 'varchar(200)') AS RegionName
FROM dbo.TransactionLPS t
JOIN dbo.servistreammyqueue my ON t.servistreamtransactionid = my.servistreammyqueueid
JOIN dbo.lkpqueuestatus lkp ON my.queuestatusid = lkp.queuestatusid
WHERE EntityID <> @RegionId
AND lkp.queuestatuscode = 'QSTAPP'
AND SubContextID IN (
SELECT t2.ContextID
FROM lkpcontext t2
WHERE t2.ContextCode = 'RGMCTX0002'
)
) RegName
WHERE lower(RegName.RegionName) = lower(@RegionName)
) SELECT @IsExist = 1
END