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

  • SEARCH
  • Home
  • 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 8932843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:24:03+00:00 2026-06-15T09:24:03+00:00

My table icdClm is a junction table between a table with person demographics and

  • 0

My table icdClm is a junction table between a table with person demographics and another table that lists various codes of interest related to that person. I’d like to be able to join these tables a variable number of times. I’m not interested in the other table involved in the relationship because of simplicity’s sake at this point. I want a while loop to help generate the query (if there is a better way I’m open to a different implementation.)

Below is the desired output if the query were to run twice (two times for the two derived tables).

select distinct x1.patid
from
(
select ic.patid
    from icdClm as ic
    where ic.icd like '123%'
) x1 inner join 
(
select ic.patid
    from icdClm as ic
    where ic.icd like '456%'
)x2 on x1.patid=x2.patid
inner join 
(
select ic.patid
    from icdClm as ic
    where ic.icd like '789%'

) x3 on x1.patid=x3.patid

notice that they alias for the derived tables increases by 1 each time. Now for my code. This code does not generate an error, but I left it running for over 10 minutes and no results had yet been returned. Eventually, I would like to make another stored procedure dependent on this one that asks you the number of derived tables you want and then fills in the where ic.icd like '123%' with parameters inputted from the user, but first things first: what is not working with the below query?

declare 
@x int
,@y int
,@sql nvarchar(1000)

select 
@x=1
,@y=3
,@sql =
'select distinct x1.patid
from
(
select ic.patid
    from icdClm as ic
    where ic.icd like ''123%''
) x1'
while @x < @y
begin;
set @sql=@sql+ 
'inner join 
(
    select ic.pati
    from icdClm as ic
    where ic.icd like ''456%''
) x1 on x'+CAST(@y as CHAR(10))+'.patid=x1.patid'
set @y=@y+1
end;
print @sql
  • 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-06-15T09:24:04+00:00Added an answer on June 15, 2026 at 9:24 am
    while @x < @y
    begin;
    
    set @y=@y+1
    end;
    

    @y is what is incremented so this loop will either never execute (because y is less than x) or it will be an infinite loop* because @y will always be larger than @x

    perhaps you meant @x=@x+1

    *You’ll probably get an in integer overflow eventually

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

Sidebar

Related Questions

TABLE 1 is like following A B 1 x 2 y 3 z 4
table A's structure is a subset of table B, that means the table A's
Table Each row represents a video that was on air at particular time on
(Table)File has many (Table)Words FK Words.file_id related to a single File.id Default value of
I'm looking for another column that will give the overall total counts from each
Table one: products Which have fields like products_id products_price master_categories_id Table two: specials .
Table schema is set up something like this: userID Points timestamp 1 40 3
table : id (integer primary key) data (blob) I use mysql and sqlalchemy. To
Table --------------------------------------------- | id | first_name | middle_name | last_name | --------------------------------------------- | 1
Table structure id (p) | date | id2 (fk) | id3 (fk) where (p)

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.