存储过程、函数区别
- 函数可以在sql语句中调用,存储过程不行
- 函数只能返回一个返回值,存储过程可以返回多个
存储过程
- 无参数的存储过程
create proc usp_helloworld
as
begin
print 'Hello World'
end
- 有参数的存储过程
create proc usp_book
@name nvarchar(50),
@author nvarchar(50)
as
begin
select * from book where name=@name and author=@author
end
调用
exec usp_book “test”,“book”
exec usp_book @name=“test”,@author=“book”
- 带输出的存储过程
create proc usp_output
@bookname nvarchar(50),
@recordCount int output --关键字代表输出参数
as
begin
select * from hero where bookname=@bookname
--把查询的记录条数赋值给变量@recordCount
set @recordCount = (select count(*) from hero where bookname=@bookname)
end
declare @num int
exec usp_output @bookname=‘test’,@recordCount=@num output
select @num as table_name