如果只找到一条记录,或者两者都被添加,则确保不添加逗号

时间:2016-01-25 10:39:12

标签: sql-server tsql sql-server-2014-express

考虑一下SQL查询的后续部分,该查询旨在以XML的形式构造复杂的返回:

ISNULL(Logbook1 + ',', '') + ISNULL(Logbook2 + ',', '') + ISNULL(Logbook3 + ',', '') AS '@logBookNums',

本质上,这将搜索相应的记录。如果发现LogBook1LogBook2LogBook3没有记录,则会返回空字符串。如果它在LogBook1中找到一条记录,它将返回该记录和一个尾随逗号。

不幸的是,发送此XML的WCF服务令人震惊地记录在案(实际上没有读过任何内容),它拒绝使用尾随逗号的单个日志编号。

我的猜测是它应该提交为:

logBookNums="12345,,"

或者

logBookNums="12345"

如何更改上面的SQL行以输出一个或另一个,并且(如果查询找到两个日志编号)生成如下输出:

logBookNums="12345,12346,"

或者

logBookNums="12345,12346"

这是整个查询,我需要插入一个解决方案来处理@logBookNums和@landingDecNums的困境。

CREATE PROCEDURE dbo.CreateErsSalesAddSubmissionXmlByDateRange

-- Add the parameters for the stored procedure here

@uname VARCHAR(10) ,
        @pword VARCHAR(10) ,
        @sntype VARCHAR(1) ,
        @action VARCHAR(10) ,
        @salesContractRef VARCHAR(10),

        @auctionId NCHAR(10) ,
        @startDate  DATE,
        @endDate DATE


AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;

  -- Insert statements for procedure here


 SELECT
    RTRIM(@uname) AS '@uname',
    RTRIM(@pword) AS '@pword',

    (SELECT

        @snType AS '@snType',
        RTRIM(@action) AS '@action',
        COALESCE(@salesContractRef, '') AS '@salesContractRef',
        CONVERT(VARCHAR(10), DateOfPurchase, 112) AS '@saleDate',
        RTRIM(COALESCE(@auctionID, '')) AS '@auctionID',
        ISNULL(Logbook1 + ',', '') + ISNULL(Logbook2 + ',', '') + ISNULL(Logbook3 + ',', '') AS '@logBookNums',
        ISNULL(LandingDecNumber1 + ',', '') + ISNULL(LandingDecNumber2 + ',', '') + ISNULL(LandingDecNumber3 + ',', '') AS '@landingDecNums',
        COALESCE(VesselName, '') AS '@vesselName',
        RTRIM(VesselPLN) AS '@vesselPln',
        RTRIM(VesselOwner) AS '@vesselMasterOwner',
        COALESCE(CONVERT(VARCHAR(10), LandingDate1, 112), '') AS '@landingDate1',
        COALESCE(CONVERT(VARCHAR(10), LandingDate2, 112), '') AS '@landingDate2',
        COALESCE(CONVERT(VARCHAR(10), LandingDate3, 112), '') AS '@landingDate3',
        RTRIM(CountryOfLanding) AS '@countryOfLanding',
        RTRIM(PortOfLanding) AS '@landingPortCode',
      RTRIM(lh1.LandingId) AS '@internalRef',
        (SELECT

            COALESCE(RTRIM(SpeciesCode),'') AS '@speciesCode',
            RTRIM(FishingArea) AS '@faoAreaCode',
            COALESCE(RTRIM(IcesZone),'') AS '@ZoneCode',
            COALESCE(RTRIM(ld.DisposalCode),'') AS '@disposalCode',
            COALESCE(ld.FreshnessGrade,'') AS '@freshnessCode',
            COALESCE(ld.ProductSize,'') AS '@sizeCode',
            COALESCE(ld.PresentationCode,'') AS '@presentationCode',
            COALESCE(ld.PresentationState,'') AS '@stateCode',
            RTRIM(ld.NumberOfFish) AS '@numberOfFish',
            FORMAT(ld.Quantity, 'N2') AS '@weightKgs',
            FORMAT(Quantity * ld.UnitPrice, 'N2') AS '@value',
            COALESCE(ld.Currency,'') AS '@currencyCode',
            RTRIM(ld.WithdrawnDestinationCode) AS '@withdrawnDestinationCode',
            RTRIM(ld.BuyersRegistrationCode) AS '@buyerReg',
            RTRIM(ld.SalesContractRef) AS '@salesContractRef'

        FROM LandingDetails ld
        JOIN LandingHeaders lh
            ON ld.LandingId = lh.LandingId
        WHERE ld.LandingId = lh1.LandingId
        FOR XML PATH ('salesline'), TYPE)



    FROM LandingHeaders lh1

    WHERE lh1.AllocatedErsId IS NULL AND lh1.LandingDate1 BETWEEN @startDate     AND @endDate
    ORDER BY VesselName,lh1.LandingId

    FOR XML PATH ('salesnote'), TYPE)
FOR XML PATH ('ers')



END
GO

编辑(我在下面的回答中尝试过)

 SELECT
    RTRIM(@uname) AS '@uname'
   ,RTRIM(@pword) AS '@pword'
   ,(SELECT

        @snType AS '@snType'
       ,RTRIM(@action) AS '@action'
       ,COALESCE(@salesContractRef, '') AS '@salesContractRef'
       ,CONVERT(VARCHAR(10), DateOfPurchase, 112) AS '@saleDate'
       ,RTRIM(COALESCE(@auctionID, '')) AS '@auctionID'
       ,(
        SELECT ',' + CAST(LogbookX AS VARCHAR(100))
         FROM
        (
            VALUES(Logbook1),(Logbook2),(Logbook3)
        ) AS x(LogbookX)
        FOR XML PATH('')
    ),1,1,'') 
AS NumList(Concatenated)
WHERE NumList.Concatenated IS NOT NULL AS '@logBookNums'
                ,ISNULL(LandingDecNumber1 + ',', '') + ISNULL(LandingDecNumber2 + ',', '') + ISNULL(LandingDecNumber3 + ',', '') AS '@landingDecNums'
       ,COALESCE(VesselName, '') AS '@vesselName'
       ,RTRIM(VesselPLN) AS '@vesselPln'
       ,RTRIM(VesselOwner) AS '@vesselMasterOwner'
       ,COALESCE(CONVERT(VARCHAR(10), LandingDate1, 112), '') AS '@landingDate1'
       ,COALESCE(CONVERT(VARCHAR(10), LandingDate2, 112), '') AS '@landingDate2'
       ,COALESCE(CONVERT(VARCHAR(10), LandingDate3, 112), '') AS '@landingDate3'
       ,RTRIM(CountryOfLanding) AS '@countryOfLanding'
       ,RTRIM(PortOfLanding) AS '@landingPortCode'
       ,RTRIM(lh1.LandingId) AS '@internalRef'

其他修改有助于澄清以下答案中的评论

完整修改的查询现在看起来如下:

CREATE PROCEDURE dbo.CreateErsSalesAddSubmissionXmlByDateRange

-- Add the parameters for the stored procedure here

@uname VARCHAR(10),
@pword VARCHAR(10),
@sntype VARCHAR(1),
@action VARCHAR(10),
@salesContractRef VARCHAR(10),

@auctionId NCHAR(10),
@startDate DATE,
@endDate DATE


AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;

  -- Insert statements for procedure here


  SELECT
    RTRIM(@uname) AS '@uname'
   ,RTRIM(@pword) AS '@pword'
   ,(SELECT

        @snType AS '@snType'
       ,RTRIM(@action) AS '@action'
       ,COALESCE(@salesContractRef, '') AS '@salesContractRef'
       ,CONVERT(VARCHAR(10), DateOfPurchase, 112) AS '@saleDate'
       ,RTRIM(COALESCE(@auctionID, '')) AS '@auctionID'
       ,STUFF
        ((SELECT
            ',' + CAST(LogbookX AS VARCHAR(100))
          FROM (
          VALUES (Logbook1), (Logbook2), (Logbook3)
          ) AS x (LogbookX)
          FOR XML PATH (''))
        , 1, 1, '')
        AS '@logBookNums'

       ,ISNULL(LandingDecNumber1 + ',', '') + ISNULL(LandingDecNumber2 + ',', '') + ISNULL(LandingDecNumber3 + ',', '') AS '@landingDecNums'
       ,COALESCE(VesselName, '') AS '@vesselName'
       ,RTRIM(VesselPLN) AS '@vesselPln'
       ,RTRIM(VesselOwner) AS '@vesselMasterOwner'
       ,COALESCE(CONVERT(VARCHAR(10), LandingDate1, 112), '') AS '@landingDate1'
       ,COALESCE(CONVERT(VARCHAR(10), LandingDate2, 112), '') AS '@landingDate2'
       ,COALESCE(CONVERT(VARCHAR(10), LandingDate3, 112), '') AS '@landingDate3'
       ,RTRIM(CountryOfLanding) AS '@countryOfLanding'
       ,RTRIM(PortOfLanding) AS '@landingPortCode'
       ,RTRIM(lh1.LandingId) AS '@internalRef'
       ,(SELECT

            COALESCE(RTRIM(SpeciesCode), '') AS '@speciesCode'
           ,RTRIM(FishingArea) AS '@faoAreaCode'
           ,COALESCE(RTRIM(IcesZone), '') AS '@ZoneCode'
           ,COALESCE(RTRIM(ld.DisposalCode), '') AS '@disposalCode'
           ,COALESCE(ld.FreshnessGrade, '') AS '@freshnessCode'
           ,COALESCE(ld.ProductSize, '') AS '@sizeCode'
           ,COALESCE(ld.PresentationCode, '') AS '@presentationCode'
           ,COALESCE(ld.PresentationState, '') AS '@stateCode'
           ,RTRIM(ld.NumberOfFish) AS '@numberOfFish'
           ,FORMAT(ld.Quantity, 'N2') AS '@weightKgs'
           ,FORMAT(Quantity * ld.UnitPrice, 'N2') AS '@value'
           ,COALESCE(ld.Currency, '') AS '@currencyCode'
           ,RTRIM(ld.WithdrawnDestinationCode) AS '@withdrawnDestinationCode'
           ,RTRIM(ld.BuyersRegistrationCode) AS '@buyerReg'
           ,RTRIM(ld.SalesContractRef) AS '@salesContractRef'

          FROM LandingDetails ld
          JOIN LandingHeaders lh
            ON ld.LandingId = lh.LandingId
          WHERE ld.LandingId = lh1.LandingId
          FOR XML PATH ('salesline'), TYPE)



      FROM LandingHeaders lh1

      WHERE lh1.AllocatedErsId IS NULL
      AND lh1.LandingDate1 BETWEEN @startDate AND @endDate
      ORDER BY VesselName, lh1.LandingId

      FOR XML PATH ('salesnote'), TYPE)
  FOR XML PATH ('ers')



END
GO

当有logBookNums时,我们将获得正确格式化的xml,如下所示。

enter image description here

然而,当我们没有得到logBookNums =""但是你看,我们一无所获。

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以像这样尝试

你需要一个“IN”搜索,没有像************************************ Test Scenario No. 1 TestcaseID = FB_71125_1 dpSettingScript = FB_71125_1_DP.txt ************************************ Setting Pre-Conditions (DP values, Sqlite DB): cp /fs/images/nfs/FileRecogTest/MNT/test/Databases/FB_71125_1_device.sqlite $NUANCE_DB_DIR/device.sqlite "sync" twice. Starting the test: 0#00041511#0000000000# FILERECOGNITIONTEST: = testScenarioNo (int)1 = 0#00041514#0000000000# FILERECOGNITIONTEST: = TestcaseID (char*)FB_71125_1 = 0#00041518#0000000000# FILERECOGNITIONTEST: = dpSettingScript (char*)FB_71125_1_DP.txt = 0#00041520#0000000000# FILERECOGNITIONTEST: = UtteranceNo (char*)1 = 0#00041524#0000000000# FILERECOGNITIONTEST: = expectedEventData (char*)0||none|0||none = 0#00041528#0000000000# FILERECOGNITIONTEST: = expectedFollowUpDialog (char*) = 0#00041536#0000000000# FILERECOGNITIONTEST: /fs/images/nfs/FileRecogTest/MNT/test/main_menu.wav#MEDIA_COND:PAS_MEDIA&MEDIA_NOT_BT#>main_menu.global<#<FS0000_Pos_Rec_Tone><FS1000_MainMenu_ini1> 0#00041789#0000000000# FILERECOGNITIONTEST: Preparing test data done 0#00043768#0000000000# FILERECOGNITIONTEST: /fs/images/nfs/FileRecogTest/MNT/test/Framework.wav##>{any_device_name}<#<FS0000_Pos_Rec_Tone><FS1400_DeviceDisambig_<slot>_ini1> 0#00044008#0000000000# FILERECOGNITIONTEST: Preparing test data done 0#00045426#0000000000# FILERECOGNITIONTESTWARNING: expected >{any_device_name}<, got >lowconfidence1@FS1000_MainMenu< 1900#00046452#0000000000# FILERECOGNITIONTESTERROR: expected <FS0000_Pos_Rec_Tone><FS1400_DeviceDisambig_<slot>_ini1>, got <FS0000_Misrec_Tone><FS1000_MainMenu_nm1_004><pause300><FS1000_MainMenu_nm_001> 0#00046480#0000000000# FILERECOGNITIONTEST: Preparing test data done 0#00047026#0000000000# FILERECOGNITIONTEST: Stopping dialog immediately [VCALogParser] Scenario 1 FAILED.

这样的搜索
aNumber='123,345,678'

结果

DECLARE @tbl TABLE(Logbook1 INT,Logbook2 INT,Logbook3 INT);
INSERT INTO @tbl VALUES
 (12345,23456,56789)
,(234,NULL,NULL)
,(NULL,123,NULL)
,(NULL,NULL,NULL);

SELECT 'logbBookNums IN(' + NumList.Concatenated +')'
FROM @tbl
CROSS APPLY
(
SELECT STUFF
(
    (
        SELECT ',' + CAST(LogbookX AS VARCHAR(100))
         FROM
        (
            VALUES(Logbook1),(Logbook2),(Logbook3)
        ) AS x(LogbookX)
        FOR XML PATH('')
    ),1,1,'') 
)AS NumList(Concatenated)
WHERE NumList.Concatenated IS NOT NULL

答案 1 :(得分:0)

那么你可以在Case When结构中写它,它不是那么久:

CASE WHEN Logbook1 is not null
        THEN 
            CASE WHEN Logbook2 is not null
                THEN 
                    CASE WHEN Logbook2 is not null
                    THEN Logbook1+','+Logbook2+','+Logbook3
                    ELSE Logbook1+','+Logbook2
                ELSE  
                    CASE WHEN Logbook3 is not null
                    THEN Logbook1+','+Logbook3
                    ELSE Logbook1
        ELSE CASE WHEN ...