无法将xml数据插入表中

时间:2018-03-31 10:52:42

标签: sql-server sqlxml

当我尝试将值插入表中时,我收到以下错误。 我们无法删除XML中的空间,因为它是从Javascript生成的。 如何将以下数据插入XMLdata表。

从字符串转换日期和/或时间时转换失败。

这是样本数据(@ bbhdn5):

341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072;
261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073;
781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;

代码:

create table WMC_Savexmldata 
(
     XML nvarchar(max)
)

Declare @bbhdn5 nvarchar(max) 
set @bbhdn5='341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072; 261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073; 781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;'

insert into WMC_Savexmldata
    select @bbhdn5

2 个答案:

答案 0 :(得分:0)

虽然这个问题绝对具有误导性,但我的魔法水晶球突然开始眨眼,并告诉我,你可能正在寻找这个:

替换分隔符; and |允许将CSV字符串传输到此XML:

<x>
  <y>341300-02-1</y>
  <y>04/10/2018 01:18:29</y>
  <y>04/10/2018 06:18:29</y>
  <y>133072</y>
</x>
<x>
  <y> 261600-01-1</y>
  <y>04/10/2018 06:18:29</y>
  <y>04/10/2018 11:18:29</y>
  <y>133073</y>
</x>
<x>
  <y> 781100-R1-1</y>
  <y>04/10/2018 11:18:29</y>
  <y>04/10/2018 16:18:29</y>
  <y>133074</y>
</x>

我使用它来获取数据作为派生表:

DECLARE @bbhdn5 NVARCHAR(MAX); 
SET @bbhdn5=N'341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072; 261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073; 781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;';

WITH Splitted AS
(
    SELECT CAST('<x><y>' + REPLACE(REPLACE(@bbhdn5,'|','</y><y>'),';','</y></x><x><y>') + '</y></x>' AS XML) AS Casted
)
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber
        ,A.x.value('y[1]','nvarchar(max)') AS RowCode
        ,CONVERT(DATETIME,A.x.value('y[2]','nvarchar(max)'),103) AS Date1
        ,CONVERT(DATETIME,A.x.value('y[3]','nvarchar(max)'),103) AS Date2
        ,A.x.value('y[4]','int') AS SomeNumber
FROM Splitted
CROSS APPLY Casted.nodes('/x[y/text()]') AS A(x);

结果

+-----------+-------------+-------------------------+-------------------------+------------+
| RowNumber | RowCode     | Date1                   | Date2                   | SomeNumber |
+-----------+-------------+-------------------------+-------------------------+------------+
| 1         | 341300-02-1 | 2018-10-04 01:18:29.000 | 2018-10-04 06:18:29.000 | 133072     |
+-----------+-------------+-------------------------+-------------------------+------------+
| 2         | 261600-01-1 | 2018-10-04 06:18:29.000 | 2018-10-04 11:18:29.000 | 133073     |
+-----------+-------------+-------------------------+-------------------------+------------+
| 3         | 781100-R1-1 | 2018-10-04 11:18:29.000 | 2018-10-04 16:18:29.000 | 133074     |
+-----------+-------------+-------------------------+-------------------------+------------+

更新

将CTE 拆分中的行更改为此

    SELECT CAST('<x><y>' + REPLACE(REPLACE(REPLACE(REPLACE(@bbhdn5,CHAR(10),' '),CHAR(13),' '),'|','</y><y>'),';','</y></x><x><y>') + '</y></x>' AS XML) AS Casted

这将用空格替换CHAR(13)CHAR(10)。输入中的任何奇数换行都应该消失。好吧,你可能在字符串值中有一些额外的空白。但你可以再用单个空白替换双倍的空白......

答案 1 :(得分:0)

enter image description here插入WMC_Savexmldata 选择'496200-01-1 | 03/31/2018 11:18:29 | 03/31/2018 16:18:29 | 133015; 245000-01-1 | 03/31/2018  16:18:29 | 03/31/2018 21:18:29 | 133017; 262100-13-1 | 03/31/2018 21:18:29 | 04/01/2018 02:18:29 | 133018;'

请插入这样的数据(附件)。日期在一行中,时间在另一行,然后运行以下查询。由于日期和时间之间的空格错误,您将收到日期时间转换错误。我只想在日期之间休息。由于它来自浏览器,因此日期将在SQL中占用空间。请帮忙。你明白我的问题吗。

声明@InputSepTmp表 ( id int, 输入nvarchar(max) ) 插入@InputSepTmp 选择Row_Number()over(Order By(Select null))       ,LTrim(RTrim(B.i.value('(。/ text())1','varchar(max)'))) From(选择x = Cast(''+ replace((选择替换(XML,';','§§Split§§')为[*]对于XML路径('')),'§§Split§§' ,'')+''作为xml).query('。')  来自WMC_Savexmldata)作为A.  交叉应用x.nodes('x')AS B(i)

-- select * from @InputSepTmp
 --- Cursor   --------------------
        SET NOCOUNT ON
        DECLARE @InputID varchar(200)
         DECLARE cur_InputSeparator CURSOR
        STATIC FOR 
    select id from @InputSepTmp where Inputs <> ''
        OPEN cur_InputSeparator
        IF @@CURSOR_ROWS > 0
         BEGIN 
         FETCH NEXT FROM cur_InputSeparator INTO @InputID
         WHILE @@Fetch_status = 0
          BEGIN

          DEclare @FinalInputtmp table
          (
          id int,
          IPValues varchar(100)
          )

        insert into @FinalInputtmp
--        SELECT 

- Split.a.value('。','NVARCHAR(max)')AS String
 --FROM(选择
  - CAST(''+ REPLACE(LTRIM(RTRIM(输入)),',','')+''AS XML)AS String
  - 来自@InputSepTmp T1,其中id = @ InputID)AS A CROSS APPLY String.nodes('/ M')AS Split(a);

选择Row_Number()over(Order By(Select null))       ,LTrim(RTrim(B.i.value('(。/ text())1','varchar(max)')))  From(选择x = Cast(''+ replace((选择替换(LTRIM(RTRIM(输入)),'|','§§Split§§')[*]对于XML路径('')),' §§Split§§','')+''as xml).query('。')  从@InputSepTmp,其中id = @InputID)为A  交叉应用x.nodes('x')AS B(i)

--select convert(datetime,'04/12/2018 12:50:08')

insert into WMC_CriticalPath_ScheduledDtls (SWOPACKAGENO,TASKNO,SCHEDULEDSTDATE,SCHEDULEDENDDATE,TRACKID,CreatedDate,ModifiedDate)
 SELECT 'adjb', MAX(CASE WHEN D.RN=1 THEN LTRIM(RTRIM(D.IPValues)) END)[task no]
,MAX(CASE WHEN D.RN=2 THEN  LTRIM(RTRIM(D.IPValues)) END) [start date]
,MAX(CASE WHEN D.RN=3 THEN  LTRIM(RTRIM(D.IPValues)) END) [end date]
,MAX(CASE WHEN D.RN=4 THEN LTRIM(RTRIM(D.IPValues)) END) [id], Getdate(),NULL

FROM(     选择 *         ,ROW_NUMBER()OVER(ORDER BY(SELECT NULL))RN     来自@FinalInputtmp     )d

 delete from @FinalInputtmp

         FETCH NEXT FROM cur_InputSeparator INTO @InputID

         END
        END
        CLOSE cur_InputSeparator
        DEALLOCATE cur_InputSeparator
        SET NOCOUNT OFF

- 从WMC_CriticalPath_ScheduledDtls中选择*  从WMC_CriticalPath_ScheduledDtls

中选择*