如何在存储过程中存储多个SELECT语句的结果

时间:2012-11-14 18:56:46

标签: sql sql-server stored-procedures

我有一个存储过程,我需要运行三个单独的SELECT语句并将结果存储在变量中。

我试过这样的事情:

SELECT @tier1MenuIds = Id
FROM TheGateKeeper.NavigationTier1
WHERE ([Page Name] = @currentSlug)

SELECT @tier2MenuIds = Id
FROM TheGateKeeper.NavigationTier2
WHERE ([Page Name] = @currentSlug)

SELECT @tier3MenuIds = Id
FROM TheGateKeeper.NavigationTier3
WHERE ([Page Name] = @currentSlug)

但它给我一个错误说

  

'('。

附近的语法不正确

如果删除底部的两个SELECT,该语句有效。还有其他选择吗?

编辑:我正在使用SQL Server。这是完整的代码:

CREATE PROCEDURE [TheGateKeeper].[editContent]
@description nvarchar(300),
@content nvarchar(MAX),
@title nvarchar(50),
@dateModified date,
@headerImageName nvarchar(100),
@slug nvarchar(50),
@id int

AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @currentSlug as nvarchar(100);
    DECLARE @tier1MenuIds as int;
    DECLARE @tier2MenuIds as int;
    DECLARE @tier3MenuIds as int;

    /* Check if the post exists */
    SELECT @currentSlug =   Slug
    FROM         TheGateKeeper.Posts
    WHERE     (Id = @id)

    IF (@@ROWCOUNT = 1)
    BEGIN
    /* Temporarily unlink all menu items linking to post */
    SELECT @tier1MenuIds = Id
    FROM TheGateKeeper.NavigationTier1
    WHERE ([Page Name] = @currentSlug)

    SELECT @tier2MenuIds = Id
    FROM TheGateKeeper.NavigationTier2
    WHERE ([Page Name] = @currentSlug)

    SELECT @tier3MenuIds = Id
    FROM TheGateKeeper.NavigationTier3
    WHERE ([Page Name] = @currentSlug)

    /* Update the post in the database */
    UPDATE Posts
    SET (Description = @description, [Content] = @content, Title = @title, [Date Modified] =@dateModified, HeaderImageName = @headerImageName, slug =@slug)
    WHERE id = @id

    RETURN 1

    END

    ELSE
    BEGIN
    RETURN 0
    END

1 个答案:

答案 0 :(得分:1)

UPDATE语句中删除括号;它们是不必要的。

您的更新声明应为:

UPDATE Posts
SET Description = @description, [Content] = @content, Title = @title, 
    [Date Modified] =@dateModified, HeaderImageName = @headerImageName, 
    slug =@slug
WHERE id = @id