Sql Server数据库实验之存储过程和函数

在这里插入图片描述


create procedure s1
@Rname nvarchar(50)='王小明',
@Btitle nvarchar(50)output,
@BorrowDate date output,
@Bauthor nvarchar(50) output
as select @Rname=Rname,@Btitle=Btitle,@BorrowDate=BorrowDate,@Bauthor=Bauthor
from Book,Borrow,Reader where @Rname=Rname and Book.Bno=Borrow.Bno and Reader.Rno=Borrow.Rno
go

create procedure s2
@maxprice float output,@minprice float output
as
select @maxprice=max(Bprice),@minprice=min(Bprice) from Book
go

create procedure s3
@Rname nvarchar(50)='王小明',
@Btitle nvarchar(50),
@Bprice float output,
@BorrowDate date output,
@ReturnDate date output,
@Bauthor nvarchar(50) output
as select @Bprice=Bprice,@BorrowDate=BorrowDate,@ReturnDate=ReturnDate,@Bauthor=Bauthor
from Book,Borrow,Reader where @Rname=Rname and @Btitle=Btitle and Book.Bno=Borrow.Bno and Reader.Rno=Borrow.Rno
go

create procedure s4
@Btitle nvarchar(50),
@Borrowtotal int output
as select  @Borrowtotal=count(Borrnum) 
from Book,Borrow,Reader where @Btitle=Btitle and Book.Bno=Borrow.Bno and Reader.Rno=Borrow.Rno
go

declare @Btitle1 nvarchar(50),@Borrowtotal1 int
select @Btitle1='数据结构'
exec s4  @Btitle1,
@Borrowtotal1 output
print @Borrowtotal1
go

create procedure s5
@Btitle nvarchar(50),
@Rname nvarchar(50)output,
@Rage smallint output,
@Reducation nvarchar(10)output,
@BorrowDate date output,
@ReturnDate date output
as select @Rname=Rname,@Rage=Rage,@BorrowDate=BorrowDate,@ReturnDate=ReturnDate,@Reducation=Reducation
from Book,Borrow,Reader where  @Btitle=Btitle and Book.Bno=Borrow.Bno and Reader.Rno=Borrow.Rno
go

declare @Btitle1 nvarchar(50),
@Rname1 nvarchar(50),
@Rage1 smallint ,
@Reducation1 nvarchar(10),
@BorrowDate1 date ,
@ReturnDate1 date 
select @Btitle1='操作系统概论'
exec s5 @Btitle1,
@Rname1 output,
@Rage1 output,
@Reducation1 output,
@BorrowDate1 output,
@ReturnDate1 output
print @Rname1
print @Rage1
print @Reducation1
print @BorrowDate1
print @ReturnDate1

在这里插入图片描述

-- ================================================
-- Template generated from Template Explorer using:
-- Create Scalar Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION f1(@Rname nvarchar(100),@Btitle  nvarchar(100))
RETURNS date
AS
BEGIN
	-- Declare the return variable here
	DECLARE @BorrowDate date

	-- Add the T-SQL statements to compute the return value here
	SELECT @BorrowDate=BorrowDate from Book,Borrow,Reader
	where  @Btitle=Btitle and Book.Bno=Borrow.Bno and Reader.Rno=Borrow.Rno
	and Book.Btitle=@Btitle and Reader.Rname=@Rname

	-- Return the result of the function
	RETURN (@BorrowDate)

END
GO
CREATE FUNCTION f2(@Btitle  nvarchar(100))
RETURNS @f2 table(Rname nvarchar(100),Rage smallint,Reducation nvarchar(10),BorrowDate date ,ReturnDate date )
AS
BEGIN
	-- Declare the return variable here
	insert @f2

	-- Add the T-SQL statements to compute the return value here
	SELECT Rname,Rage,Reducation,BorrowDate,ReturnDate from Book,Borrow,Reader
	where  @Btitle=Btitle and Book.Bno=Borrow.Bno and Reader.Rno=Borrow.Rno
	and Book.Btitle=@Btitle order by BorrowDate desc

	-- Return the result of the function
	RETURN 

END
GO
CREATE FUNCTION f3(@Btitle  nvarchar(100))
RETURNS @f3 table(Btitle nvarchar(100),Bookcount smallint,avgRage smallint)
AS
BEGIN
	-- Declare the return variable here
	insert @f3

	-- Add the T-SQL statements to compute the return value here
	SELECT Btitle,avg(Rage) as avgRage,count(Borrnum) as Bookcount from Book,Borrow,Reader 
	where  @Btitle=Btitle and Book.Bno=Borrow.Bno and Reader.Rno=Borrow.Rno
	and Book.Btitle=@Btitle group by Btitle

	-- Return the result of the function
	RETURN 

END
GO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值