sql创建自定义函数
1. 用SQL语言,创建一个用户自定义函数,判断一个数是否能被5和7同时整除
CREATE
FUNCTION
F2(@数
INT)
RETURNS
VARCHAR(20)
AS
BEGIN
DECLARE
@结果
VARCHAR(20)
IF
@数
%
5=0
AND
@数
%
7=0
SET
@结果='可以被5和7
整除
'
ELSE
SET
@结果='不能被5和7整除'
RETURN
@结果
END
GO
select
dbo.F2(20),dbo.F2(35)
2. SQL创建用户自定义函数
楼主我只写了其中一个..基本方法都如此,希望对您有帮助,代码如下
ALTER
FUNCTION
[dbo].[sum]
(
@class
varchar(100),
--所选课程
@username
varchar(20)
--学生姓名
)
RETURNS
int
AS
BEGIN
DECLARE
@reun
int
SELECT
SUN(@class)
FROM
YOURTABLE
WHERE
USERNAME=@username
RETURN
@reun
END
3. SQL自定义函数的创建(给定班级号,统计该班级男女生数量)
CREATE FUNCTION dbo.fn_stucount (@classno nvarchar(20))
RETURNS TABLE
AS
RETURN
(
select a.sex,count(*)数量 from student a,class b where a.sno=b.sno and b.classno=@classno
group by a.sex
);
4. 写一个Sql自定义函数
declare @flag int,@no varchar(20),@return int
--先为变量赋值
--set @flag=0...
select @return = search1(@flag,@no)
我没明白,是要写函数的代码还是写调用的代码?调用方法上面已写。
5. Microsoft SQL Server如何创建 自定义函数
Create Function RmGetPY(@chn nchar(1))
returns char(1)
as
begin
declare @n int
declare @c char(1)
set @n = 63
select @n = @n +1,@c = case chn when @chn then char(@n) else @c end from(
select top 27 * from (
select chn =
'吖' union all select
'八' union all select
'嚓' union all select
'咑' union all select
'妸' union all select
'发' union all select
'旮' union all select
'铪' union all select
'丌' union all select
'丌' union all select
'咔' union all select
'垃' union all select
'呒' union all select
'拏' union all select
'噢' union all select
'妑' union all select
'七' union all select
'呥' union all select
'仨' union all select
'他' union all select
'屲' union all select
'屲' union all select
'屲' union all select
'夕' union all select
'丫' union all select
'帀' union all select @chn) as a
order by chn COLLATE Chinese_PRC_CI_AS
) as b
return(@c)
end
go
Create Function GetAllPY(@chn nvarchar(100))
returns varchar(30)
as
begin
declare @i int,@j int,@result varchar(100)
set @result=''
set @i=len(@chn)
set @j=1
while @j<=@i
begin
set @result = @result + dbo.RmGetPY(substring(@chn,@j,1))
set @j=@j+1
end
return @result
end
看看这两个,典型的取汉字拼音码的函数
6. 什么是SQL自定义函数
就是自己定义出一个满足自己程序需要的函数,可以参考
书上的函数,创建一个满足自己需要的函数
7. SQL中创建一个用户自定义函数
CREATE
FUNCTION
DBO.tFProctsHS
(
@name
VARCHAR(10))
RETURNS
INT
--这个根据价格的类型自己修改
AS
begin
DECLARE
@jiage
INT;
select
@jiage=
jiage
from
商品表
where
name=
@name
RETURN
(@jiage)
end
--ceshi
select
DBO.tFProctsHS
('记事本')