如何在父查询中使用带引号的查询

问题描述 投票:0回答:1

我有一个用例,我需要更新列值,它本身就是一个查询。

表结构:

CREATE TABLE MyTable 
(
    ID int,
    Query varchar(max)
)

更新查询:

UPDATE MyTable  
SET Query = '{{child query}}' 
WHERE ID = @ID;

儿童查询:

DECLARE @ServiceIDsTableString AS varchar(20)  
SET @ServiceIDsTableString = @ServiceIDsString    

DECLARE @ServiceIDs TABLE (ServiceID INT PRIMARY KEY CLUSTERED)    
DECLARE @OrderID varchar(10), @Pos int    

SET @ServiceIDsTableString = LTRIM(RTRIM(@ServiceIDsTableString)) + ',' 
SET @Pos = CHARINDEX(',', @ServiceIDsTableString, 1)    

IF REPLACE(@ServiceIDsTableString, ',', '') <> ''  
BEGIN   
    WHILE @Pos > 0   
    BEGIN    
        SET @OrderID = LTRIM(RTRIM(LEFT(@ServiceIDsTableString, @Pos - 1))) 

        IF @OrderID <> ''    
        BEGIN     
            INSERT INTO @ServiceIDs (ServiceID) 
            VALUES (CAST(@OrderID AS int))     
        END    

        SET @ServiceIDsTableString = RIGHT(@ServiceIDsTableString, LEN(@ServiceIDsTableString) - @Pos)    
        SET @Pos = CHARINDEX(',', @ServiceIDsTableString, 1)   
    END  
END       

DECLARE @CurrentTime_UTC AS DateTime    
SELECT @CurrentTime_UTC = GETUTCDATE()      

DECLARE @ProviderTripIDs TABLE (ProviderTripID varchar(255))

INSERT INTO @ProviderTripIDs   
    SELECT ProviderTripID         
    FROM MfTrips mt WITH (nolock)   
    INNER JOIN Mfs m WITH (nolock) ON m.MfID = mt.MfID   
    INNER JOIN MfDatas md WITH (nolock) ON md.MfDataID = m.MfDataID   
    INNER JOIN locations lp WITH (nolock) ON mt.PickLocationID = lp.LocationID   
    INNER JOIN Locations ld WITH (nolock) ON mt.DropLocationID = ld.LocationID   
    INNER JOIN Customers c WITH (nolock) ON mt.CustomerID = c.CustomerID   
    INNER JOIN Reservations r WITH (nolock) ON r.ReservationID = mt.ReservationID  
    WHERE 
        m.active = 1    
        AND r.ParentReservationID = 0   
        AND m.date = @Date     
        AND mt.ProviderID = 1    
        AND mt.MfTripStatusID = 1    
        AND mt.ModeID = 3   
        AND mt.PassengerTypeID IN (SELECT PassengerTypeID 
                                   FROM PassengerTypes WITH (nolock) 
                                   WHERE CorePassengerTypeID = 1)     
        AND mt.ServiceID IN (SELECT ServiceID FROM @ServiceIDs)   
        AND md.MfDataTypeID = dbo.GetCurrentMfDataTypeID(m.Date)    
        AND (mt.WillCall = 0 OR mt.WillCallReadyAt >= '1/1/2000')    
        AND m.description = 'Unassigned'    
        AND mt.LevelOfServiceID = 1    
        AND mt.TripPurposeID NOT IN (1)   
        AND dbo.GetAge(c.BirthDate, m.Date) < 75    
        AND (dbo.GetAge(c.BirthDate, m.Date) > 18 or 
              1 = (SELECT TOP 1 1 
                   FROM MfTripExtraPassengers imtep WITH (nolock)  
                   WHERE imtep.MfTripID = mt.MfTripID 
                     AND imtep.Deleted_UTC IS NULL
                     AND dbo.GetAge(imtep.BirthDate, m.Date) > 18))
       AND (SELECT COUNT(*) 
            FROM MfTripExtraPassengers imtep WITH (nolock) 
            WHERE imtep.MfTripID = mt.MfTripID 
              AND imtep.Deleted_UTC IS NULL) <= 2   
       AND (SELECT COUNT(*) 
            FROM MfTripExtraPassengers imtep WITH (nolock) 
            WHERE imtep.MfTripID = mt.MfTripID 
              AND imtep.Deleted_UTC IS NULL
              AND mt.PassengerTypeID NOT IN (SELECT PassengerTypeID 
                                             FROM PassengerTypes WITH (nolock)
                                             WHERE CorePassengerTypeID = 1)) <=0    
       AND mt.C_EstMeters < 48280   
       AND mt.CustomerID NOT IN (SELECT CustomerID 
                                 FROM CustomerProviderExclusions 
                                 WHERE ProviderID = 112 
                                   AND Exclude = 1 
                                   AND RemovedAt_UTC IS NULL)      
       AND (mt.CustomerID IN (SELECT CustomerID 
                              FROM CustomerProviderExclusions 
                              WHERE ProviderID = 112 
                                AND Include = 1 
                                AND RemovedAt_UTC IS NULL
                                AND CustomerID = mt.CustomerID)    
        OR mt.CustomerID NOT IN (SELECT CustomerID 
                 from CustomerProviderExclusions where  Include = 1 and RemovedAt_UTC is null and CustomerID = mt.CustomerID)    )    and         case      when mt.ScheduledPickWindowBegin > m.Date then  mt.ScheduledPickWindowBegin AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22)     when mt.scheduledDropWindowEnd <CONVERT(NVARCHAR(30), DateAdd(dd,2,m.Date), 126)  then       case        when DateAdd(mi,-60,mt.scheduledDropWindowEnd) < DateAdd(ss,-1* ISNULL(mt.C_EstSeconds,0), mt.ScheduledDropWindowEnd) then DateAdd(mi,-60,mt.scheduledDropWindowEnd AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22))       else DateAdd(ss,-1* ISNULL(mt.C_EstSeconds,0), mt.ScheduledDropWindowEnd AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22))  end     else     m.Date AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22)    end          > DateAdd(mi,90,GetUTCDate())         update @ProviderTripIDs set ProviderTripID = SUBSTRING(ProviderTripID,0,11)    select   PickLatitude,   PickLongitude,   PickAdress,   DropLatitude,   DropLongitude,   DropAdress,   CustomerFirstName,   CustomerLastName,   '+'+Replace(Replace(Phone,'-',''),'+','') as [Phone],   CONVERT(NVARCHAR(30),[ScheduledPickTime], 126) as [ScheduledPickTime_ISO8601],   ProviderTripID  from  (  SELECT               cast(Round(lp.Latitude,7,1) as decimal(18,7)) as [PickLatitude]        ,cast(Round(lp.Longitude,7,1) as decimal(18,7)) as [PickLongitude]        ,RTrim(lp.Address1 + ' ' + lp.Address2) + ', ' + lp.City +', ' + lp.State+' ' + lp.Zip as [PickAdress]        ,cast(Round(ld.Latitude,7,1) as decimal(18,7)) as [DropLatitude]      ,cast(Round(ld.Longitude,7,1) as decimal(18,7)) as [DropLongitude]      ,RTrim(ld.Address1 + ' ' + ld.Address2) + ', ' + ld.City +', ' + ld.State+' ' + ld.Zip as [DropAdress]            ,c.FirstName as [CustomerFirstName]      ,c.LastName as [CustomerLastName]      ,ISNULL(        (         SELECT TOP 1 '1'+cc.Description         FROM CustomerContacts cc WITH(NOLOCK)         WHERE cc.Active = 1          AND cc.[Order] = 1          AND cc.CustomerID = mt.CustomerID       and lTrim(rTrim(cc.Description)) <> ''         ),          isnull(      ( SELECT TOP 1 '1'+cc.Description         FROM CustomerContacts cc WITH(NOLOCK)         WHERE cc.Active = 1          AND cc.CustomerID = mt.CustomerID            and lTrim(rTrim(cc.Description)) <> ''       order by [Order])      ,'19999999999')            ) as [Phone]      ,case      when mt.ScheduledPickWindowBegin > m.Date then  mt.ScheduledPickWindowBegin AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22)     when mt.scheduledDropWindowEnd <CONVERT(NVARCHAR(30), DateAdd(dd,2,m.Date), 126)  then       case        when DateAdd(mi,-60,mt.scheduledDropWindowEnd) < DateAdd(ss,-1* ISNULL(mt.C_EstSeconds,0), mt.ScheduledDropWindowEnd) then DateAdd(mi,-60,mt.scheduledDropWindowEnd AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22))       else DateAdd(ss,-1* ISNULL(mt.C_EstSeconds,0), mt.ScheduledDropWindowEnd AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22))  end     else     m.Date AT TIME ZONE (select Value from ClientSettings where ClientSettingID = 22)    end           as [ScheduledPickTime],     mt.ProviderTripID            FROM  MfTrips mt with (nolock)   INNER JOIN Mfs m  with (nolock)on m.MfID = mt.MfID   inner join MfDatas md with (nolock) on md.MfDataID = m.MfDataID   inner join locations lp with (nolock) on mt.PickLocationID = lp.LocationID   inner join Locations ld with (nolock) on mt.DropLocationID = ld.LocationID   inner join Customers c  with (nolock)on mt.CustomerID = c.CustomerID   inner join Reservations r with (nolock) on mt.ReservationID = r.ReservationID      where   m.active = 1    and m.date = @Date     and mt.ProviderID = 1    and mt.MfTripStatusID = 1    and mt.ModeID = 3   and substring(mt.ProviderTripID,0,11) in (Select ProviderTripID from @ProviderTripIDs)   and md.MfDataTypeID = dbo.GetCurrentMfDataTypeID(m.Date)       )a     order by ProviderTripID

子查询是一个很长的复杂查询,其中有多个引号。 这些子查询正在 SSMS(SQL Server Management Studio)中准备,以便稍后生成报告。

我正在做的是使用C#的Replace函数,将子查询的所有单引号替换为'',然后在父查询中使用它。

但我需要确保替换操作不会干扰原始子查询。

有没有更好的方法来执行此操作?

c# sql .net sql-server entity-framework
1个回答
0
投票

好的。 有一种本机方法可以在数据库中存储一堆 TSQL 语句并稍后执行它们。 它称为“存储过程”,您可以在互联网上了解它们:创建存储过程

© www.soinside.com 2019 - 2024. All rights reserved.