动态创建另一个数据库中的函数

时间:2014-02-25 15:44:44

标签: sql sql-server sql-server-2008 tsql sql-server-2012

当我在掌握时,我正在尝试在每个数据库上创建函数, 我正在使用游标

DECLARE c_db_names CURSOR FOR
      SELECT name 
      FROM sys.databases
       WHERE name NOT IN('master','model','msdb','tempdb','ReportServer$SQLEXPRESS','ReportServer$SQLEXPRESSTempDB') 
      OPEN c_db_names
      FETCH next from c_db_names INTO @db_name

      WHILE @@Fetch_Status = 0
      BEGIN

      set @sql = 'Create function...'

set @UseAndExecStatment = 'use ' + @db_name + ' exec sp_executesql '+@sql

        exec Sp_executeSql @UseAndExecStatment
        FETCH next from c_db_names INTO @db_name
      END
      CLOSE c_db_names
      DEALLOCATE c_db_names

问题是它不起作用,它说: “CREATE FUNCTION”必须是查询批处理中的第一个语句“

有什么想法吗? 谢谢你的帮助。

3 个答案:

答案 0 :(得分:2)

您可以改为使用db..sp_executesql:

declare @sql nvarchar(max), @db_name nvarchar(max), @sql2 nvarchar(max)

DECLARE c_db_names CURSOR FOR
      SELECT name 
      FROM sys.databases
       WHERE name NOT IN('master','model','msdb','tempdb','ReportServer$SQLEXPRESS','ReportServer$SQLEXPRESSTempDB') 
       and name ='reports'
      OPEN c_db_names
      FETCH next from c_db_names INTO @db_name

      WHILE @@Fetch_Status = 0
      BEGIN

      set @sql2 = 'create procedure ...'
      set @sql = 'exec '+@db_name+'..sp_executesql N''' + @sql2+''''

        print @sql
        --exec(@sql)
        FETCH next from c_db_names INTO @db_name
      END
      CLOSE c_db_names
      DEALLOCATE c_db_names

答案 1 :(得分:0)

在“创建”查询中定义数据库

DECLARE c_db_names CURSOR FOR
      SELECT name 
      FROM sys.databases
       WHERE name NOT IN('master','model','msdb','tempdb','ReportServer$SQLEXPRESS','ReportServer$SQLEXPRESSTempDB') 
      OPEN c_db_names
      FETCH next from c_db_names INTO @db_name

      WHILE @@Fetch_Status = 0
      BEGIN

        set @sql = 'Create function '+@db_name+'dbo.functionName ......'

        exec Sp_executeSql @sql
        FETCH next from c_db_names INTO @db_name
      END
      CLOSE c_db_names
      DEALLOCATE c_db_names

请参阅:

设置@sql ='创建功能'+@db_name+'dbo.functionName ......'

答案 2 :(得分:0)

完成:))

declare @sql nvarchar(max), @db_name nvarchar(max), @sql2 nvarchar(max)
declare @sp_executesql nvarchar (max)
DECLARE c_db_names CURSOR FOR
      SELECT name 
      FROM sys.databases
       WHERE name NOT IN('master','model','msdb','tempdb','ReportServer$SQLEXPRESS','ReportServer$SQLEXPRESSTempDB') 
       --and name ='reports'
      OPEN c_db_names
      FETCH next from c_db_names INTO @db_name

      WHILE @@Fetch_Status = 0
      BEGIN


      set @sql2 = N'
CREATE FUNCTION [String_Turn_Month]  (@d char(6))
RETURNS char(6)
AS
BEGIN 
DECLARE @#DSTR char(6)
IF @D=''''000000''''
set @#dstr=''''000000''''
IF substring(@d,3,4)<1800
RETURN(@D)
IF substring(@d,3,4)>1800
set @#dstr=cast(substring(@d,3,4)+substring(@d,1,2) as char(6))
RETURN(@#DSTR)
END'
  --  set @sql = 'exec '+@db_name+'..sp_executesql N''' + @sql2+''''

  set @sp_executesql = 'exec ' + @db_name + '..sp_executeSQL N''' + @sql2 + ''''
  PRINT @sp_executesql
    EXEC sp_executeSQL   @sp_executesql

        print '------------'
        print '------------'
        print '------------'

     --   exec(@sql)
        FETCH next from c_db_names INTO @db_name
      END
      CLOSE c_db_names
      DEALLOCATE c_db_names