在存储过程中创建函数

时间:2017-09-20 22:09:09

标签: sql sql-server stored-procedures sql-server-2012 sql-function

我有一个存储过程可以更新一对聚合和一个函数 - 通过删除它们,组装并重新导入它们。

我的代码:

CREATE PROCEDURE [maint].[UpdateSqlClr]
AS
     BEGIN

    IF OBJECT_ID('dbo.[HistoricAnalysis]') IS NOT NULL
    DROP FUNCTION [dbo].[HistoricAnalysis];
IF OBJECT_ID('dbo.[OverallStatus]') IS NOT NULL
    DROP AGGREGATE [dbo].[OverallStatus];
IF OBJECT_ID('dbo.[OverallStatusBreakdown]') IS NOT NULL
    DROP AGGREGATE [dbo].[OverallStatusBreakdown];
IF EXISTS
(
    SELECT *
    FROM sys.assemblies
    WHERE [name] = 'SuperDbSqlCLR'
)
    DROP ASSEMBLY [SuperDbSqlCLR];

CREATE ASSEMBLY [SuperDbSqlCLR] FROM 'd:\Super\SuperDb.Sql.CLR\SuperDb.SqlClr.dll' WITH PERMISSION_SET = SAFE;


CREATE AGGREGATE [dbo].[OverallStatus]
(@input [NVARCHAR](4000)
)
RETURNS [NVARCHAR](4000)
EXTERNAL NAME
     SuperDbSqlCLR.[SuperDb.SqlClr.OverallStatus];
CREATE AGGREGATE [dbo].[OverallStatusBreakdown]
(@input [NVARCHAR](4000)
)
RETURNS [NVARCHAR](4000)
EXTERNAL NAME
     [SuperDbSqlCLR].[SuperDb.SqlClr.OverallStatusBreakdown];

-- Where the GO would be in a query window

CREATE FUNCTION [dbo].[HistoricAnalysis] ()
RETURNS TABLE (
  [Sequence] [int] NULL,
  [Text] [nvarchar](100) NULL
)
AS
EXTERNAL NAME [SuperDbSqlCLR].[SuperDb.SqlClr.UserDefinedFunctions].[HistoricAnalysis]

    END

问题在于,虽然我可以在查询窗口中运行SPROC的内容时添加GO,但SPROC创建似乎不喜欢GO语句,每次都会出现'CREATE'的语法错误FUNCTION'必须是批次中唯一的陈述 -

有没有在SPROC中这样做?

1 个答案:

答案 0 :(得分:2)

这应该使用exec命令,即动态SQL。