如何通过内连接改进存储过程的代码到可能的空tvp

时间:2015-11-03 13:17:45

标签: sql-server stored-procedures join

我想改进以下存储过程的代码。我想将它加入到单个select语句中。你能用更好的方式吗?

CREATE PROCEDURE [dbo].[pr_FinDocument_Filter]
  @finDocIdForFilter [dbo].[GuidList] READONLY,
  @filteredSid nvarchar(64),
  @filteringOffsetInDay int
AS
BEGIN

    IF (@filteredSid is null or @filteringOffsetInDay is null)
        BEGIN
            RAISERROR(N'arguments must have a value', 15, 1);
        END

    IF EXISTS (SELECT 1 FROM @finDocIdForFilter)
        BEGIN
            SELECT fin_doc_extra.docId
            FROM
                [CpsOther].[dbo].[FinDocumentExtra] AS fin_doc_extra
                INNER JOIN @finDocIdForFilter       AS fin_doc_for_filter
                    ON fin_doc_extra.docId = fin_doc_for_filter.Id
                    AND fin_doc_extra.sid = @filteredSid
            WHERE
                DATEDIFF(DAY, CONVERT(DATE, fin_doc_extra.value, 105), CONVERT(DATE, GETDATE(), 126)) = @filteringOffsetInDay
        END
    ELSE
        BEGIN
            SELECT fin_doc_extra.docId
            FROM
                [CpsOther].[dbo].[FinDocumentExtra] AS fin_doc_extra
            WHERE
                fin_doc_extra.sid = @filteredSid
                AND DATEDIFF(DAY, CONVERT(DATE, fin_doc_extra.value, 105), CONVERT(DATE, GETDATE(), 126)) = @filteringOffsetInDay
        END

END

0 个答案:

没有答案