我有一个完美的动态TSQL查询。但是,在我像这样动态之前,我将它作为XML输出返回。
我现在如何对输出做同样的事情?我需要XML格式的结果。
ALTER PROCEDURE [dbo].[empowermentFetchSubmissions2]
@category INT=NULL, @department INT=NULL, @startDate DATE=NULL, @endDate DATE=NULL, @empID VARCHAR (60)=NULL, @submissionID INT=NULL, @inVoting INT=NULL, @pastWinners INT=NULL
AS
DECLARE @sSQL AS NVARCHAR (3000),
@Where AS NVARCHAR (1000) = ' (1=1) ';
BEGIN
SET NOCOUNT ON;
BEGIN
SET @sSQL = 'SELECT A.[submissionID],
A.[subEmpID],
A.[nomineeEmpID],
A.[nomineeDepartment],
CONVERT (VARCHAR (10), A.[submissionDate], 101) AS submissionDate,
A.[situation],
A.[task],
A.[action],
A.[result],
A.[timestamp],
A.[statusID],
A.[approver],
A.[approvalDate],
B.[FirstName] + '' '' + B.[LastName] AS nomineeName,
B.[ntid] AS nomineeNTID,
B.[qid] AS nomineeQID,
C.[FirstName] + '' '' + C.[LastName] AS submitName,
C.[ntid] AS submitNTID,
D.[categoryName],
(SELECT CAST
(CASE WHEN EXISTS (SELECT TOP (1) submissionID
FROM empowermentEntries
WHERE sessionID = (SELECT TOP (1) sessionID
FROM empowermentSessions
WHERE status = 1
AND CAST(GETDATE() as date) >= startDate
AND CAST(GETDATE() as date) <= endDate ) AND submissionID = A.[submissionID])
THEN ''true''
ELSE ''false''
END AS XML) AS inVoting)
FROM empowermentSubmissions AS A
INNER JOIN
empTable AS B
ON A.[nomineeEmpID] = B.[empID]
INNER JOIN
empTable AS C
ON A.[subEmpID] = C.[empID]
INNER JOIN
empowermentCategories AS D
ON A.[categoryID] = D.[catID]';
IF @category IS NOT NULL
SET @Where = @Where + ' AND A.[categoryID] = @_category';
IF @department IS NOT NULL
SET @Where = @Where + ' AND A.[nomineeDepartment] = @_department';
IF @startDate IS NOT NULL
SET @Where = @Where + ' AND A.[submissionDate] >= @_startDate';
IF @endDate IS NOT NULL
SET @Where = @Where + ' AND A.[submissionDate] <= @_endDate';
IF @empID IS NOT NULL
SET @Where = @Where + ' AND A.[nomineeEmpID] = @_empID';
IF @submissionID IS NOT NULL
SET @Where = @Where + ' AND A.[submissionID] = @_submissionID';
IF @inVoting IS NOT NULL
SET @Where = @Where + ' AND A.[submissionID] IN (SELECT submissionID
FROM empowermentEntries
WHERE sessionID = (SELECT TOP (1) sessionID
FROM empowermentSessions
WHERE status = 1
AND CAST(GETDATE() as date) >= startDate
AND CAST(GETDATE() as date) <= endDate )
AND submissionID = A.[submissionID])';
IF @pastWinners IS NOT NULL
SET @Where = @Where + ' AND A.[submissionID] IN (SELECT E.[submissionID]
FROM empowermentEntries as E
JOIN empowermentWinners as F
ON E.[entryID] = F.[entryID]
WHERE submissionID = A.[submissionID])';
IF LEN(@Where) > 0
SET @sSQL = @sSQL + ' WHERE ' + @Where;
EXECUTE sp_executesql @sSQL, N'@_category INT, @_department INT, @_startDate DATE, @_endDate DATE, @_empID VARCHAR(60), @_submissionID INT, @_inVoting INT, @_pastWinners INT', @_category = @category, @_department = @department, @_startDate = @startDate, @_endDate = @endDate, @_empID = @empID, @_submissionID = @submissionID, @_inVoting = @inVoting, @_pastWinners = @pastWinners;
END
END
答案 0 :(得分:1)
您需要获取现有的动态SQL,将FOR XML...
添加到末尾,将其包装在括号中,并将该值设置为用作sp_executesql
的OUTPUT的变量。例如:
DECLARE @SQL NVARCHAR(MAX),
@Results XML;
SET @SQL = N'
SET @Out = (SELECT * FROM sys.objects FOR XML AUTO);
';
EXEC sp_executesql @SQL, N'@Out XML OUTPUT', @Out = @Results OUTPUT;
SELECT @Results;
因此在顶部声明一个新变量:
DECLARE @Results XML;
然后在EXECUTE sp_executesql @sSQL...
行之前,执行以下操作:
SET @sSQL = N'SET @Results = (' + @sSQL + N' FOR XML {xml options});';
然后更新sp_executesql
的参数规范,最后包括:
N'@_category INT, ..., @Results XML OUTPUT'
并将以下内容添加到sp_executesql
:
@_category = @category, ..., @Out = @Results OUTPUT