このクエリをデバッグしようとしていますが、成功しません。以下のコードでは、Pt.pt_id_nbr をコメントアウトすると動作します。それ以外の場合、クエリは長時間実行され続けます。
Declare @sAlias char(1),
@sGQOrgId char(3),
@sID char(12),
@sType char(1),
@sSource char(2),
@dcLocationId numeric(18, 0) = NULL
set @sAlias='N'
set @sGQOrgId =''
Set @sID =''
Set @sType ='M'
Set @sSource ='MD'
SELECT DISTINCT
'' 'ImportID',d.GQOrgId, d.DeviceId
--,isnull(Pt.Pt_id_nbr,0)as Pt_id_nbr
--,Pt.pt_id_nbr works if commented out else the query keeps executing for a long time
, DeviceDesc = LTRIM(RTRIM(d.DeviceDesc)), d.Source,
SourceDesc = LTRIM(RTRIM(s.SourceDesc))
FROM GasQuality.dbo.Device AS d WITH (NoLock)
JOIN GasQuality.dbo.SourceCodes AS s WITH (NoLock)
ON s.Source = d.Source
left join elm_qa.elm.dbo.pt_sys_xref as Pt WITH (NoLock)
ON d.DeviceID = Pt.REF_VAL and (Pt.OLD_SYS_ID = 'PK' and Pt.VAL_TYPE_CD = 'OM' )
WHERE s.Type = @sType AND
d.GQOrgId LIKE RTRIM(@sGQOrgId) + '%'
AND d.Source LIKE RTRIM(@sSource) + '%'
AND d.DeviceId LIKE RTRIM(@sID) + '%'
ORDER BY d.GQOrgId, d.DeviceId, d.Source