且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

函数的创建及使用时的注意事项

更新时间:2022-10-03 18:48:51

函数的创建及使用时的注意事项

--以下是sql中函数的创建及使用方法

create function test(@Num varchar(20))--@Num 参数

returns varchar(50) --返回值类型

as

begin

declare @MSG varchar(20)

if(@Num =1)

    select @MSG ='正确'

else

    select @MSG ='错误'

return @MSG

end

--调用函数

select dbo.test(2)—-这里的表名一定得加上dbo不加就报错

 

--创建返回table类型的函数

create function GetAllStudents()

returns table

as

return (select * from student)

--调用函数

select * from GetAllStudents()—-这里的表名可加可不加dbo



本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/416792,如需转载请自行联系原作者