SQL存儲過程的創建和使用


存儲過程創建

create procedure 存儲過程名
as
sql語句

 

go
create procedure bookbooks(@bno int, @sno varchar(12))
as
begin transaction
declare @state int
select @state = Bstate from book where Bno = @bno
if @state in (1 ,3)
	begin
		rollback
	end
else if @state = 0
	begin
		update book set Bstate = 1 where Bno = @bno
		insert into Bbook 
		(Bdate, Bno, Sno)
		values
		(GETDATE(), @bno, @sno)
	end
else if @state = 2
	begin
		update book set Bstate = 3 where Bno = @bno
		insert into Bbook 
		(Bdate, Bno, Sno)
		values
		(GETDATE(), @bno, @sno)
	end
commit
go

go
create procedure renwebook(@bno int, @sno varchar(12), @aid varchar(12))
as
begin transaction
	declare @state int
	select @state = bstate from book where Bno = @bno
	if(@state in (0, 1, 3))
		begin
			rollback
		end
	else if(@state = 2)
		begin
			insert into Rbook
			(Aid, Bdate, Bno, Sno)
			values
			(@aid, GETDATE(), @bno, @sno)
			update book set Bstate = 3 where Bno = @bno
		end
commit
go

存儲過程使用

exec dbo.bookbooks 參數1 = 值1, 參數2 = 值2, ... 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM