[SQL] The association in the MSSQL UPDATE statement

Source: Internet
Author: User

Update all the title in the TableA table with the TitleName in the TableB if there is a corresponding relationship in the TableB

Update  as a Set = (Selectfrom aswhere=b.xid)

The above notation cannot be performed in MSSQL, so

We can use a function-disguised script

incoming parameters are the same

Update TableA Set =fn_getxtitle (ID)

Fn_getxtitle

ALTER FUNCTION [dbo].[Fn_getxtitle](  @id NVARCHAR( -)) RETURNS  NVARCHAR( -) asBEGINDeclare @title nvarchar( -)    Set @title =SelectB.titlename fromTableB asBwhereB.xid=@idRETURN @titleEnd
incoming parameters are different

--Create FUNCTION function

Create FUNCTION [dbo].[Fn_getdatafromoldve](  @OLDVE NVARCHAR( -)    ,@Type NVARCHAR( -) ) RETURNS  NVARCHAR( -) asBEGINDeclare @BO_ID nvarchar( -)Declare @NEWVE nvarchar( -)Declare @PjtNO_X nvarchar( -)IF @TYPE ='M'    BEGIN  Set @NEWVE =(Select Top 1Newve from [XXXX].[dbo].[mig_mapping1] whereOldve=@OLDVE  andFIELDNAME='Pspix')  if @NEWVE  is  not NULL  begin   Set @BO_ID =(Select Top 1bo_id from [XXXX].[dbo].[Mig_order_base] WHEREorder_id=@NEWVE)   if @BO_ID  is  not NULL    begin    RETURN @BO_ID   End    Else   begin    RETURN @OLDVE   End        End  Else   begin    RETURN @OLDVE   End  ENDIF @TYPE ='P'   BEGIN  Set @NEWVE =(Select Top 1Newve from [XXXX].[dbo].[mig_mapping1] whereOldve=@OLDVE  andFIELDNAME='Pspid')  if @NEWVE  is  not NULL    begin    RETURN @NEWVE   End    Else   begin    RETURN @OLDVE   End       ENDIF @TYPE ='PX'   BEGIN  Set @PjtNO_X =(Select Top 1Newve from [XXXX].[dbo].[mig_mapping1] whereOldve=@OLDVE  andFIELDNAME='Pspix')      if @PjtNO_X  is  not NULL    begin    RETURN @PjtNO_X   End    Else   begin    RETURN @OLDVE   End      END  RETURN @OLDVEEND
--Executed script
 update  [   ". [ "  . [ tb_prjmethod   [ pjtno   [ dbo  ] . [ ]  (pjtno, " p   ' ) 

[SQL] The association in the MSSQL UPDATE statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.