2017-04-03 47 views
0

我正在嘗試運行基本的select語句以返回作業編號列表。然後,我想要拿出這份工作號碼清單,並通過4種不同的選擇陳述來查看我是否得到了任何結果。這一點是創建一個看門狗列表來通知我是否有任何作業的任何部分尚未創建。這裏是我的代碼至今:通過SQL Server循環選擇staement結果並將它們用於另一個SELECT查詢

--Search for all Jobs 
--Use select statement here to return a list of job numbers 


--Then crank through both of the following queries to see if I get any results 
--Laser 1 
SELECT * FROM db1.dbo.table1 
WHERE PARTNAME NOT IN (SELECT PARTNAME FROM db2.Laser1.dbo.Part WHERE ORDERNO = @JobNumber) 
AND DISTRICT = 1 AND ORDERNO = @JobNumber 

--Laser 2 
SELECT * FROM db1.dbo.table1 
WHERE PARTNAME NOT IN (SELECT PARTNAME FROM db2.Laser2.dbo.Part WHERE ORDERNO = @JobNumber) 
AND DISTRICT = 3 AND ORDERNO = @JobNumber 

總之我想用select語句返回當前在proccess的所有作業。然後用該作業替換@JobNumber並查看它是否返回任何內容。

+0

如果我是你,我會選擇工作數量爲#temp表並改變你的'ORDERNO = @ JobNumber'成'ORDERNO中(從#TEMP選擇jobnumber可以)' – user1935361

+0

樣本數據和期望結果會有幫助。 (只是關鍵部件) –

回答

0

這裏的細節非常稀疏,但你絕對不需要光標或循環。也許這樣的事情?

select * 
from Jobs j 
where j.PARTNAME not in 
(
    SELECT p1.PARTNAME 
    from db1.dbo.table1 
    left join db2.Laser1.dbo.Part p1 on p1.ORDERNO = @JobNumber AND DISTRICT = 1 
    UNION ALL 
    SELECT p2.PARTNAME 
    from db1.dbo.table1 
    left join db2.Laser2.dbo.Part p2 on p2.ORDERNO = @JobNumber AND DISTRICT = 3 
) 
相關問題