将XML格式的数据写入一个临时表

本文展示了如何在SQL中通过DECLARE语句和sp_xml_preparedocument存储过程,将XML格式的数据解析并写入一个临时表#tmptblStockMovementHeader,用于处理库存移动记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)  SELECT @XML = N'<Root><tblStockMovementHeader>
<intRowNum>1</intRowNum><intSourceCurrentRowVersion>78001</intSourceCurrentRowVersion><intSourceSysID>1007</intSourceSysID><intCurrentRowVersion>78001</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>1007</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>AO00000003</MovementNo><MovementDate>2015-01-08T00:00:00</MovementDate><DocTypeCode>AO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABMLE=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-01-08T15:39:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-01-08T15:39:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>2</intRowNum><intSourceCurrentRowVersion>78046</intSourceCurrentRowVersion><intSourceSysID>1008</intSourceSysID><intCurrentRowVersion>78046</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>1008</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>AO00000004</MovementNo><MovementDate>2015-01-08T00:00:00</MovementDate><DocTypeCode>AO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABMN4=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-01-08T15:59:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-01-08T15:59:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader><intRowNum>3</intRowNum><intSourceCurrentRowVersion>105095</intSourceCurrentRowVersion><intSourceSysID>2007</intSourceSysID><intCurrentRowVersion>105095</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>2007</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000001</MovementNo><MovementDate>2015-03-03T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABmoc=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-03T12:47:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-03T12:47:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader><intRowNum>4</intRowNum><intSourceCurrentRowVersion>105106</intSourceCurrentRowVersion><intSourceSysID>2009</intSourceSysID><intCurrentRowVersion>105106</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>2009</SysID><MovementShopCode>MYSH001</MovementShopCode><OrgNo>TO00000001</OrgNo><MovementNo>TI00000001</MovementNo><MovementDate>2015-03-04T00:00:00</MovementDate><DocTypeCode>TI</DocTypeCode><MovementDirection>1</MovementDirection><MovementStatus>COMP</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>1</HandleVersion><FromShop>MYSH002</FromShop><ToShop>MYSH001</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABmpI=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-04T11:53:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-04T11:53:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>5</intRowNum><intSourceCurrentRowVersion>105109</intSourceCurrentRowVersion><intSourceSysID>2010</intSourceSysID><intCurrentRowVersion>105109</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>2010</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000002</MovementNo><MovementDate>2015-03-04T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABmpU=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-04T14:20:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-04T14:20:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>6</intRowNum><intSourceCurrentRowVersion>106007</intSourceCurrentRowVersion><intSourceSysID>3008</intSourceSysID><intCurrentRowVersion>106007</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>3008</SysID><MovementShopCode>MYSH001</MovementShopCode><OrgNo>TO00000002</OrgNo><MovementNo>TI00000002</MovementNo><MovementDate>2015-03-09T00:00:00</MovementDate><DocTypeCode>TI</DocTypeCode><MovementDirection>1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>1</HandleVersion><FromShop>MYSH002</FromShop><ToShop>MYSH001</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABnhc=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-09T11:04:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-09T11:04:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader><intRowNum>7</intRowNum><intSourceCurrentRowVersion>106010</intSourceCurrentRowVersion><intSourceSysID>3009</intSourceSysID><intCurrentRowVersion>106010</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>3009</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000003</MovementNo><MovementDate>2015-03-09T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABnho=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-09T15:41:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-09T15:41:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>8</intRowNum><intSourceCurrentRowVersion>106020</intSourceCurrentRowVersion><intSourceSysID>3011</intSourceSysID><intCurrentRowVersion>106020</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>3011</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000005</MovementNo><MovementDate>2015-03-10T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>2</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABniQ=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-10T10:17:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-10T10:17:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>9</intRowNum><intSourceCurrentRowVersion>106023</intSourceCurrentRowVersion><intSourceSysID>3012</intSourceSysID><intCurrentRowVersion>106023</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>3012</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000006</MovementNo><MovementDate>2015-03-10T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABnic=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-10T10:34:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-10T10:34:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>10</intRowNum><intSourceCurrentRowVersion>106025</intSourceCurrentRowVersion><intSourceSysID>3013</intSourceSysID><intCurrentRowVersion>106025</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>3013</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000007</MovementNo><MovementDate>2015-03-10T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABnik=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-10T10:46:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-10T10:46:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>11</intRowNum><intSourceCurrentRowVersion>106031</intSourceCurrentRowVersion><intSourceSysID>3014</intSourceSysID><intCurrentRowVersion>106031</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>3014</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000008</MovementNo><MovementDate>2015-03-10T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABni8=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-10T10:49:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-10T10:49:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader><tblStockMovementHeader>
<intRowNum>12</intRowNum><intSourceCurrentRowVersion>106014</intSourceCurrentRowVersion><intSourceSysID>3010</intSourceSysID><intCurrentRowVersion>106014</intCurrentRowVersion><strSourceLocationCode>MYSH001</strSourceLocationCode><strSourceMachineCode>01</strSourceMachineCode><SysID>3010</SysID><MovementShopCode>MYSH001</MovementShopCode><MovementNo>TO00000004</MovementNo><MovementDate>2015-03-10T00:00:00</MovementDate><DocTypeCode>TO</DocTypeCode><MovementDirection>-1</MovementDirection><MovementStatus>NEW</MovementStatus><MovementVersion>1</MovementVersion><HandleVersion>0</HandleVersion><FromShop>MYSH001</FromShop><ToShop>MYSH002</ToShop><LocationCode>MYSH001</LocationCode><MachineCode>01</MachineCode><MarkDel>0</MarkDel><CurrentRowVersion>AAAAAAABnh4=</CurrentRowVersion><IsSource>1</IsSource><AllowPublishUp>1</AllowPublishUp><AllowPublishDown>1</AllowPublishDown><SourceTier>99</SourceTier><SourceLocationCode>MYSH001</SourceLocationCode><SourceMachineCode>01</SourceMachineCode><CreateDate>2015-03-10T09:46:00</CreateDate><CreateBy>admin</CreateBy><UpDateDate>2015-03-10T09:46:00</UpDateDate><UpDateBy>admin</UpDateBy></tblStockMovementHeader></Root>'  
EXEC sp_xml_preparedocument @hDoc OUTPUT
, @XML  Select * Into #tmptblStockMovementHeader From OpenXML(@hDoc, 'Root/tblStockMovementHeader',3)  With (   [intRowNum] int,   [intSourceCurrentRowVersion] bigint,  [intSourceSysID] bigint,[intCurrentRowVersion] bigint, [strSourceLocationCode] nvarchar(30),[strSourceMachineCode] nvarchar(10),  [SysID] [int],  [MovementShopCode] [nvarchar](10),  [OrgNo] [nvarchar](50),  [MovementNo] [nvarchar](10),  [MovementDate] [smalldatetime],  [DocTypeCode] [nvarchar](50),  [MovementDirection] [int],  [DocRouteCode] [nvarchar](10),  [Remarks] [nvarchar](255),  [MovementStatus] [varchar](16),  [MovementVersion] [int],  [HandleVersion] [int],  [FromShop] [nvarchar](50),  [FromSupplier] [nvarchar](50),  [ToShop] [nvarchar](50),  [ToSupplier] [nvarchar](50),  [LocationCode] [nvarchar](30),  [MachineCode] [nvarchar](10),  [MarkDel] [int],  [CurrentRowVersion] [timestamp],  [ParentSysID] [int],  [ParentCurrentRowVersion] [bigint],  [SourceSysID] [int],  [SourceCurrentRowVersion] [bigint],  [IsSource] [smallint],  [AllowPublishUp] [smallint]
,  [AllowPublishDown] [smallint],  [SourceTier] [int],  [SourceLocationCode] [nvarchar](30),  [SourceMachineCode] [nvarchar](10),  [RecFromLocationCode] [nvarchar](30),  [RecFromMachineCode] [nvarchar](10),  [CreateDate] [smalldatetime],  [CreateBy] [varchar](50),  [UpDateDate] [smalldatetime],  [UpDateBy] [varchar](50)  )  
EXEC sp_xml_removedocument @hDoc  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值