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 6899943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:29:07+00:00 2026-05-27T07:29:07+00:00

I created stored proc so that user can select multiple comma separated values into

  • 0

I created stored proc so that user can select multiple comma separated values into single parameter.

Here I am getting some problem in looping those values. here is the code

Alter PROCEDURE [dbo].[Testing] @Databases varchar(4096)
AS Begin

    SET NOCOUNT ON;

    Declare @cnt varchar(500)
    Declare @sql varchar(Max) = ''
    Declare @DB varchar(50)

    ;WITH MyCTE
        AS (SELECT * FROM [dbo].[fnSplitStringList] (@Databases))

    select * into #temp from MyCTE

    DECLARE tenant_cursor CURSOR FOR
        select * from #temp
        OPEN tenant_cursor;

    FETCH NEXT FROM tenant_cursor INTO @cnt;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        select @DB = dbname from CountryLookup where dbName = @cnt
        if @sql  = ''
        Begin
            SET @sql = 'select C1, C2, C3 
                from ' + @DB + '.dbo.T1 
                inner join DB1.T2 on T1.C4 = T2.C4
            '
        End
        Else
        Begin
            SET @sql = 'select C1, C2, C3 
                from ' + @DB + '.dbo.T1 
                inner join DB1.T2 on T1.C4 = T2.C4
            '
        End
        FETCH NEXT FROM tenant_cursor INTO @cnt;
    END

    CLOSE tenant_cursor;
    DEALLOCATE tenant_cursor;

    Drop table #temp
    exec (@sql)

END

Now when I execute my proc like exec [dbo].[Testing] ‘Usa,japan,France’

It is only giving me records for france.

Can some one help me where I am wrong.?

  • 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-27T07:29:07+00:00Added an answer on May 27, 2026 at 7:29 am

    I don’t recommend this approach, but if you look closely you’ll see that you’re never appending to @sql, merely assigning to it.

    Change your else clause to SET @sql = @sql + ....

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up a stored proc that (in part) drops a user.
Everytime a stored proc is ALTERed or CREATEd, I want to capture that in
I'm using a user defined table type to pass into my stored proc. I've
I need to create a stored proc that will return records (more than one
Got a strange problem created a little stored proc which need to execute a
I can't seem to find the syntax for editing an already-created stored procedure in
We have a 3rd-party app that wants to call a stored proc using SQL
So I've created a cursor on a temp table in a stored proc. However,
I have a stored proc which inserts rows from a view with ranks into
I have a stored proc that should conditionally update a bunch of fields in

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.