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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:24:34+00:00 2026-05-25T01:24:34+00:00

I am working on a stored procedure that will trigger once a week so

  • 0

I am working on a stored procedure that will trigger once a week so I am not worried about speed as much as getting the thing to work.

What I am trying to do is loop through a table called households that stores information about the houses, then using the houseid looping through a table called individuals which will returns more than one person therefore leading me to loop through that result to gather information.

My main goal is to concatenate all the names of the people in the household, and their codes

declare @RowNum int,
    @houseId nchar(10), 
    @hid nchar(25) 

    select @houseId=MAX(ID) FROM households     --start with the highest ID 
    Select @RowNum = Count(*) From households      --get total number of records
    WHILE @RowNum > 0                          --loop until no more records BEGIN       

    select @hid = id from households where ID = @houseid    --get other info from that row  

    DECLARE @RowCount INT

    SET @RowCount = (SELECT COUNT(ID) FROM (select id, firstname,lastname,party_code from indviduals where householdid = @houseid) as table1)  
    DECLARE @code1 VARCHAR(50), @names1 VARCHAR(500), @lastname1 VARCHAR(50) 
    -- Declare an iterator
    DECLARE @I INT
    -- Initialize the iterator
    SET @I = (select min(id) from indviduals where householdid = @houseid)

    -- Loop through the rows of a table @myTable
    WHILE (@I <= @RowCount)
    BEGIN
            -- Declare variables to hold the data which we get after looping each record 
            DECLARE @code VARCHAR(50), @names VARCHAR(500), @lastname VARCHAR(50)    

            -- Get the data from table and set to variables
            SELECT @code = party_code, @names = firstname, @lastname = lastname FROM (select id, firstname,lastname,party_code from indviduals where householdid = @houseid) as table1  WHERE ID = @I
            -- Display the looped data



    set @code1 = (@code + ',' + @code1)
    set @names1 = (@names + ',' + @names1)
    set @lastname1 = @lastname
            -- Increment the iterator
            SET @I = @I  + 1
    END

    update households    
    SET firstnames=@names1, lastname=@lastname1, party_codes=@code1,
    where id = @houseid  

    select top 1 @houseId=ID 
    from households
    where ID < @houseID 
    order by ID desc --get the next one    

     set @RowNum = @RowNum - 1                              
     END 


household table
    1 bekshire st   dell    MA  10001   02639   50  0002    dell    NULL    ALRGEN  1   BERKSHIRE ST    NULL    NULL    NULL    NULL

individuals that belong to household id 10001
first    last     code
BOB BUILDER     U
JESS     BUILDER  A

i want 

1 bekshire st   dell    MA  10001   02639   50  0002    dell    NULL    ALRGEN  1   BERKSHIRE ST    BOB,JESS    BUILDER U,A 
  • 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-25T01:24:35+00:00Added an answer on May 25, 2026 at 1:24 am

    If you are having to concatenate data to get it into the table, then your design is wrong. Please read up on normalization. A concatenated list should not be stored in a field. Each field in each row of data should have no more than 1 piece of information. What you need are related tables.

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

Sidebar

Related Questions

I've been working on a Stored Procedure that checks the time, then retrieves records
I have a stored procedure that is working with a large amount of data.
I need to write a stored procedure that will provide the data from two
I am currently working on a project that will store specific financial information about
I am working on a stored procedure with several optional parameters. Some of these
I'm working on a stored procedure in SQL Server 2000 with a temp table
I am working on a Linq based CLR Stored Procedure for some complex filtering
Can someone provide a working example in which stored procedure returns a recordset and
I'm working on a pl sql stored procedure. What I need is to do
Is the following stored procedure code robust for multi-user application. It is working fine.

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.