
自定义函数
用户定义自定义函数像内置函数一样返回标量值,也可以将结果集用表格变量返回
用户自定义函数的类型:
标量函数:返回一个标量值
表格值函数{内联表格值函数、多表格值函数}:返回行集(即返回多个值)
1、标量函数
Create function 函数名(参数)
Returns 返回值数据类型
[with {Encryption | Schemabinding }]
[as]
begin
SQL语句(必须有return 变量或值)
End
Schemabinding :将函数绑定到它引用的对象上(注:函数一旦绑定,则不能删除、修改,除非删除绑定)
Create function AvgResult(@scode varchar(10))
Returns real
As
Begin
Declare @avg real
Declare @code varchar(11)
Set @code=@scode + ‘%’
Select @avg=avg(result) from LearnResult_baijiali
Where scode like @code
Return @avg
End
执行用户自定义函数
select 用户名。函数名 as 字段别名
select dboAvgResult(‘s0002’) as result
用户自定义函数返回值可放到局部变量中,用set ,select,exec赋值
declare @avg1 real ,@avg2 real ,@avg3 real
select @avg1= dboAvgResult(‘s0002’)
set @avg2= dboAvgResult(‘s0002’)
exec @avg3= dboAvgResult ‘s0002’
select @avg1 as avg1 ,@avg2 as avg2 ,@avg3 as avg3
函数引用
create function code(@scode varchar(10))
returns varchar(10)
as
begin
declare @ccode varchar(10)
set @scode = @scode + ‘%’
select @ccode=ccode from cmessage
where ccode like @scode
return @ccode
end
select name from class where ccode = dbocode(‘c001’)
2、表格值函数
a、 内联表格值函数
格式:
create function 函数名(参数)
returns table
[with {Encryption | Schemabinding }]
as
return(一条SQL语句)
create function tabcmess(@code varchar(10))
returns table
as
return(select ccode,scode from cmessage where ccode like @ccode)
b、 多句表格值函数
create function 函数名(参数)
returns 表格变量名table (表格变量定义)
[with {Encryption | Schemabinding }]
as
begin
SQL语句
end
多句表格值函数包含多条SQL语句,至少有一条在表格变量中填上数据值
表格变量格式
returns @变量名 table (column 定义| 约束定义 [,…])
对表格变量中的行可执行select,insert,update,delete , 但select into 和 insert 语句的结果集是从存储过程插入。
Create function tabcmessalot (@code varchar(10))
Returns @ctable table(code varchar(10) null,cname varchar(100) null)
As
Begin
Insert @ctable
Select ccode,explain from cmessage
Where scode like @code
return
End
Select from tabcmessalot(‘s0003’)
来自:http://hibaiducom/datachina/blog/item/801def0366c4e7ea09fa9344html
C#调用SQL自定义函数返回值
代码
1 --SQL自定义函数:
2
3 CREATE FUNCTION [GetProjectID] (@headStr nvarchar(10),@date datetime)
4 )
5
6 RETURNS NVARCHAR(200)
7
8 AS
9
10 BEGIN
11
12 --不能在自定义函数中用INSERT INTO
13
14 --insert into emos_cust(cust_name,dates)values(
15
16 --@headStr,@date
17
18 --)
19
20 return 'TEST BY HANSHU'
21 END
代码
1 /// <summary>
2 /// 获取项目文件编号 涂聚文
3 /// </summary>
4 private void FileNo()
5 {
6
7 SqlConnection conn = new SqlConnection(connectionString);
8 string strSql = "GetProjectID"; //自定SQL函数
9 SqlCommand cmd = new SqlCommand(strSql, conn);
10 cmdCommandType = CommandTypeStoredProcedure;
11 cmdParametersAdd("@headStr", SqlDbTypeNVarChar)Value = "ZQ3"; //输入参数
12 cmdParametersAdd("@date", SqlDbTypeDateTime)Value = SystemDateTimeNowToShortDateString(); //输入参数
13 cmdParametersAdd("@returnString", SqlDbTypeNVarChar);
14 cmdParameters["@returnString"]Direction = ParameterDirectionReturnValue; //返回参数
15 try
16 {
17 connOpen();
18 object o= cmdExecuteScalar();
19
20 thistxtAFileNOText = cmdParameters["@returnString"]ValueToString();
21
22 //ResponseWrite("");
23
24 }
25 catch (Exception ex)
26 {
27
28 thistxtAFileNOText = exMessage;
29
30 }
31 finally
32 {
33
34 if (!(connState == ConnectionStateClosed))
35 {
36
37 connClose();
38
39
40 }
41
42 }
43
44
45 }
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)