Question

SQL Server Management Studio 2005

I wrote a query that would allow the user to search on various categories such as Description, Country of Origin, Brand Name, and Grow Method. The user is able to input data for as many filters as they would like and the results returned would be based on what they inputted.

Below is a working version of this query; however this is how SSMS reformatted my original query that I wrote before. I need to try and figure out how I wrote it before so I can add two more columns to this filter:

    SELECT DISTINCT masterCorporateGtin.SupplierNo, masterCorporateGtin.GTIN, masterCorporateGtin.Description, masterCorporateGtin.Commodity, masterCorporateGtin.Variety, masterCorporateGtin.CoO, masterCorporateGtin.PackSize, masterCorporateGtin.BrandName, masterCorporateGtin.GrowMethod, masterCorporateGtin.Grade
    FROM         GTINs AS masterCorporateGtin INNER JOIN
        (SELECT DISTINCT SupplierNo, GTIN, Description, CoO, PackSize, BrandName, GrowMethod, Grade
        FROM          GTINs AS masterCorporateGtin
        WHERE      (Description LIKE '%' + @productDescription + '%') AND (BrandName LIKE '%' + @brandName + '%') AND
        (GrowMethod LIKE '%' + @growmethod + '%') AND (CoO = @countryoforigin) OR
        (Description LIKE '%' + @productDescription + '%') AND (BrandName LIKE '%' + @brandName + '%') AND (CoO = @countryoforigin) AND
        (@growmethod IS NULL) OR
        (Description LIKE '%' + @productDescription + '%') AND (BrandName LIKE '%' + @brandName + '%') AND
        (GrowMethod LIKE '%' + @growmethod + '%') AND (@countryoforigin IS NULL) OR
        (Description LIKE '%' + @productDescription + '%') AND (BrandName LIKE '%' + @brandName + '%') AND (@growmethod IS NULL) AND
        (@countryoforigin IS NULL) OR
        (Description LIKE '%' + @productDescription + '%') AND (GrowMethod LIKE '%' + @growmethod + '%') AND (CoO = @countryoforigin) AND
        (@brandName IS NULL) OR
        (Description LIKE '%' + @productDescription + '%') AND (CoO = @countryoforigin) AND (@growmethod IS NULL) AND
        (@brandName IS NULL) OR
        (Description LIKE '%' + @productDescription + '%') AND (GrowMethod LIKE '%' + @growmethod + '%') AND (@countryoforigin IS NULL) AND
        (@brandName IS NULL) OR
        (Description LIKE '%' + @productDescription + '%') AND (@growmethod IS NULL) AND (@countryoforigin IS NULL) AND
        (@brandName IS NULL) OR
        (BrandName LIKE '%' + @brandName + '%') AND (GrowMethod LIKE '%' + @growmethod + '%') AND (CoO = @countryoforigin) AND
        (@productDescription IS NULL) OR
        (BrandName LIKE '%' + @brandName + '%') AND (CoO = @countryoforigin) AND (@growmethod IS NULL) AND
        (@productDescription IS NULL) OR
        (BrandName LIKE '%' + @brandName + '%') AND (GrowMethod LIKE '%' + @growmethod + '%') AND (@countryoforigin IS NULL) AND
        (@productDescription IS NULL) OR
        (BrandName LIKE '%' + @brandName + '%') AND (@growmethod IS NULL) AND (@countryoforigin IS NULL) AND
        (@productDescription IS NULL) OR
        (GrowMethod LIKE '%' + @growmethod + '%') AND (CoO = @countryoforigin) AND (@brandName IS NULL) AND
        (@productDescription IS NULL) OR
        (GrowMethod LIKE '%' + @growmethod + '%') AND (@countryoforigin IS NULL) AND (@brandName IS NULL) AND
        (@productDescription IS NULL) OR
        (CoO = @countryoforigin) AND (@growmethod IS NULL) AND (@brandName IS NULL) AND (@productDescription IS NULL) OR
        (@growmethod IS NULL) AND (@countryoforigin IS NULL) AND (@brandName IS NULL) AND (@productDescription IS NULL))
        AS productFilter ON masterCorporateGtin.Description = productFilter.Description AND masterCorporateGtin.BrandName = productFilter.BrandName AND
        masterCorporateGtin.GrowMethod = productFilter.GrowMethod AND masterCorporateGtin.CoO = productFilter.CoO

From what I can remember all I did before was something similar to this below. I think I just kept adding statements in the WHERE clause one by one to see if they worked. I've tried this again but it didn't work. I'd appreciate any insight. Thank you.

    SELECT DISTINCT masterCorporateGtin.SupplierNo, masterCorporateGtin.GTIN,         masterCorporateGtin.Description, masterCorporateGtin.Commodity,         masterCorporateGtin.Variety, masterCorporateGtin.CoO, masterCorporateGtin.PackSize,         masterCorporateGtin.BrandName, masterCorporateGtin.GrowMethod, masterCorporateGtin.Grade
    FROM         GTINs AS masterCorporateGtin INNER JOIN
    (SELECT DISTINCT SupplierNo, GTIN, Description, Commodity, PackSize, BrandName,         GrowMethod, Grade
    FROM          GTINs AS masterCorporateGtin
    WHERE      (Description LIKE '%' + @productDescription + '%') OR
    (@productDescription IS NULL)) AS searchFilter ON masterCorporateGtin.Description =         searchFilter.Description
Was it helpful?

Solution

Something seems off with your parentheses... Generally you'd do it like such:

((@somefield IS NULL) OR (SomeField = @somefield))

For example, if I'm reading your query correctly, if @brandname is null, then all results will return. There seems to be a lot of redundant statements in your query and missing outer parentheses to group statements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top