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张]')