简体   繁体   中英

Combining Dynamic Tables Into One Query

Is there a clean way to combine the SQL below into 1 statement? The FilterID's can be zero-length, so the Filters table is optional. So, unless there is a better way to do it, I am "forced" to use the EXISTS logic below.

I Already Realize...
I already realize I can build a dynamic String and use the EXEC to run it...but that is slow.

The Document-to-DocumentMetadata relationship is: 1 Document to many DocumentMetadata 's
The Filter-to-DocumentMetadata relationship is: 1 Filter to many DocumentMetadata 's

The SQL Is As Follows:

   DECLARE @SearchTerms NVARCHAR(MAX)
    DECLARE @FilterIDs  VARCHAR(100)
    SET @SearchTerms        = '7%'
    SET @FilterIDs      = '12'
    ------------------------
    DECLARE @Filters TABLE (FilterID int)

    IF(@FilterIDs IS NOT NULL)
            INSERT INTO @Filters SELECT items AS INT FROM Split(@filterIDs, ',')

    IF EXISTS(SELECT FilterID FROM @Filters)
        BEGIN
            SELECT
                Document.ID AS DocumentID
                ,Document.SourceID
                ,Document.Name
                ,Document.Title
                ,Document.DocumentUrl
                ,Document.DocType
                ,Document.DocumentModifiedDate
                ,Document.Library
                ,DocumentMetadata.ID AS DocumentMetadataID
                ,DocumentMetadata.DocumentID
                ,DocumentMetadata.FilterID
                ,DocumentMetadata.Value
            FROM Document
            JOIN DocumentMetadata
                ON DocumentMetadata.DocumentID = Document.ID
            JOIN Filter
                ON Filter.ID = DocumentMetadata.FilterID
                AND Filter.ID IN (SELECT FilterID FROM @Filters)
            WHERE
                Document.Name LIKE @SearchTerms
                OR Document.Title LIKE @SearchTerms
            ORDER BY
                Document.Name, Document.Title
        END
    ELSE
        BEGIN
            SELECT
                Document.ID AS DocumentID
                ,Document.SourceID
                ,Document.Name
                ,Document.Title
                ,Document.DocumentUrl
                ,Document.DocType
                ,Document.DocumentModifiedDate
                ,Document.Library
                ,DocumentMetadata.ID AS DocumentMetadataID
                ,DocumentMetadata.DocumentID
                ,DocumentMetadata.FilterID
                ,DocumentMetadata.Value
            FROM Document
            JOIN DocumentMetadata
                ON DocumentMetadata.DocumentID = Document.ID
            JOIN Filter
                ON Filter.ID = DocumentMetadata.FilterID
            WHERE
                Document.Name LIKE @SearchTerms
                OR Document.Title LIKE @SearchTerms
            ORDER BY
                Document.Name, Document.Title
        END
JOIN Filter ON Filter.ID = DocumentMetadata.FilterID 
AND (Filter.ID IN (SELECT FilterID FROM @Filters) OR @FilterIDs is null) 

That should cover whether you have items in the @filters table or you had null come in for the filtersID (which drives the Filters table). In the case null came in the OR clause lets the record thru, so it relies on the other part of the join clause which is common between your statements. That should let you just use 1 select clause to cover both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM