Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I dont know how to solve your question, however my alternative approach to your problem would instead be to have a stored procedure that accepted a comma delimited list of ID's, inserted those ID's into a temporary table and then do a "SELECT WHERE IN", for example (altered from another blog):</p> <pre><code>CREATE PROC dbo.SelectStudents ( @StudentIdList varchar(8000) ) AS BEGIN SET NOCOUNT ON CREATE TABLE #StudentIdList ( StudentId int, ) DECLARE @StudentId varchar(10), @Pos int SET @StudentIdList = LTRIM(RTRIM(@StudentIdList)) + ',' SET @Pos = CHARINDEX(',', @StudentIdList, 1) IF REPLACE(@StudentIdList, ',', '') &lt;&gt; '' BEGIN WHILE @Pos &gt; 0 BEGIN SET @StudentId = LTRIM(RTRIM(LEFT(@StudentIdList, @Pos - 1))) IF @StudentId &lt;&gt; '' BEGIN INSERT INTO #StudentIdList (StudentId) VALUES (CAST(@StudentId AS int)) END SET @StudentIdList = RIGHT(@StudentIdList, LEN(@StudentIdList) - @Pos) SET @Pos = CHARINDEX(',', @StudentIdList, 1) END END SELECT * FROM dbo.Students WHERE StudentId IN ( SELECT StudentId FROM #StudentIdList ) END </code></pre> <p>You can then call this stored procedure with a comma delimtied list of id's:</p> <pre><code>exec dbo.SelectStudents '1,2' </code></pre> <p>You will need to make sure that your formatted list of id's is no longer than 8000 characters (and make multiple calls to the database if it is) however this method will be far more efficient and requires only a large parameter, not a long command text - making large numbers of trips to the database to insert each of the Id's will be <strong>extremely</strong> slow.</p> <p>Similar approaches exist for passing in the list of Id's as Xml (although my preference would probably be to go for the simplicity of delimited strings).</p> <p>Also, it shoud be possible to use the above technique even if for whatever reason you aren't able to create stored procedures on your database.</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.
 

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