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
('記事本')