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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:40:22+00:00 2026-05-22T21:40:22+00:00

Below is pseudo-code for SQL Server stored procedure I need to write: int[] followers

  • 0

Below is pseudo-code for SQL Server stored procedure I need to write:

int[] followers = (select FollowerID from StoreFollower where StoreId = @storeId)

insert into Message (senderId, recipientId)
values (@senderId, followers[0])

int rootMessageId = last_id()

foreach (int follower in followers.Skip(1))
    insert into Message (senderId, recipientId, rootMessageId)
    values (@senderId, follower, rootMessageId

It gets all Store‘s follower IDs, creates a record in Message for the first one. Then it creates a Message for each subsequent follower ID, also specifying ID of the first Message record in the batch.

I need to convert this to SQL Server stored procedure, however I never wrote one before so I’m hesitant. Should I use a table variable to hold select result? Should I use arrays? What is the closest match to foreach here? How do I slice off the first element?

I would very much appreciate a sketch of such proc, just to know what to look further at.

  • 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-22T21:40:23+00:00Added an answer on May 22, 2026 at 9:40 pm

    My stab at it in T-SQL. I assume that (a) FollowerID is int, (b) @storeId and @senderID ar parameters of the stored procedure.

    DECLARE @FirstFollower int
    DECLARE @FirstMessage int
    
    --Get the first follower (i.e. smallest FollowerID, hopefully that's what you mean
    --otherwise you need an appropriate ORDER BY clause here
    SELECT @FirstFollower=TOP(1) FollowerId from StoreFollower 
    where StoreId = @storeId
    
    --Store message for first follower and get root message id
    INSERT INTO Message (senderId, recipientId)
    VALUES(@senderId, @FirstFollower)
    
    SELECT @FirstMessage=SCOPE_IDENTITY()
    
    --store a message per follower except the first. Same conditions apply here
    --regarding the order of the followers as in the first SELECT query
    INSERT INTO Message(senderId, recipientId, rootMessageId)
    SELECT @senderId, FollowerID, @FirstMessage
    FROM StoreFollower WHERE
    FollowerID <> @FirstFollower
    

    HTH

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

Sidebar

Related Questions

How would I achieve the pseudo-code below in JavaScript? I want to include the
Below is a stored procedure to check if there is a duplicate entry in
i've created a socket extension class. the pseudo code below tries to make a
I have a JSF page with two forms (pseudo-code below). First form is for
Below is the code i use to extract data from a table to a
Below is the code of a simple html with a table layout. In FF
Below are lines from the c++ programming language template<class T > T sqrt(T );
I've developed a couple of T-SQL stored procedures that iterate over a fair bit
This SQL statement example is very close to what I think I need... update
I'm using SQL Server 2005 and am logged in as sa . I would

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.