我有一个sql过程,可以从表中生成一些数据。我的问题是,有时它会生成带有错误的xml,更具体地说,它会为我的一个xml添加一个额外的字符。它始终是相同的标签,如果我删除标签,它会使用前一个标签。基本上在xml的相同位置,无论标签。这是我的sql代码:
ALTER proc [dbo].[genUserVehicleXML]
@userID nvarchar(450)
as
BEGIN
declare @IMEI nvarchar(138),@totalKM decimal(16,4),@funcH decimal(16,4), @stayH decimal(16,4), @totalH decimal(16,4),@dayKM decimal(16,4)
DECLARE contact_cursor CURSOR FOR
SELECT IMEI FROM ClientsIMEI where ApplicationUserId=@userID
OPEN contact_cursor;
-- Perform the first fetch.
FETCH NEXT FROM contact_cursor
into @IMEI;
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
select @totalKM=cast (sum(Kilometers)/cast(1000 as decimal(16,4)) as decimal(16,4)) from Properties where IMEI=@IMEI
select @dayKM=cast (sum(Kilometers)/cast(1000 as decimal(16,4)) as decimal(16,4)) from Properties where IMEI=@IMEI and day(Time_Stamp)=day(getdate())
select @funcH=cast (sum(Properties.Hours)/cast(3600 as decimal(16,4)) as decimal(16,4)) from Properties where IMEI=@IMEI and EngineStatus=1 and day(Time_Stamp)=day(getdate())
select @stayH=cast (sum(Properties.Hours)/cast(3600 as decimal(16,4)) as decimal(16,4)) from Properties where IMEI=@IMEI and EngineStatus=0 and day(Time_Stamp)=day(getdate())
select @totalH=cast (sum(Properties.Hours)/cast(3600 as decimal(16,4)) as decimal(16,4)) from Properties where IMEI=@IMEI
INSERT INTO dbo.XMLData
select top 1 P.IDProperty,p.ApplicationUserId,
P.IMEI,P.Time_Stamp,
P.Latitude,
P.Longitude,
P.Speed,
P.FuelLevel,
P.EngineStatus,
isnull(@dayKM,0),
C.InitialKilometers+@totalKM,
isnull(@funcH,0),
isnull(@stayH,0),
isnull(@totalH,0)+C.InitialHours,
isnull(@totalH,0),
P.Angle,
P.Altitude,
P.Satellites,
C.IDClientsIMEI,
C.CarNumber,
C.DriverName,
C.Model
from Properties P join ClientsIMEI C on P.IMEI=c.IMEI where P.ApplicationUserId=@userID and P.IMEI=@IMEI order by Time_Stamp desc
FETCH NEXT FROM contact_cursor into @IMEI;
END
CLOSE contact_cursor;
DEALLOCATE contact_cursor;
DECLARE @fileName NVARCHAR(1000)
DECLARE @sqlStr VARCHAR(1000)
DECLARE @sqlCmd VARCHAR(1000)
SET @fileName = 'C:\gps_beta01\wwwroot\outputXML_'+ @userID +'.xml'
SET @sqlStr = 'select [IDProperty],[ApplicationUserId], [IMEI],convert(varchar(50),cast([Time_Stamp] as datetime),20) as [Time_Stamp],[Latitude],[Longitude],
convert(numeric(16,0),cast([Speed] as float)) as Speed,convert(numeric(16,0),cast([FuelLevel] as float)) as FuelLevel,[EngineStatus],
convert(numeric(16,2),cast([Kilometers] as float))as Kilometers,[KilometersTotal],[FunctionHours],
[StationHours],[TotalHours],[TotalH],convert(numeric(16,0),cast([Angle] as float)) as Angle,convert(numeric(16,0),cast([Altitude] as float)) as Altitude,
[Satellites],[IDClientsIMEI] as [IDClients],replace([CarNumber],'' '','''') as [Number] ,[DriverName],[Model] from gpsTEST.dbo.XMLDATA for xml path (''Vehicle''), root (''Vehicles'')'
SET @sqlCmd= 'bcp "'+ @sqlStr+'" QUERYOUT '+@fileName+' -c -t, -T -S' + @@SERVERNAME;
EXEC Master..xp_CmdShell @sqlCmd
delete XMLData
END
每次只在那个位置发生这种情况。请帮助因为我不知道发生了什么。
更新: 这是与xml:https://www.dropbox.com/s/9b10s1x2l85l8lf/outputXML_ccbfcf7e-2086-41ef-9e98-ef0b0084c8f4.xml?dl=0
的链接答案 0 :(得分:1)
首先:这是一个非常难看的方法。根本不需要光标......解决这个基于集合的更好(也更容易!)...
但是对于你的问题:你的每次只在那个位置发生这种情况不是真的。看看你的照片:页面中间有一个1
。这并没有打破任何元素名称,但是 - 显然 - XML在某种程度上被破坏了部分,而这些部分被编号。我很确定,从开始到浮动1
的计数完全相同,因为从那里到2
正在破坏XML元素的名称。找出,如果这些数字已包含在XML文件中......
正如预期的那样你的DropBox链接指向有效的XML ...唯一不好的是换行符,它不应该在那里......看不到任何愚蠢的数字......它们会在某个地方进入你的文件......
默认情况下,行以换行符结束。您可以找到选项列表here: Specify Field and Row Terminators (SQL Server)。使用XML-BULK-export建议使用-r
或-N
(但不能-t
)
答案 1 :(得分:0)
解决方案是将-r
添加到bcp命令。
SET @sqlCmd= 'bcp "'+ @sqlStr+'" QUERYOUT '+@fileName+' -c -T -r -S' + @@SERVERNAME;
我在这里找到了解决方案:SQL Server Bcp Export XML Format