如何在sql server中解析数组

时间:2016-01-28 17:56:36

标签: sql tsql sql-server-2014

我创建了一个如下所示的存储过程。

CREATE PROCEDURE [EXP_RfPickAfter] (
    @SESSIONVALUE xml,
    @WorkDSArray nvarchar(max),
    @UserSignonDS nvarchar(max),
    @Direction nvarchar(max),
    @ImmediateCount nvarchar(max),
    @CCWorkUnit nvarchar(max),
    @PartialPick nvarchar(max),
    @RETURNVALUE nvarchar(max) output
       )
      AS 
        SET NOCOUNT ON;
@workDSArray has this long string in it 

924WORK0924841SEQUENCE8410841842EQUIPMENT_LOC842842843PARENT_LOGISTICS_    
UNIT843843844REFERENCE_TYPE844Order844845INVENTORY_AT_
PD845845846FROMVER846None846847INTERNAL_NUM8476851842847848START_
DATE_TIME848848849INCHKDIG849849850TOTAL_VOLUME8500850851
TO_LOC851SHIP-1851852INTERNAL_NUM_TYPE852Shipment852853
WEIGHT_UM853LB853854COMPANY854SHOS854855QUANTITY_UM855CS855856OUTGOING_
PD_LOC856856857WORK_TYPE857LPNPick857858END_DATE_TIME858858859
ACTUALFROMWHS859L455859860LOT860860861WORK_ZONE1861861862
TOCONTAINERID8621862863INVER863863864TRACK_CONTAINERS864N864865
TO_LOC_INV_ATTRIBUTES_ID865865866CYCLE_COUNT866866867
LAUNCH_NUM86716949391867868ACTUALTOLOC868-jisavm3868869
FROM_CHECK_DIG869869870FROM_WHS870L455870871
WORK_GROUP871Picking871872INTERNAL_LINE_NUM187232612518872873
VOLUME_UM873873874ACTUALFROMLOC874NF-1-F-28-1874875
INTERNAL_REQ_NUM87520774185875876TO_WHS876L455876877
INTERNAL_LINE_NUM87732612518877878TREE_UNIT87826385537878879
INWRKZONE879879880PUTLOC880SHIP-1880881CONTAINER_ID88116489660881882
INTERNAL_CONTAINER_NUM88226385537882883FROMTRKCONT883Y883884
PARENT_CONTAINER_NUM8840884885CONVERTED_QTY_UM885CS885886
ACTUALTOWHS886L455886887OUTWRKZONE887887888
VERIFICATION_METH888None888889USER_DEF4889 L455-RS 2889890
REFERENCE_ID890XKJNNLJN890891LOGISTICS_UNIT89116489660891892
MESSAGE_ID892892893PICKLOC893NF-1-F-28-1893894
CURRENTLOC894F894895TREE_UNIT_ID89516489660895897
TOCONTAINERID089716489660897898TOTAL_WEIGHT8981898899
TOTAL_VALUE8990899900TO_CHECK_DIG900900901OUTCHECKDIG901901902
TO_QTY9020902903INTRKCONT903903904FROM_LOC_INV_ATTRIBUTES_ID904904905
INTERNAL_INSTRUCTION_NUM90520699485905906ITEM_DESC906MSF21D4MDE -       
Refrigerator906907ITEM907SHOS-STOCK907908
COMPLETEDBYUSER908jisavm3908909
TRANSPORT_CONT_ID90916489660909910OUTTRKCONT910910911ACCOUNT911
Store #9449911912INCOMING_PD_LOC912912913INVENTORY_TRACKING913Y913914
LOCATION_CLASS914Shipping Dock914915FROM_QTY9151915916OUTVERMETH916916917
CONVERTED_QTY9171917918QUANTITY9181918919
PARENT_INSTR91920699484919920
FROM_LOC920NF-1-F-28-1920921WORK_ZONE921W- 
Returns921922WORK_UNIT92216489660922923QTY
CONFIRMED9231923924925WORK9251925

我试图从这个字符串中解析containerID及其值,并在我的存储过程的select语句中使用它,但我不确定如何只解析containerID。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

这就是我为获得正确的ID所做的工作,我不得不触发别的东西。这不太理想,因为我不知道字符串是否会改变。如果有人知道更好的方式,请发帖并感谢@ymuribbi你帮了我一大堆。我编辑了这个以显示我的完整解决方案,以防其他人遇到此问题。

 DECLARE @WorkDSArray nvarchar(max);

    SET @WorkDSArray= '924WORK0924841SEQUENCE8410841842EQUIPMENT_LOC842842843PARENT_LOGISTICS_UNIT843843844REFERENCE_TYPE844Order844845INVENTORY_AT_PD845845846FROMVER846None846847INTERNAL_NUM8476851842847848START_DATE_TIME848848849INCHKDIG849849850TOTAL_VOLUME8500850851TO_LOC851SHIP-1851852INTERNAL_NUM_TYPE852Shipment852853WEIGHT_UM853LB853854COMPANY854SHOS854855QUANTITY_UM855CS855856OUTGOING_PD_LOC856856857WORK_TYPE857LPN Pick857858END_DATE_TIME858858859ACTUALFROMWHS859L455859860LOT860860861WORK_ZONE1861861862TOCONTAINERID8621862863INVER863863864TRACK_CONTAINERS864N864865TO_LOC_INV_ATTRIBUTES_ID865865866CYCLE_COUNT866866867LAUNCH_NUM86716949391867868ACTUALTOLOC868-jisavm3868869FROM_CHECK_DIG869869870FROM_WHS870L455870871WORK_GROUP871Picking871872INTERNAL_LINE_NUM187232612518872873VOLUME_UM873873874ACTUALFROMLOC874NF-1-F-28-1874875INTERNAL_REQ_NUM87520774185875876TO_WHS876L455876877INTERNAL_LINE_NUM87732612518877878TREE_UNIT87826385537878879INWRKZONE879879880PUTLOC880SHIP-1880881CONTAINER_ID88116489660881882INTERNAL_CONTAINER_NUM88226385537882883FROMTRKCONT883Y883884PARENT_CONTAINER_NUM8840884885CONVERTED_QTY_UM885CS885886ACTUALTOWHS886L455886887OUTWRKZONE887887888VERIFICATION_METH888None888889USER_DEF4889 L455-RS 2889890REFERENCE_ID890XKJNNLJN890891LOGISTICS_UNIT89116489660891892MESSAGE_ID892892893PICKLOC893NF-1-F-28-1893894CURRENTLOC894F894895TREE_UNIT_ID89516489660895897TOCONTAINERID089716489660897898TOTAL_WEIGHT8981898899TOTAL_VALUE8990899900TO_CHECK_DIG900900901OUTCHECKDIG901901902TO_QTY9020902903INTRKCONT903903904FROM_LOC_INV_ATTRIBUTES_ID904904905INTERNAL_INSTRUCTION_NUM90520699485905906ITEM_DESC906MSF21D4MDE - Refrigerator906907ITEM907SHOS-STOCK907908COMPLETEDBYUSER908jisavm3908909TRANSPORT_CONT_ID90916489660909910OUTTRKCONT910910911ACCOUNT911Store #9449911912INCOMING_PD_LOC912912913INVENTORY_TRACKING913Y913914LOCATION_CLASS914Shipping Dock914915FROM_QTY9151915916OUTVERMETH916916917CONVERTED_QTY9171917918QUANTITY9181918919PARENT_INSTR91920699484919920FROM_LOC920NF-1-F-28-1920921WORK_ZONE921W-Returns921922WORK_UNIT92216489660922923QTYCONFIRMED9231923924925WORK9251925'
    DECLARE @ContainerID integer;

    SET @ContainerID = CHARINDEX('TREE_UNIT_ID', @WorkDSArray); 

    DECLARE @ID nvarchar(max); 

    SET @ID = SUBSTRING(@WorkDSArray, @ContainerID,300)

    SELECT @ID;

    DECLARE @ActualContainerID nvarchar(max); 

    SET @ActualContainerID = SUBSTRING(@ID, 54, 9)

    SET @ActualContainerID = REPLACE(@ActualContainerID, CHAR(31), '')

    DECLARE @User integer

    DECLARE @UserID nvarchar(max) 

    SET @User = CHARINDEX('COMPLETEDBYUSER', @WorkDSArray);

    SET @UserID = SUBSTRING(@WorkDSArray, @User,27)

    DECLARE @ActualUserID nvarchar(max); 

    SET @ActualUserID = SUBSTRING(@UserID, 20, 8)

    SET @ActualUserID = REPLACE(@ActualUserID, CHAR(31), '')

    DECLARE @Shipment nvarchar(max)
    SET @Shipment = (SELECT CASE WHEN SH.COMPANY = 'SHOS' THEN '' ELSE SH.SHIPMENT_ID + '|' + 
SH."ROUTE" + '|' + 
SH."STOP" + '|' + 
SH.COMPANY + '|' + 
LEFT(SH.CUSTOMER_NAME,25) + '|' +
LEFT(SH.SHIP_TO_ADDRESS1,25) + '|' +
CASE WHEN SH.SHIP_TO_ADDRESS2 <> '' THEN SH.SHIP_TO_ADDRESS2 + '|' 
ELSE '' END +
LEFT(SH.SHIP_TO_CITY,25) + '|' +
LEFT(SH.SHIP_TO_STATE,25) + '|' +
LEFT(SH.SHIP_TO_POSTAL_CODE,25) END
FROM SHIPMENT_HEADER SH
JOIN SHIPPING_CONTAINER SC
ON SC.INTERNAL_SHIPMENT_NUM = SH.INTERNAL_SHIPMENT_NUM
WHERE SC.CONTAINER_ID = @ActualContainerID)

     DECLARE @ShipmentID nvarchar(max)

   SET @ShipmentID = SUBSTRING(@Shipment,0,9)



      --select top 1  device_name from dbo.DOCUMENT_ROUTING where document_type = 'HJBT_REC' and user_name = @user

     INSERT INTO [jbh].[PRT_REQ_WRK] 
     ([REQ_REF_TYP], [REQ_REF_I], [PRT_DVC_NM], [REQ_TMPL_NM], [REQ_VAL], [PRT_STT], [PRT_PRS_TYP], [PRT_Q], [CRT_S], [CRT_UID], [CRT_PGM_C], [LST_UPD_S], [LST_UPD_UID], [LST_UPD_PGM_C]) 
     VALUES (
     @ShipmentID, --[REQ_REF_TYP]
     '123abc', --[REQ_REF_I]
     'LB01885', --[PRT_DVC_NM]
     'HJBT_Shipping_Label.lbl', --[REQ_TMPL_NM]
     @Shipment, --[REQ_VAL]
     0, --[PRT_STT]
     'LABEL', --[PRT_PRS_TYP]
      1, --[PRT_Q]
      CURRENT_TIMESTAMP, --[CRT_S]
      @ActualUserID, --[CRT_UID]
     'WMSPrintService', --[CRT_PGM_C]
      CURRENT_TIMESTAMP, --[LST_UPD_S]
     'JISAVM3', --[LST_UPD_UID]
     'WMSPrintService') --[LST_UPD_PGM_C] 

答案 1 :(得分:-1)

正如@hogan在他的评论中所说,你可以使用字符串运算符来提取一些while和if-else语句

- 我正在编辑我的答案。

要检索容器ID,请使用如下的存储过程:

create proc getid (@string varchar(max)) as

declare @containerIdIndex int = (select charindex('CONTAINER_ID',@string))

declare @internalContainerIndex int = (select(Charindex('INTERNAL_CONTAINER', @string)))

declare @range int = @internalContainerindex-@containerIdindex+12
declare @start int = @containerIdIndex+12  --12 is to pass the 'Container_ID'

declare @containerId varchar(max)
set @containerId = substring(@string,@start,@range)
print @containerId