猜测下面显示的SELECT
语句可能有什么问题;从存储过程中测试时返回null(输入没有问题),但测试在独立执行时通过。
SELECT
@JunitSlugs = ISNULL(@JunitSlugs, '') + ',' + JunitSlug + '-' +
(SELECT LookupValue
FROM Lookup
WHERE LookupId = JunitTypeId)
FROM
JunitList
WHERE
JunitId IN (SELECT JunitId
FROM CapsuleJunits
WHERE CapsuleId = @CapsuleId)
我不是SQL专家,但我知道可以有更好的方法来编写上面的SQL语句。请建议。
低于完成PROC
ALTER PROCEDURE [dbo].[usp_CapsuleList_GetReadyToPub] (@xmlDoc XML = NULL)
AS
BEGIN
BEGIN TRY
SET NOCOUNT ON
DECLARE @CapsuleId INT
DECLARE @PublicationIds VARCHAR(128)
DECLARE @JunitSlugs VARCHAR(MAX)
IF @xmlDoc IS NOT NULL
BEGIN
SELECT @CapsuleId = c.value('(CapsuleId)[1]', 'INT')
,@PublicationIds = c.value('(PublicationIds)[1]', 'VARCHAR(128)')
FROM @xmlDoc.nodes('MCapsuleList') AS T(C)
END
BEGIN
SELECT @JunitSlugs = ISNULL(@JunitSlugs, '') + ',' + JunitSlug + '-' + (
SELECT LookupValue
FROM Lookup
WHERE LookupId = JunitTypeId
)
FROM JunitList
WHERE JunitId IN (
SELECT JunitId
FROM CapsuleJunits
WHERE CapsuleId = 211
)
--SELECT @JunitSlugs = ISNULL(@JunitSlugs, '') + ',' + JunitSlug + '-' + (SELECT LookupValue FROM Lookup WHERE LookupId = JunitTypeId) FROM JunitList WHERE JunitId IN ( SELECT JunitId FROM CapsuleJunits WHERE CapsuleId = @CapsuleId)
SELECT @JunitSlugs = ISNULL(@JunitSlugs, '') + ',' + jl.JunitSlug + '-' + lu.LookupValue
FROM JunitList jl
INNER JOIN Lookup lu ON jl.JunitTypeID = lu.LookupID
WHERE jl.JunitId IN (
SELECT JunitId
FROM CapsuleJunits
WHERE CapsuleId = @CapsuleId
)
SELECT (
SELECT CL.[CapsuleId]
,[PublicationIds]
,[CapsuleSlug] AS Slug
,@JunitSlugs AS JunitIds
--,CreatedBy
,[Headline] AS HeadLine
--,[Excerpt]
--,[Correction]
,CL.UpdatedOn AS UpdatedOn
FROM CapsuleList CL
--LEFT JOIN [PublishUrls] PU ON PU.ParentId = CL.CapsuleId
WHERE CL.PublicationIds = @PublicationIds
AND CL.CapsuleStatusId = 53
ORDER BY CL.CapsuleId
FOR XML PATH('CapsuleList')
,TYPE
)
FOR XML PATH('CapsuleLists')
,ROOT('CapsuleListInfoByXml')
END
END TRY
BEGIN CATCH
DECLARE @ReturnedErrorID INT
EXECUTE @ReturnedErrorID = dbo.usp_HandleException
END CATCH
SET NOCOUNT OFF
END
答案 0 :(得分:3)
就更改SQL语句而言,您可以这样做(使用各种JOINS):
SELECT @JunitSlugs = ISNULL(@JunitSlugs, '') + ',' + jl.JunitSlug + '-' + lu.LookupValue
FROM JunitList jl
INNER JOIN Lookup lu ON jl.JunitTypeID = lu.LookupID
WHERE jl.JunitId IN (SELECT JunitId
FROM CapsuleJunits
WHERE CapsuleId = @CapsuleId)
在评论中,我们已经讨论了存储过程之外的SELECT
返回值,因此我删除了关于检查NULL
的部分答案。我还删除了有关您是否在存储过程中使用OUTPUT
参数的部分,因为您最近发布了存储过程的样子。可能关于存储过程的问题应该是它自己的单独问题,因为它涉及的不仅仅是原始问题中发布的初始查询。