I am using XML EXPLICIT to generate an XML document that is sent to another application. The tags are Labels/Values and the order is important, since the other app will show the label and the value in the received order.
Th开发者_如何学JAVAis an example of the info I using
N | V
_______________
Objective - XXX
State - New York
Country - US
and I am expecting to get
<X>
<T>
<N>Objective</N>
<V>XXX</V>
</T>
<T>
<N>State</N>
<V>New York</V>
</T>
<T>
<N>Country</N>
<V>US</V>
</T>
</X>
however im getting them ordered alphabetically by the N tag
<X>
<T>
<N>Country</N>
<V>US</V>
</T>
<T>
<N>Objective</N>
<V>XXX</V>
</T>
<T>
<N>State</N>
<V>New York</V>
</T>
</X>
this is the query
CREATE PROCEDURE [dbo].[XML]
@iPackageId INT,
@iUserId INT
AS
BEGIN
SET NOCOUNT ON
DECLARE @iPluginId INT, @iIndex INT, @sLabel VARCHAR(100), @sValue VARCHAR(200), @sField VARCHAR(4000), @sQuery VARCHAR(8000)
DECLARE @MySettings TABLE(iIndex INT PRIMARY KEY IDENTITY, iSettingsId INT, sLabel VARCHAR(100), sField VARCHAR(4000))
DECLARE @Fields TABLE(sLabel VARCHAR(100), sValue VARCHAR(400))
SET @iPluginId = (SELECT iPluginId FROM trnPackage WHERE iPackageId = @iPackageId)
SET @iIndex = 1
SET @sQuery = ''
//WE get for the N and a sqlstatement to get the V from the catPluginSettings table, and order them by iDisplayOrder, and insert them in temp MySettings table.
//By executing that sql statement we get the actual value of for V, and save the N and V in @Fields table
INSERT INTO @MySettings
SELECT
iSettingsId,sLabel, sField
FROM catPluginSettings
WHERE iPluginId = @iPluginId
AND bActive = 1
ORDER BY iDisplayOrder
WHILE EXISTS (SELECT iIndex FROM @MySettings)
BEGIN
SELECT
@sLabel = sLabel,
@sField = sField + CAST(@iPackageId AS varchar)
FROM @MySettings T
WHERE iIndex = @iIndex
-- GET VALUE FROM sFiled
DECLARE @sResult NVARCHAR(400)
DECLARE @SelectStatement NVARCHAR(4000)
DECLARE @FullStatement NVARCHAR(4000)
DECLARE @ParameterList NVARCHAR(500)
SET @ParameterList = '@sResult NVARCHAR(400) OUTPUT'
SET @FullStatement = @sField
EXECUTE sp_executesql @FullStatement, @ParameterList, @sResult = @sResult OUTPUT
IF NOT @sLabel IS NULL AND NOT @sResult IS NULL BEGIN
INSERT INTO @Fields VALUES (@sLabel ,@sResult)
END
-- END GET VALUE FROM sField
DELETE @MySettings WHERE iIndex = @iIndex
SET @iIndex = @iIndex + 1
END
-- ROOT
SELECT distinct
1 as Tag,
NULL as Parent,
NULL [X!1],
BB.iBBId as [X!1!A!element],
T.sPckCode as [X!1!B!element],
SUBSTRING(cU.sNetLogin, CHARINDEX('\',cU.sNetLogin )+1, LEN(cU.sNetLogin)) as [X!1!C!element],
cUR.sFullName as [X!1!D!element],
CAST(DATEPART(YEAR, T.dCreationDate) AS VARCHAR)
+ dbo.PadLeft(CAST(DATEPART(MONTH, T.dCreationDate) AS VARCHAR),2,'0')
+ dbo.PadLeft(CAST(DATEPART(DAY, T.dCreationDate) AS VARCHAR),2,'0')
+ dbo.PadLeft(CAST(DATEPART(HOUR, T.dCreationDate) AS VARCHAR),2,'0')
+ dbo.PadLeft(CAST(DATEPART(MINUTE, T.dCreationDate) AS VARCHAR),2,'0')
AS [X!1!E!element],
cP.sAppShortName as [X!1!F!element],
CONVERT (DECIMAL (16, 2), T.mAmountUSD) as [X!1!G!element], ---> CAST a 2 Decimales <--
NULL AS [T!2!N!element],
NULL AS [T!2!V!element]
FROM trnPackage T
INNER JOIN trnPkgBB BB ON T.iPackageId = BB.iPackageId
INNER JOIN relPackageUser R ON T.iPackageId = R.iPackageId --AND R.bIsSysAdmin = 0 AND R.bIsBackupUser = 0
INNER JOIN catUser cU ON R.iUserId = cU.iUserId
INNER JOIN catUser cUR ON T.iUserId = cUR.iUserId
INNER JOIN catPlugin cP ON T.iPluginId = cP.iPluginId
WHERE (T.iPackageId = @iPackageId AND R.iUserId = @iUserId)
AND (BB.iPackageId = @iPackageId AND BB.iApproverId = @iUserId)
AND BB.iBBStatusId <> (SELECT iStatusId FROM catStatus WHERE sStatusName = 'Completed')
-- EXTRA INFO
UNION
SELECT
2 AS Tag,
1 AS Parent,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
sLabel,
sValue
FROM @Fields T
FOR XML EXPLICIT
select * from @Fields
END
I don't see an order by in your final output...
Without it you can't control the order the xml is generated in.
See this question if you don't know how to apply an order by to your union: SQL Query - Using Order By in UNION
精彩评论