This is the function I used to generate XML string. I think there are ways that I can make the function a little tighter, but it does the job and is too inefficient.
public static string BuildXmlString<t>(string xmlRootName, T[] values)
{
StringBuilder xmlString = new StringBuilder();
xmlString.AppendFormat("<{0}>", xmlRootName);
int count = values.Length;
for (int idx = 0; idx < count; idx++)
{
xmlString.AppendFormat("<value>{0}</value>", values[idx]);
}
xmlString.AppendFormat("</{0}>", xmlRootName);
return xmlString.ToString();
}
Here is the SQL that uses the XML and updates a table with a resolved parameter value.
DECLARE @status varchar(50), @demoItemIDs xml
SET @status = ''
-- Load up the table variable with the parsed XML data
DECLARE @DemoItems TABLE (ID int)
INSERT INTO @DemoItems (ID) SELECT ParamValues.ID.value('.','int')
FROM @demoItemIDs.nodes('/demoItemIDs/value') as ParamValues(ID)
-- Resolve status name to ID
DECLARE @statusID int
SELECT TOP 1 @statusID = DemoStatusID FROM DemoStatuses ds WHERE ds.Name = @status
IF (@statusID > 0)
BEGIN
-- Update the payments with the new status
UPDATE [Demo].[dbo].[DemoItemApprovals]
SET DemoStatusID = @statusID
WHERE DemoItemID IN (
SELECT ID FROM @DemoItems d
INNER JOIN DemoItemApprovals da
ON da.PaymentID = d.ID
)
END
ELSE
BEGIN
RAISERROR (
N'The status ''%s'' does not exist.' -- Message text.
, 10 -- Severity,
, 1 -- State,
, @status -- First argument.
);
END
No comments:
Post a Comment