文件名称:sql 游标、存储过程
文件大小:2KB
文件格式:RAR
更新时间:2017-04-15 10:09:19
sql 游标
--新建视图 v_desk create view v_desk as select a.*, b.usingType_code, b.sOrderNo from t_desk a left join t_using_desk b on a.id = b.desk_id go --新建台号时向 t_using_desk 插入记录 --台号显示的时候查 v_desk --去掉表 t_dish_orderedNumber --t_order有些字段改为float --t_order_temp增加 price float字段 --t_using_desk增加 sOrderNo nvarchar(20) 字段 --t_dishes 修改 ordered_number float ----------------------------- --发送过程:先选台号,若该台号处于正在使用状态(在表 t_using_desk 中 usingType_code = '02'), --则表明是加菜,从 t_using_desk 中获取该台号对应的 sOrderNo,传入存储过程 sp_orderDishes;(在 sp_orderDishes 中将有菜品的台号的 singType_code 置为‘02’,在结账时重新置为 ‘01’) --若该台号处于空闲状态(在表 t_using_desk 中 usingType_code = '01'),则表明该台号即将被使用,需生成 sOrderNo, 向 t_order 表中增加该订单的信息,然后执行存储过程 sp_orderDishes ------------------------- --创建存储过程 sp_orderDishes --1、将临时表 t_order_temp 中的数据插入到 t_order_dishes --2、将表 t_using_desk 中对应的 desk_id 的 usingType_code 置为 '02'(使用中) --3、更新 t_dishes 的 ordered_number --4、删除临时表 t_order_temp 中该 userId 对应的数据 --drop procedure sp_orderDishes create procedure sp_orderDishes @orderNo nvarchar(20), @userId uniqueidentifier, @deskId uniqueidentifier as begin transaction insert into t_order_dishes(sOrderNo, dish_id, price, order_num, subtotal, taste_id1, taste_id2, remark, waiter) select @orderNo, dish_id, price, order_num, price*order_num, taste_id1, taste_id2, remark, userId from t_order_temp where userId = @userId IF @@ROWCOUNT > 0 update t_using_desk set usingType_code = '02', sOrderNo = @orderNo where desk_id = @deskId if @@error <> 0 begin rollback transaction--发生错误则回滚事务,无条件退出l return end DECLARE @DishId uniqueidentifier declare @OrderNum float DECLARE My_Cursor CURSOR--定义游标 FOR (SELECT dish_id, order_num FROM t_order_temp where userId = @userId) --查出需要的集合放到游标中 OPEN My_Cursor; --打开游标 FETCH NEXT FROM My_Cursor INTO @DishId, @OrderNum; --读取第一行数据(将 t_order_temp 表中的 dish_id 放到 @DishId 变量中) WHILE @@FETCH_STATUS=0 BEGIN UPDATE t_dishes SET ordered_number = ordered_number + @OrderNum WHERE id = @DishId; --更新数据 FETCH NEXT FROM My_Cursor INTO @DishId, @OrderNum; --读取下一行数据(将 t_order_temp 表中的 dish_id 放到 @DishId 变量中) END CLOSE My_Cursor; --关闭游标 DEALLOCATE My_Cursor; --释放游标 if @@error <> 0 begin rollback transaction--发生错误则回滚事务,无条件退出l return end delete from t_order_temp where userId = @userId if @@error <> 0 begin rollback transaction--发生错误则回滚事务,无条件退出l return end commit transaction --所有语句都完成,提交事务 go --新建视图 v_order_temp create view v_order_temp as select a.userId as userId, b.name_ch as dishName_ch, b.name_en as dishName_en, a.price as price, a.order_num as order_num, e.name_ch as *itName_ch, e.name_en as *itName_en, isnull(c.name_ch, '')+isnull(','+d.name_ch, '') as tasteName_ch, isnull(c.name_en, '')+isnull(','+d.name_en, '') as tasteName_en, a.remark as remark from t_order_temp a left join t_dishes b on a.dish_id = b.id left join t_taste c on a.taste_id1 = c.id left join t_taste d on a.taste_id2 = d.id left join t_dish_unit e on b.*it_id = e.id go --新建表 t_title_caption create table t_title_caption ( code nvarchar(20), name_ch nvarchar(20), name_en nvarchar(80), item_index int ) go
【文件预览】:
sql.txt