张志峰的博客

水滴石川,积少成多。

导航

sql server 自定义函数

Posted on 2015-07-07 11:25  ╰★张志峰★╮  阅读(242)  评论(0)    收藏  举报

create function 函数名 (@pno int) returns int as begin declare @a int if not exists(select * from person where pno=@pno) set @a=-1 else set @a=1 return @a end 调用函数: use 数据库名 go select dbo.函数名(13250)

 

 

--自己编写的sql 函数  用于去除字符串中有[]和()

DROP FUNCTION replacestr
create function replacestr
(@str VARCHAR(4000))
returns VARCHAR(4000)
as
begin
declare @a VARCHAR(4000)
SET @a = @str

SET @a = REPLACE(@a,'[', '_')
SET @a = REPLACE(@a,']', '_')
SET @a = REPLACE(@a,'(', '_')
SET @a = REPLACE(@a,')', '_')
return @a
END

SELECT dbo.replacestr('sdfgsdf掌上地方[sdfgz张]')