2017-08-02 84 views
-1

我有一列存储select语句,并且我用一个数字标记每列,并将此数字存储在另一个表中。SQL Server根据条件在列中替换多个值_字符串

这是样本数据:

CREATE TABLE #Table 
(Cmd Nvarchar(Max)) 

INSERT #Table 
    SELECT 
     'select FacilityId as [Id] ,Fld00012 as [English Name] ,Fld00011 as [Arabic Name] ,TypeCode as [Facility Type] ,ParentFacilityId,Fld00027 as [Photo] From Facilities Where (((Facilities.TypeCode = ''Application'') OR (Facilities.TypeCode = ''Company'') OR (Facilities.TypeCode = ''Folder'') OR (Facilities.TypeCode = ''Module'') OR (Facilities.TypeCode = ''PhysicalView'') OR (Facilities.TypeCode = ''Query'') OR (Facilities.TypeCode = ''SheetWindow'') OR (Facilities.TypeCode = ''SSRS'') OR (Facilities.TypeCode = ''FacilityGenerator'') OR (Facilities.TypeCode = ''SystemMenu'') OR (Facilities.TypeCode = ''SetGenerator'') ) AND (Facilities.state != ''Removed'') ) ' 

UNION ALL 

SELECT 
    'select Lookup(ParentFacilityId,Company,Name_En) as [Company Name] ,Lookup(ParentFacilityId,Company,Name_Ar) as [Company Name Ar] ,Fld00068 as [StartDate] ,Fld00333 as [MonthDays] , Facilities.state as [State] ,Lookup(revised_by,User,Name_En) as [User Name En] ,Lookup(revised_by,User,Name_Ar) as [User Name Ar] ,FacilityId as [Id],ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where (( (Facilities.TypeCode = ''HRSetting'') AND (Facilities.state != ''Removed'') )) ' 

UNION ALL 

SELECT 
    'select FacilityId as [Id] ,Fld00011 as [Role Name Ar] ,Fld00012 as [Role Name] ,Fld00028 as [Description] , Facilities.state as [State] ,Lookup(revised_by,User,Name_En) as [User Name] ,Lookup(revised_by,User,Name_Ar) as [User Name Ar] ,ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where (( (Facilities.TypeCode = ''Role'') AND (Facilities.state != ''Removed'') )) ' 

UNION ALL 

SELECT 
    'select TypeCode as [Facility Type] ,FacilityId as [Id] ,ParentFacilityId as [ParentFacilityId] , Facilities.state as [State] ,Fld00011 as [Name Ar] ,Fld00012 as [Name En] ,Lookup(ParentFacilityId,Installation,Name_En) as [Installation Name En] ,Fld00027 as [Photo] From Facilities Where (( (Facilities.TypeCode = ''Company'') )) ' 

UNION ALL 

SELECT 
    'select FacilityId as [Id] ,Fld00011 as [Name Ar] ,Fld00012 as [Name] ,Fld00006 as [Birth Date] ,Lookup(Fld00039,CountryNation,Name_En) as [Nationality] ,Lookup(Fld00039,CountryNation,Name_Ar) as [CountryNation Name Ar] , Facilities.state as [State] ,Fld00001 as [User Name] ,ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where (( (Facilities.TypeCode = ''User'') AND (Facilities.state != ''Removed'') )) ' 

UNION ALL 

SELECT 
    'select FacilityId as [Id] ,Fld00012 as [Name En] ,Fld00011 as [Name Ar] , Facilities.state as [State] ,ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where (( (Facilities.TypeCode = ''Department'') AND (Facilities.state != ''Removed'') )) ' 

UNION ALL 

SELECT 
    'select Fld00011 as [Name Ar] ,Fld00012 as [Bank Name] , Facilities.state as [State] ,Fld00055 as [BankCode] ,Lookup(revised_by,User,Name_En) as [User Name] ,Lookup(revised_by,User,Name_Ar) as [User Name Ar] , Facilities.revised_at as [Revised At] ,FacilityId as [Id],ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where (( (Facilities.TypeCode = ''Bank'') AND (Facilities.state != ''Removed'') )) Order by Facilities.FacilityId DESC' 

UNION ALL 

SELECT 
    'select Fld00011 as [Name Ar] ,Fld00012 as [Name] , Facilities.state as [State] ,Lookup(revised_by,User,Name_En) as [User Name] ,Lookup(revised_by,User,Name_Ar) as [User Name Ar] , Facilities.revised_at as [Revised At] ,FacilityId as [Id],ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where ( (Facilities.TypeCode = ''Governorate'') AND (Facilities.state != ''Removed'') ) ' 

UNION ALL 

SELECT 
    'select FacilityId as [Id] ,Fld00011 as [Name Ar] ,Fld00012 as [Name] , Facilities.state as [State] ,Lookup(revised_by,User,Name_En) as [User Name] ,Lookup(revised_by,User,Name_Ar) as [User Name Ar] , Facilities.revised_at as [Revised At] ,ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where (( (Facilities.TypeCode = ''sponsor'') AND (Facilities.state != ''Removed'') )) ' 

UNION ALL 

SELECT 
    'select Fld00011 as [Name Ar] ,Fld00012 as [Name En] , Facilities.state as [State] ,FacilityId as [Id],ParentFacilityId,TypeCode as [Facility Type],Fld00027 as [Photo] From Facilities Where ( (Facilities.TypeCode = ''License'') AND (Facilities.state != ''Removed'') ) ' 

SELECT cmd FROM #Table 

列数表:

CREATE TABLE #FieldNumber 
(Old_Fld_No bigInt, New_Fld_NO BigInt) 

INSERT INTO #FieldNumber 
VALUES (11, 1320), (12, 1450), 
     (27, 1260), (68, 2065) 

Figure 1

Figure 2

我需要什么来代替每个FldNo000FldNo000 [Old_Fld_No] [New_F ld_No]

例如:Fld00012 ==> Fld0001450

,并做了所有行

回答

0

希望这是你在找什么:

begin 
    declare pcursor cursor for select distinct old_fld_no,new_fld_no from 
    fieldnumber ; 
    declare @old_fld_no Bigint; 
    declare @new_fld_no Bigint; 
    open pcursor 
     fetch next from pcursor into @old_fld_no,@new_fld_no 
     while (@@FETCH_STATUS <> -1) 
      begin 
       UPDATE TAB 
       SET CMD =REPLACE(CMD,@OLD_FLD_NO,@NEW_FLD_NO) 
       fetch next from pcursor into @old_fld_no,@new_fld_no 
      end 
    close pcursor 
    deallocate pcursor 
end