我继续发现SQL和存储过程,遇到@feedid仅返回1个id的麻烦,但是对于ex来说却有。 20.如何返回ID数组?

CREATE PROC spGetItemsByUser
    @userName NVARCHAR(50)
AS BEGIN

    DECLARE @userId INT,
            @feedId INT;

    SELECT @userId = ID
    FROM users
    WHERE name = @userName

    SELECT @feedId = feedid
    FROM userstofeed
    WHERE userid = @userId

    SELECT *
    FROM feed
    WHERE ID = @feedId

END

最佳答案

您必须使用表变量:

declare @userId table (uid int)
declare @feedId table (id int)

insert into @userid(uid)
select id
from Users
where name = @userName

insert into @feedid(id)
select feedid
from userstofeed
where userid in (select uid from @userId)

select * from feed where id in (select id from @feedId)

09-28 14:50