如何将此类似于C#的代码转换为SQL Server存储的proc

|| 下面是我需要编写的SQL Server存储过程的伪代码:
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
它获取所有
Store
的关注者ID,并在
Message
中为第一个创建记录。然后,它为每个后续跟随者ID创建一个“ 2”,同时指定批处理中第一个“ 2”记录的ID。 我需要将其转换为SQL Server存储过程,但是我之前从未写过任何存储过程,所以我很犹豫。我应该使用
table
变量来保存
select
结果吗?我应该使用数组吗?
foreach
最接近的匹配是什么?如何分割第一个元素? 我很欣赏这样一个过程的草图,只是想知道进一步研究什么。     
已邀请:

bab

        我在T-SQL中遇到了麻烦。我假设(a)“ 8”是“ 9”,(b)“ 10”和“ 11”是存储过程的参数。
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
高温超导     

要回复问题请先登录注册