2014-09-04 10 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 쿼리 LOGIC

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

이 SQL 서버처럼 보인다. 질문에 적절하게 태그를 지정해야합니다. –

+2

코드를 읽을 수 있도록 포맷하고 실제 질문과 함께 업데이트 할 수 있습니까? 우리는 불분명하고/읽을 수없는 질문으로 당신을 도울 방법이 없습니다. – Josien

+1

무엇이 오류입니까? –

답변

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

업데이트 [BBG 최종 식별자] = (u.Identifier01 = @identifier의 경우 i. @ identifier_New – user3751161