如何将类似 C# 的代码转换为 SQL Server 存储过程
下面是我需要编写的 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 创建一条 Message
,同时指定批次中第一个 Message
记录的 ID。
我需要将其转换为 SQL Server 存储过程,但我以前从未编写过,所以我很犹豫。我应该使用 table
变量来保存 select
结果吗?我应该使用数组吗?这里与 foreach
最接近的匹配是什么?如何切掉第一个元素?
我非常感谢这样的过程的草图,只是为了知道要进一步看什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 T-SQL 中的尝试。我假设 (a)
FollowerID
是int
,(b)@storeId
和@senderID
ar 参数存储过程。华泰
My stab at it in T-SQL. I assume that (a)
FollowerID
isint
, (b)@storeId
and@senderID
ar parameters of the stored procedure.HTH