2017-02-13 77 views
1

數據我已經在ColdFusion中以下內容:SQL Server 2008中查詢移動臺從一個到另一個

<cfquery name="queryInRange" datasource="Database1" > 
     select STOCK 
     from CAR 
     WHERE (CAR_INV_DATE between '#dateStartDate#' and '#dateEndDate#') 
    </cfquery> 

    <cfoutput query="queryInRange"> 

     <cfquery name="INSStocks" datasource="Database1" > 
      if not exists (select strStockNumber from Aggregates WHERE strStockNumber='#STOCK#') 
      BEGIN 
       insert into Aggregates (strStockNumber) 
        VALUES('#STOCK#') 
       END 
     </cfquery> 

    </cfoutput> 

這裏的想法是從一個表,供應商臺拉場的股票,並確保它具有在第二個表中記錄,然後我們添加一些信息。我想知道,有沒有辦法在sql中這樣做,所以有cf循環?

回答

3

是的,有一種方法可以用sql來完成。其實有不少。這是一個。

insert into table2 
(field1, field2, etc) 
select value1, value2, etc 
from otherTables 
etc 
except 
select value1, value2, etc 
from table1 
+0

EXCEPT與我的查詢中的IF NOT EXISTS的工作方式不同嗎? –

+1

閱讀[Except文檔](https://msdn.microsoft.com/en-us/library/ms188055.aspx)瞭解它是如何工作的。 – Leigh

+0

正確的,它說:「從查詢中返回EXCEPT運算符左側的任何不同的值,這些值也不是從正確的查詢中返回的。」我試圖理解它與NOT EXISTS的不同之處「指定要測試的子查詢對於行的存在。「 –

相關問題