Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7673737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:34:51+00:00 2026-05-31T16:34:51+00:00

I tried to make a dynamic SQL which gets all tasks for some users

  • 0

I tried to make a dynamic SQL which gets all tasks for some users

The procedure [GetAllSubExecutorsByUserId] returns IDs of all subalterns of curent user
I write these IDs into a temporary table, and after that I want to make a dynamic SQL to get all tasks from [tasks] table where “Executor” column has the value IN this temporary table

The query I wrote follows:

DECLARE @UserId VARCHAR(10) = 72;

DECLARE @tmp TABLE  ( Id VARCHAR(10));
INSERT @tmp exec [dbo].[GetAllSubExecutorsByUserId] @Source = @UserId;

DECLARE @SQL VARCHAR(max);

SELECT @SQL = 'SELECT * FROM tasks ';
SELECT @SQL = @SQL + 'WHERE Executor IN (' + (select Id from @tmp) + ')';

EXEC(@SQL);

But when i run it , it gives an error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

And i can’t understand how to fix it, because if i run the same query (which is not a dynamic SQL it works perfectly)

the query which works is static:

DECLARE @UserId VARCHAR(10) = 72;

DECLARE @tmp TABLE  ( Id VARCHAR(10));
INSERT @tmp exec [dbo].[GetAllSubExecutorsByUserId] @Source = @UserId;

SELECT * FROM tasks WHERE Executor IN (select Id from @tmp)

But I need a diynamic SQL…
Help me please to resolve this problem.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:34:52+00:00Added an answer on May 31, 2026 at 4:34 pm

    This is because you are trying to set a string value from a select that is returning more than one value. SQL does not know how to turn this list into a comma delineated one. In the prepared statement it is running as a true IN against a returned ‘table’, so it can run SET operators against that subquery table. In the dynamic query, you are essentially trying to create something like this, though:

    ...IN (1,2,3)
    

    You must rewrite the dynamic query like below to get it to run just like the prepared statement:

    'WHERE Executor IN (select Id from @tmp)'
    

    If you do this, then you will have to pass the table variable into the exec though, but you will have to use sp_executesql. So, your code becomes:

    DECLARE @UserId VARCHAR(10) = 72;
    
    DECLARE @tmp TABLE  ( Id VARCHAR(10));
    INSERT @tmp exec [dbo].[GetAllSubExecutorsByUserId] @Source = @UserId;
    
    DECLARE @SQL VARCHAR(max);
    
    SELECT @SQL = 'SELECT * FROM tasks ';
    SELECT @SQL = @SQL + 'WHERE Executor IN (select Id from @tmp)';
    
    DECLARE @SQLParamSetup VARCHAR(150);
    SET @SQLParamSetup = '@tmp TABLE  ( Id VARCHAR(10))'
    
    
    EXEC sp_executesql @SQL, @SQLParamSetup, @tmp ;
    

    However, another, cleaner option would be to use a temp table (not variable), as it will stay alive throughout your connection (even in child queries)

    DECLARE @UserId VARCHAR(10) = 72;
    
    CREATE TABLE #tmp  ( Id VARCHAR(10));
    INSERT INTO #tmp exec [dbo].[GetAllSubExecutorsByUserId] @Source = @UserId;
    
    DECLARE @SQL VARCHAR(max);
    
    SELECT @SQL = 'SELECT * FROM tasks ';
    SELECT @SQL = @SQL + 'WHERE Executor IN (select Id from #tmp)';
    
    EXEC @SQL ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to make ValidationAttribute which would check the database whether such an object.
I have a sp which builds a dynamic sql query based on my input
Im trying to make this dynamic email input field which uses autocomplete on a
On mouseenter I make an AJAX request to get some dynamic text based on
I wanted to make a dynamic method in which i pass any instance of
today i tried to make my listview a bit more dynamic. So i created
I am trying to make a form with some dynamic behavior. Specifically, I have
Tried to make a little old school ajax (iframe-javascript) script. A bit of mootools
I tried to make the title as clear as possible... here is my scenario:
I tried to make an independent copy of an array but couldnt get one.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.