Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming you have something like this:</p> <ul> <li>city (string)</li> <li>state (string)</li> <li>zip (number / string)</li> <li>optional criteria (string items) : for the purposes of this example, I'm going to assume a software domain - say it's something like a television provider. So a sample option is: hasExpandedTVPackage, hasHBOpackage, etc as checkBoxes only provide a bool on/off piece of data.</li> </ul> <p>So for example, you have two checkbox items on the page: [ ] Also has Expanded Package [ ] Also has HBO. etc - basically a 'find all guys who live in city, state, zip (and if say HBO is selected), and also has paid for the HBO package'.</p> <p>Now, if you just wrote the SQL yourself, it would be "SELECT * FROM Users WHERE State = @State AND City = @City AND ZipCode = @ZipCode AND HasHboPackage = 1". But we would need to use dynamic SQL to put that together. </p> <p>My suggestion is to put together a table and create the extra bits of SQL there, so all you pass in to the stored proc are the IDs of the seleted items (no transmission of input data). Sample code:</p> <pre><code>DECLARE @Items TABLE ( ID INT, DisplayName VARCHAR(200), SQLStatement VARCHAR(200) ) INSERT INTO @Items (ID, DisplayName, SQLStatement) VALUES (1, 'Has Expanded Package', 'HasExpandedPackage = 1') INSERT INTO @Items (ID, DisplayName, SQLStatement) VALUES (2, 'Has HBO Package', 'HasHBOPackage = 1') SELECT * FROM @Items DECLARE @City VARCHAR(200) DECLARE @State VARCHAR(2) DECLARE @ZipCode VARCHAR(5) DECLARE @Statement VARCHAR(1000) DECLARE @SelectedOptions VARCHAR(30) SET @City = 'Dallas' SET @State = 'TX' SET @ZipCode = '12345' SET @SelectedOptions = '2' DECLARE @TempOptionsTable TABLE ( OptionID VARCHAR(3) ) DECLARE @Delimiter VARCHAR(1) DECLARE @StartPosition INT DECLARE @Length INT DECLARE @Item VARCHAR(3) SET @Delimiter = ',' WHILE LEN(@SelectedOptions) &gt; 0 BEGIN SET @StartPosition = CHARINDEX(@Delimiter, @SelectedOptions) IF @StartPosition &lt; 0 BEGIN SET @StartPosition = 0 END SET @Length = LEN(@SelectedOptions) - @StartPosition - 1 IF @Length &lt; 0 BEGIN SET @Length = 0 END IF @StartPosition &gt; 0 BEGIN SET @Item = SUBSTRING(@SelectedOptions, 1, @StartPosition - 1) SET @SelectedOptions = SUBSTRING(@SelectedOptions, @StartPosition + 1, LEN(@SelectedOptions) - @StartPosition) END ELSE BEGIN SET @Item = @SelectedOptions SET @SelectedOptions = '' END INSERT INTO @TempOptionsTable (OptionID) VALUES (@Item) END DECLARE @StatementTable TABLE ( StatementID INT IDENTITY, OptionID INT, SQLStatement VARCHAR(200) ) SELECT * FROM @StatementTable --SELECT I.* FROM @TempOptionsTable TOT INNER JOIN @Items I ON I.ID = TOT.OptionID INSERT INTO @StatementTable SELECT I.ID, I.SQLStatement FROM @TempOptionsTable TOT INNER JOIN @Items I ON I.ID = TOT.OptionID SELECT * FROM @StatementTable DECLARE @Iterator INT DECLARE @MaxIndex INT DECLARE @TempStatement VARCHAR(200) SELECT @Iterator = MIN(StatementID) FROM @StatementTable SELECT @MaxIndex = MAX(StatementID) FROM @StatementTable SELECT @Iterator, @MaxIndex SELECT @Statement = 'SELECT * FROM Users WHERE City = ''' + @City + ''' AND State = ''' + @State + ''' AND ZipCode = ''' + @ZipCode + '''' SELECT @Statement WHILE @Iterator &lt; (@MaxIndex + 1) BEGIN SELECT @TempStatement = SQLStatement FROM @StatementTable WHERE StatementID = @Iterator SET @Statement = @Statement + ' AND ' + @TempStatement SET @Iterator = @Iterator + 1 END SELECT @Statement --EXEC(@Statement) </code></pre> <p>This way, all you have to do is add a new record to your statement table and you instantly get the data binding and extra search features. You need to make sure the city state and zip code fields are properly cleansed though, as they could be potentially exploited to SQL injection if those are open text fields (and not drop downs or something). </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload