2011-07-08 60 views
0

我有兩個表如何查詢匹配兩個不同表中的記錄,如果匹配更新爲新值,則不匹配會提示我填寫詳細信息?

+---------------------------------+ 
|    Table A    | 
+----------+-----------+----+-----+ 
| Part No | Serial No | PO | DO | 
+----------+-----------+----+-----+ 
| 1AB1009 | GR7764 | ST | OND | 
| 1AB1009 | GR7765 | ST | OND | 
+----------+-----------+----+-----+ 

+-----------+ 
| Table B | 
+-----------+ 
| Serial No | 
+-----------+ 
| GR7764 | 
| GR7765 | 
+-----------+ 

表B具有獨特的序列號的只有一列,是匹配兩個表中的序列號和更新的值,如果有匹配......我的查詢運行不錯這但我有一個情景喜歡第一我上傳一個csv文件到表B,然後我會匹配...但表A可能沒有一些序列號,所以如果沒有匹配,它應該提示我去添加頁面添加記錄到表A,然後執行更新查詢。

這是我的查詢

update TableA 
    set Mat_No ='"+ Mat_No+"',WO_No='"+WO_No+"',Code = '"+Code+"', 
     Desc = '"+Desc+"',Center='"+Center+"',Date='"+Date+"',Remarks='"+Remarks+"' 
where SerialNo in(select A.SerialNo 
        from Table A, Table B 
        where B.SerialNo = A.SerialNo and A.Status = 'IN'); 

首先,我要上傳CSV文件表B,然後我填補像MATNO等所有的細節,然後更新如果CSV文件是具有序列號不屬於表那麼它應該提示我所有的序列號在表A中都不匹配......不知道該怎麼做?請幫我...

回答

0

下面是步驟,你可以這樣做:

  • 裝入的CSV文件(使用任何BCP,BULK INSERT,導入導出嚮導,SSIS包)裝載tableB的。該過程獨立於更新tableA。
  • 現在爲TableA創建一個更新觸發器,用於檢查B中存在的所有SNO,並在更新時檢查A中的NOT。請參閱下面的DDL和查詢爲例,並相應修改:
  • 
    
        create table TABLEA (
        PartNo varchar(30), 
        SNo varchar(30), 
        PO varchar(10), 
        DO varchar(30)) 
    
        insert into TABLEA 
        select '1AB1009', 'GR7764', 'ST', 'OND' 
        union 
        select '1AB1009','GR7765','ST','OND' 
    
        create table TABLEB ( 
        SNo varchar(30) 
        ) 
        insert into TABLEB 
        select 'GR7764' 
        union 
        select 'GR7765' 
    
        select * from TABLEA 
        select * from TABLEB 
        GO 
    
    

    現在創建的,而不是TableA上更新觸發警告SNOS試圖從前端應用程序插入時缺少表A

    
    
        CREATE TRIGGER missingSNOs ON TABLEA 
        INSTEAD OF UPDATE 
        AS 
    
         BEGIN 
          if EXISTS (SELECT 1 
              FROM TABLEB B 
              LEFT OUTER JOIN 
              INSERTED I 
              ON B.SNO = I.SNO 
              WHERE I.SNO IS NULL 
              ) 
          begin 
            SELECT B.SNO 
              FROM TABLEB B 
              LEFT OUTER JOIN 
              INSERTED I 
              ON B.SNO = I.SNO 
              WHERE I.SNO IS NULL 
           RAISERROR('S.nos are missing in tableA which are present in tableB',16,1); 
           ROLLBACK; 
          end  
         END 
        GO 
    
    

    如果測試觸發器觸發時,斯諾缺少

    
    
    -- Errors with message as the SNO is missing 
    update TABLEA 
    set PartNo = 'newPartNo' 
    where SNO = 'SnoNOTinB' 
    
    -- works no errors as both SNOS are present in tableB 
    update TABLEA 
    set PartNo = 'new one' 
    where SNO in ('GR7764', 'GR7765') 
    
    -- Also you dont have to join with tableB now and modify query as below 
    UPDATE A 
    set A.Mat_No ='"+ Mat_No+"',WO_No='"+WO_No+"', 
    Code = '"+Code+"',Desc = '"+Desc+"', 
    Center='"+Center+"', 
    Date='"+Date+"', 
    Remarks='"+Remarks+"' 
    FROM TableA A     
    WHERE A.Status = 'IN' 
    
    

    最後清理代碼

    
    
        drop table TABLEA 
         drop table TABLEB 
    
    
    +0

    感謝您的幫助...將嘗試這一點。 – sailaja