使用存储过程 *** 作数据表

使用存储过程 *** 作数据表,第1张

概述go use db_Test;if exists (select * from sysobjects where name='book')drop table book;create table book( bookId int primary key, bookName char(20), bookPrice float, publishTime date);--创建存储过程
go use db_Test;if exists (select * from sysobjects where name='book')drop table book;create table book(	bookID int primary key,bookname char(20),bookPrice float,publishTime date);--创建存储过程,向book表中插入10条数据goif exists (select * from sysobjects where name='addBook')drop procedure addBook;gocreate procedure addBook asbegin	declare @i int;	set @i=1;	while(@i<10)	begin		--cast 将一个表达式转化成另外一个类型		insert into book select @i,'book '+cast(@i as CHAR(1)),(10+@i)/10.0,GETDATE();		set @i=@i+1;	endendgo--执行存储过程exec addBook;--创建带参数的存储过程:@ID表示所借书ID @money表示钱,result 表示借书结果if exists(select * from sysobjects where name='borrowBook')drop procedure borrowBook;gocreate procedure borrowBook @ID int,@money float,@result bit output asbegin	declare @bookPrice float;	--获取所借书本的价格	select @bookPrice=bookPrice from book where bookID=@ID;	if(@money>=@bookPrice)		begin		set @result=1;		print '可以借书';		end	else		begin		set @result=0;		print '不能借书';		endend go--执行存储过程declare @result bit;--使用output 返回参数exec borrowBook 1,1.3,@result output;select case 			when @result=0 then '不能借书'			when @result=1 then '可以借书' end as '能否借书';--修改存储过程:将borrowBook的返回值的类型改为chargoalter procedure borrowBook @ID int,@result char(20) output asbegin	declare @bookPrice float;	--获取所借书本的价格	select @bookPrice=bookPrice from book where bookID=@ID;	if(@money>=@bookPrice)		begin		set @result='可以借书';		end	else		begin		set @result='不能借书';		endend godeclare @re char(20);exec borrowBook 1,1.2,@re output;select @re as '能否借书';
总结

以上是内存溢出为你收集整理的使用存储过程 *** 作数据表全部内容,希望文章能够帮你解决使用存储过程 *** 作数据表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1163249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存