打赏

sql 函数

正确写法:
ALTER FUNCTION [dbo].[uf_GetContractPartyName] ( @ContractPartyID as uniqueidentifier ) RETURNS varchar(300) AS BEGIN DECLARE @Ret varchar(300) SELECT @Ret =PartyName FROM tbl_biz_ContractPartyInfo WHERE ContractPartyID = @ContractPartyID RETURN @Ret END

  

错误写法:
ALTER FUNCTION [dbo].[uf_GetContractPartyName] ( @ContractPartyID as uniqueidentifier ) RETURNS @Ret varchar(300) AS BEGIN --DECLARE @Ret varchar(300) SELECT @Ret =PartyName FROM tbl_biz_ContractPartyInfo WHERE ContractPartyID = @ContractPartyID RETURN --@Ret END

sql变量使用前必须declare,临时表使用前不需要declare

  

/******************************************************************************
**		Name: ufn_Split
**		Desc: 拆分字符串
**
**              
**		Return Values:
** 				
**		Parameters:	
**		Auth: 
**		Date:2008-10-24
*******************************************************************************/
ALTER function [dbo].[ufn_Split]
(
	@String varchar(max),				-- 要拆分的字符串
	@Key varchar(50)					-- 关键字
)
returns @sValues table(sIndex int identity(1,1), sValue varchar(max) )
as
begin
	-- 索引及当前值
	declare @KeyIndex int
	declare @CurrentValue varchar(500)

	set @string = RTrim(LTrim(@String))
	
	-- 拆分
	set @KeyIndex = charindex(@Key,@string)

	while @KeyIndex <> 0
	begin
		set @CurrentValue = substring(@String,1,@KeyIndex-1)

		insert into @sValues(sValue) values (@CurrentValue)

		set @String = substring(@String, @KeyIndex+1, len(@String)- @KeyIndex)

		set @KeyIndex = charindex(@Key, @String)
	end

	insert into @sValues(sValue) values (@String)

	-- 返回拆份结果
	return
end

sql变量使用前必须declare,临时表使用前不需要declare

  

posted @ 2017-09-29 09:50  刘奇云  阅读(197)  评论(0编辑  收藏  举报