1

嗨,下面是我的存储过程:

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 中给出错误,即(无法绑定多部分标识符)。请帮忙

4

2 回答 2

4

lkp是子查询中的别名。WHERE对于外部查询的子句,它不再存在。如果要在外部WHERE子句中查询它,则将其包含在子选择的子句中,并观察子查询的别名是,而不是SELECT的事实:RegNamelkp

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测试中使用,我会删除DISTINCT- 充其量是多余的,最坏的情况是它可能会导致系统产生一个不太理想的计划。

于 2013-04-23T07:13:33.317 回答
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
于 2013-04-23T07:20:05.987 回答