混合创建存储过程并在Sql Server中插入语句

时间:2011-01-11 21:06:29

标签: sql sql-server stored-procedures

我创建了许多要单独运行的脚本,但是被要求将它们全部组合在一起,所以DBA只需要执行一次。问题是我似乎无法将它们组合在一起运行。只运行查询中的第一项。如何将这些格式设置为在一个大脚本中一起运行?

   USE [DEV]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    Create PROCEDURE [dbo].[Projects]
       @ProjectID            int,
       @ClientID             int
    AS
    BEGIN
        .....Cool procedure here
    END
   GRANT EXECUTE ON [dbo].[Projects] TO Admin, Employee

INSERT INTO random_table(stuff)
VALUES (stuff)

4 个答案:

答案 0 :(得分:4)

在陈述之间添加GO

USE [DEV]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    Create PROCEDURE [dbo].[Projects]
       @ProjectID            int,
       @ClientID             int
    AS
    BEGIN
        .....Cool procedure here
    END

   GO -- Add GO here

   GRANT EXECUTE ON [dbo].[Projects] TO Admin, Employee

   GO -- Add GO here
INSERT INTO random_table(stuff)
VALUES (stuff)

答案 1 :(得分:2)

   USE [DEV]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    Create PROCEDURE [dbo].[Projects]
       @ProjectID            int,
       @ClientID             int
    AS
    BEGIN
        .....Cool procedure here
    END
   GRANT EXECUTE ON [dbo].[Projects] TO Admin, Employee
GO -- added this "go" statement
INSERT INTO random_table(stuff)
VALUES (stuff)

答案 2 :(得分:0)

在每个陈述后插入GO。

答案 3 :(得分:0)

USE [DEV]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    Create PROCEDURE [dbo].[Projects]
       @ProjectID            int,
       @ClientID             int
    AS
    BEGIN
        .....Cool procedure here
    END
    GO //add Go after every statement
   GRANT EXECUTE ON [dbo].[Projects] TO Admin, Employee
   GO
   INSERT INTO random_table(stuff)
   VALUES (stuff)

虽然,我建议你从SQL服务器数据库生成数据库模式的脚本(包括存储过程,函数,表创建,插入,更新和删除),然后用.SQL文件保存它,你不必手动放置这些GO。看看这个很好的example