无法将 xml 数据插入 table

Could not able to insert the xml data into a table

当我尝试将值插入 table 时出现以下错误。 我们不能删除 XML 中的 space,因为它是从 Javascript 生成的。 如何将以下数据插入 XML 数据 table。

从字符串转换日期 and/or 时间时转换失败。

这是示例数据(@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

尽管这个问题绝对具有误导性,但我的魔法 crystal 球突然开始闪烁并告诉我,您可能正在寻找这个:

替换分隔符 ; and | 允许将您的 CSV-string 转移到此 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>

我用它来获取派生的数据table:

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 Splitted 中的行更改为此

    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) 替换为空格。输入中的任何奇怪的换行符都应该消失。好吧,您可能在字符串值中有一些额外的空白。但是你可以再次用单空格替换双空格...

插入 WMC_Savexmldata select '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;'

请像这样插入数据(附件)。日期排在一行,时间排在另一行,然后 运行 下面的查询。由于日期和时间之间的 space 错误,您将收到日期时间转换错误。我只想在日期之间休息一下。由于它来自浏览器,因此日期在 SQL 中带有 space。请帮忙解决这个问题。你清楚我的问题了吗

声明@InputSepTmp table ( 身份证, 输入 nvarchar(max) ) 插入@InputSepTmp Select Row_Number() over (Order By (Select null)) , LTrim(RTrim(B.i.value('(./text())1', 'varchar(max)'))) 来自 (Select x = Cast('' + replace((Select replace(XML,';','§§Split§§') as [*] For XML Path('')),'§§Split§§','')+'' as 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 (SELECT
-- CAST ('' + REPLACE(LTRIM(RTRIM(Inputs)), ',', '') + '' AS XML) AS String
-- 来自@InputSepTmp T1 where id=@InputID) AS A CROSS APPLY String.nodes ('/M') AS Split(a);

Select Row_Number() over (Order By (Select null)) , LTrim(RTrim(B.i.value('(./text())1', 'varchar(max)'))) 从 (Select x = Cast('' + replace((Select replace(LTRIM(RTRIM(Inputs)),'|','§§Split§§') as [*] For XML Path('')),'§§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

从( SELECT * ,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

--select * 来自 WMC_CriticalPath_ScheduledDtls select * 来自 WMC_CriticalPath_ScheduledDtls