2014-09-04 162 views
-2
Declare @identifier nvarchar(100), 
     @identifier_New nvarchar(100) 
Declare identifier cursor for 
    select distinct Identifier01 
    from update_rules 
    where Identifier01 is not null 
      and Vendor='Bloomberg' 
      and [Geneva Code]='Geneva77' 

open identifier 

    fetch next from identifier into @identifier 

    while @@fetch_status=0 
    begin 
     set @identifier_New=upper(substring(@identifier,2,len(@identifier)-2)) 

     if exists(select * from INFORMATION_SCHEMA.columns where table_name='investment' and [email protected]_New) 
     begin 
      update i set i.[BBG Final Identifier]=case when [email protected] then @identifier_New end 
      FROM investment i,update_rules u 
      where isnull(i.AType,'0')=isnull(u.[Asset Type],'0') and 
        isnull(i.IType,'0')=isnull(u.[Investment Type],'0') and 
        isnull(i.Under_AType,'0')=isnull(u.[Under Lying Asset Type],'0') and 
        isnull(i.Under_IType,'0')=isnull(u.[Under Lying Investment Type],'0') and 
        u.Vendor='Bloomberg' and u.[Geneva Code]='Geneva77' 
     end 
     fetch next from identifier into @identifier 
    end 
close identifier 
deallocate identifier 

我得到一個錯誤,她SQL UPDATE查詢邏輯

update i set i.[BBG Final Identifier]=case when [email protected]**identifier** then @identifier_New end 
    FROM investment i,update_rules u 
+3

這看起來像SQL服務器。你應該適當地標記問題。 – 2014-09-04 12:24:58

+2

您能否請格式化您的代碼,以便它可讀並用實際的錯誤消息更新您的問題?我們無法幫助您解決一個模糊不清的問題。 – Josien 2014-09-04 12:25:12

+1

什麼是錯誤? – 2014-09-04 12:26:26

回答

0

這是您的查詢:

 update i set i.[BBG Final Identifier]=case when [email protected] then @identifier_New end 
     FROM investment i, update_rules u 
     where isnull(i.AType,'0')=isnull(u.[Asset Type],'0') and 
       isnull(i.IType,'0')=isnull(u.[Investment Type],'0') and 
       isnull(i.Under_AType,'0')=isnull(u.[Under Lying Asset Type],'0') and 
       isnull(i.Under_IType,'0')=isnull(u.[Under Lying Investment Type],'0') and 
       u.Vendor='Bloomberg' and u.[Geneva Code]='Geneva77'; 

首先,你應該使用顯式join S,而不是隱含在加入where條款。 SQL Server只允許從一個表進行更新,因此您不在set的受讓方處使用表別名。

試着寫了這樣的查詢:

update i 
    set [BBG Final Identifier] = (case when u.Identifier01 = @identifier 
             then @identifier_New 
            end) 
    from investment i join 
     update_rules u 
     on isnull(i.AType,'0') = isnull(u.[Asset Type],'0') and 
      isnull(i.IType,'0') = isnull(u.[Investment Type],'0') and 
      isnull(i.Under_AType,'0') = isnull(u.[Under Lying Asset Type],'0') and 
      isnull(i.Under_IType,'0') = isnull(u.[Under Lying Investment Type],'0') 
    where u.Vendor='Bloomberg' and u.[Geneva Code]='Geneva77'; 
+0

更新i set [BBG最終標識符] =(當u.Identifier01 = @標識符 然後i。@ identifier_New – user3751161 2014-09-04 12:33:59