Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this can't be done, when this is the only information you've got. The relation between the values of the parameters, is their position in their list. There is no option to relate and/or preserve these positions in SQL.</p> <p>What influence do you have on creating the sql-statement?</p> <p>If you can iterate over the lists in code and dynamically create the sql, you could come to a solution like this:</p> <pre><code>SELECT * FROM Gecoserv WHERE typeOfEcosystem = 'Freshwater' AND service = 'Habitat' UNION SELECT * FROM Gecoserv WHERE typeOfEcosystem = 'Freshwater' AND service = 'Food' UNION ... </code></pre> <p>For an example of how to parameterize this, have a look at <a href="https://stackoverflow.com/a/337792/410699">this answer</a>.</p> <p><strong>An alternative: splitting strings</strong></p> <p>Your main problem is to preserve the "rank"/"position" of the parameters in their lists. This can be done, by replacing your lists by strings and break them up.</p> <p>I used this <a href="https://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-reco">example of a splitting a string in sql</a>.</p> <p>The resulting query (made with <a href="http://www.sqlfiddle.com/#!3/d960c/12" rel="nofollow noreferrer">this Fiddle</a>) then looks like:</p> <pre><code>DECLARE @ecoSystems varchar(100) = 'Freshwater,Saltwater,Dunes', @services varchar(100) = 'Habitat,Food,Recreation', @separator char(1) = ',' SELECT [g].[id], [g].[typeOfEcoSystem], [g].[service] FROM [dbo].[Split](@separator, @ecoSystems) [e] INNER JOIN [dbo].[Split](@separator, @services) [s] ON [e].[position] = [s].[position] INNER JOIN [Gecoserv] [g] ON [e].[part] = [g].[typeOfEcoSystem] AND [s].[part] = [g].[service] ORDER BY [id] ASC </code></pre> <p><em>Will this work for your scenario?</em> (Answer: yes, almost...)</p> <p><strong>And finally without a function</strong></p> <pre><code>DECLARE @ecoSystems varchar(100) = 'Freshwater,Saltwater,Dunes', @services varchar(100) = 'Habitat,Food,Recreation', @separator char(1) = ','; WITH [EcoSystemsAndServices]([posE], [startE], [endE], [posS], [startS], [endS]) AS ( SELECT 1, 1, CHARINDEX(@separator, @ecoSystems), 1, 1, CHARINDEX(@separator, @services) UNION ALL SELECT [posE] + 1, [endE] + 1, CHARINDEX(@separator, @ecoSystems, [endE] + 1), [posS] + 1, [endS] + 1, CHARINDEX(@separator, @services, [endS] + 1) FROM [EcoSystemsAndServices] WHERE [endE] &gt; 0 ) SELECT [g].[id], [g].[typeOfEcoSystem], [g].[service] FROM [Gecoserv] [g] INNER JOIN [EcoSystemsAndServices] [ess] ON [g].[typeOfEcoSystem] = SUBSTRING(@ecoSystems, [startE], CASE WHEN [endE] &gt; 0 THEN [endE] - [startE] ELSE 100 END) AND [g].[service] = SUBSTRING(@services, [startS], CASE WHEN [endS] &gt; 0 THEN [endS] - [startS] ELSE 100 END) ORDER BY [id] ASC </code></pre>
    singulars
    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.
 

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