日期:2014-05-18 浏览次数:20583 次
ALTER FUNCTION [dbo].[fun_HFL] () returns @TableTmp table(database_name varchar(36)) as begin declare @currentdatabasename nvarchar(20) declare myCur cursor for select database_name from company_set-------取数据库名称 open myCur fetch next from myCur into @currentdatabasename while @@fetch_status = 0 begin ---------------------------数 据 库 名 变 量-------------------- insert into @TableTmp select database_name from dbo.company_set fetch next from myCur into @currentdatabasename end close myCur deallocate myCur return end
------解决方案--------------------
没看明白你想要做什么 给你一个 表值函数的例子。希望对你有帮助 USE [mytest] GO /****** Object: UserDefinedFunction [dbo].[fn_get_Split] Script Date: 04/10/2012 14:12:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER FUNCTION [dbo].[fn_get_Split] ( @SplitString varchar(4000), -- 如果要传入NText类型,下面需要相应的修改,注释行为NText下同 @Separator varchar(2) = ','-- NVarChar(2) = N',' ) RETURNS @SplitStringsTable TABLE ( [id] int identity(1,1), [value] varchar(8000) -- NVarChar(4000) ) AS BEGIN DECLARE @CurrentIndex int DECLARE @NextIndex int DECLARE @ReturnText varchar(8000)-- NVarChar(4000) SELECT @CurrentIndex=1 WHILE(@CurrentIndex<=datalength(@SplitString)) -- DATALENGTH(@SplitString)/2 BEGIN SELECT @NextIndex=charindex(@Separator,@SplitString,@CurrentIndex) IF(@NextIndex=0 OR @NextIndex IS NULL) SELECT @NextIndex=datalength(@SplitString)+1--DATALENGTH(@SplitString)/2 SELECT @ReturnText=substring(@SplitString,@CurrentIndex,@NextIndex-@CurrentIndex) INSERT INTO @SplitStringsTable([value]) VALUES(@ReturnText) SELECT @CurrentIndex=@NextIndex+1 END RETURN END
------解决方案--------------------
SELECT DB_NAME() --获取当前数据库名
------解决方案--------------------
函数中不能exec 动态拼接。