从存储过程中的表中返回所有id

时间:2016-03-22 14:52:05

标签: sql sql-server tsql stored-procedures

我继续发现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

5 个答案:

答案 0 :(得分:2)

您必须使用变量:

Msgbox ActivePresentation.Slides(1).Tags("TIMING")

答案 1 :(得分:2)

或者只是简单明了地加入

create proc spGetItemsByUser
    @userName nvarchar(50)
as
begin

    select * from Users usr
    join UsersToFeed utf on usr.id = utf.userID
    join feed fee on utf.feedid = fee.id
    where usr.name = @userName
end

答案 2 :(得分:1)

CREATE PROC spGetItemsByUser
(
    @userName NVARCHAR(50)
)
AS BEGIN

    SET NOCOUNT ON;

    DECLARE @userId INT

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

    SELECT *
    FROM feed f
    WHERE EXISTS(
        SELECT *
        FROM userstofeed uf
        WHERE f.ID = uf.feedid
            AND uf.userid = @userId
    ) 

END

答案 3 :(得分:1)

问题在于您是在尝试逐步完成此操作。您的变量只会在任何给定时间保持单个变量,即使您在具有多个结果的#include "contiki-net.h" #include "http-socket.h" #include "ip64-addr.h" #include <stdio.h> static struct http_socket s; static int bytes_received = 0; static void callback(struct http_socket *s, void *ptr, http_socket_event_t e, const uint8_t *data, uint16_t datalen) { if(e == HTTP_SOCKET_ERR) { printf("HTTP socket error\n"); } else if(e == HTTP_SOCKET_DATA) { bytes_received += datalen; printf("HTTP socket received %d bytes of data\n", datalen); } } PROCESS_THREAD(http_example_process, ev, data) { PROCESS_BEGIN(); /* Initializes the socket */ http_socket_init(&s); /* GET request */ http_socket_get(&s, "http://www.contiki-os.org/", 0, 0, callback, NULL); /* Waits forever for the HTTP callback */ while(1) { PROCESS_WAIT_EVENT_UNTIL(0); } PROCESS_END(); } 查询中进行设置也是如此。只需将这些全部放入一个查询中就可以了:

SELECT

答案 4 :(得分:1)

CREATE PROC spGetItemsByUser
    @userName NVARCHAR(50)
AS BEGIN

    DECLARE @userId INT,
            @feedId INT;

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

    SELECT *
    FROM feed
    WHERE ID IN (
        SELECT feedid
        FROM userstofeed
        WHERE userid = @userId
    )

END