Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 如何将此类C代码转换为SQL Server存储过程_Sql Server 2008_Stored Procedures_Enumeration_Pseudocode - Fatal编程技术网

Sql server 2008 如何将此类C代码转换为SQL Server存储过程

Sql server 2008 如何将此类C代码转换为SQL Server存储过程,sql-server-2008,stored-procedures,enumeration,pseudocode,Sql Server 2008,Stored Procedures,Enumeration,Pseudocode,下面是我需要编写的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 foll

下面是我需要编写的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
它获取所有商店的跟随者ID,在消息中为第一个创建记录。然后,它为每个后续跟随者ID创建一条消息,同时指定批处理中第一条消息记录的ID

我需要将其转换为SQL Server存储过程,但我以前从未编写过存储过程,所以我很犹豫。我应该使用表变量来保存select结果吗?我应该使用数组吗?与foreach最接近的匹配是什么?如何切掉第一个元素


我非常希望能有一个这样的过程的草图,只是想知道进一步看什么。

我在T-SQL中的尝试。我假设FollowerID是存储过程的int、b@storeId和@senderID ar参数

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