嗨什么时候在sql server中使用事务,请在插入,更新和删除记录时给出样本,我告诉我的朋友他们正在使用事务概念,即使... [英] Hi when to use transactions in sql server and please give a sample of it while inserting, updating and deleting records, i have told by my friend they are using transaction concept even while...

查看:62
本文介绍了嗨什么时候在sql server中使用事务,请在插入,更新和删除记录时给出样本,我告诉我的朋友他们正在使用事务概念,即使...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您何时在sql server中使用事务并在插入,更新和删除记录时请提供示例,我已经告诉我的朋友他们正在使用事务概念,即使在将数据插入数据库时​​

解决方案

sql事务集用于下面的标准,

我有两个表

PurchaseMain(BillNo,BillDate,BillTotalAmt)

PurchaseDetails(BillNo(FK),ProductId,ProductQty,ProductRate,ProductPrice)



你可以理解上面的结构,

如果未保存列表中的任何产品,则在插入数据时很明显,那么您需要回滚整个Bill。 (回滚丢弃保存的数据,这些数据在逻辑上是不完整的 - 这里没有产品并不意味着继续进入购买主表。)



在这种情况下交易可以使用

例如

开始交易tran_purchase 
- 注册购买主
- 用于循环添加产品购买详情
- 在 Purchasedetail
中使用购买主新帐单否条目 - 如果错误则 RollBack事务tran_purchase 返回 ...表示发生错误,然后回滚更改并终止执行,无需执行下一行。
- for循环
提交事务tran_purchase ...意味着保存交易没有发现错误





some用于与锁定概念保持一致的时间

例如

车辆编号分配

车辆号码是alphaNumeric它不是int因此我们必须手动生成它

一次来自不同城市分支的状态多个命令将被赋予数据库以生成车辆没有

所以,对于单一车辆重复生成nos的可能性

此标准假设车辆号码是GJ-1-ZZ-9789

开始交易tranVehiclenoGeneration与锁定水平
- 特里姆4位数来自rigthside
- 增加1来
- 检查数据库GJ-1-ZZ-9790新的不存在如果没有则继续...中间'zz'的相同方式需要生成下一个系列所以,那时它的依赖性是否应该在表中添加其他记录,否则如果程序同时是
则可能使计算错误 - 所以这种情况我们需要在事务集中保持逻辑与合适的锁定概念
- 最终提交/回滚事务将重新发布锁定



快乐编码!

:)


嗨检查链接....



SQL Server事务和错误处理 [< a href =http://www.codeproject.com/Articles/4451/SQL-Server-Transactions-and-Error-Handlingtarget =_ blanktitle =New Window> ^ ]

交易简介 [ ^ ]

管理SQL Server存储过程中的事务 [ ^ ]

在SQL Server中使用事务 [ ^ ]

问候,

GVPrabu

事务用于确保数据库始终处于一致状态。

当需要完全更改数据库时使用事务。如果发生错误所有更改都回滚到之前的状态。

您还可以阅读此博文

http: //blogs.msdn.com/b/florinlazar/archive/2005/10/04/476775.aspx [ ^ ]


Hi when to use transactions in sql server and please give a sample of it while inserting, updating and deleting records, i have told by my friend they are using transaction concept even while inserting data to database

解决方案

sql transaction set is used when criteria like below,
I have two tables
PurchaseMain (BillNo,BillDate,BillTotalAmt)
PurchaseDetails (BillNo(FK),ProductId,ProductQty,ProductRate,ProductPrice)

you can understand above structure,
it is obvious while inserting data if any product from list is not saved then you need to rollback whole Bill. (Rollback discard saved data which is logically incomplete - here without products no mean to keep entry in purchase main table.)

in this case transaction se can be used
e.g.

begin transaction tran_purchase
--entry in purchase main
--for loop to add products in purchase details
    --using purchase main new bill no entry in Purchasedetail
    --if error then RollBack transaction tran_purchase and return  ...means error occur then rollback changes and terminate execution no need to execute next line.
--end of for loop
commit transaction tran_purchase  ... means save transaction no error found



some times it''s used for consistency with locking concepts
e.g.
Vehicle number allocation
vehicle no. are alphaNumeric it''s not int so that we have to manually produce it
at a time from different city branches in a state multiple command will given to database for generate vehicle no
so, chances of duplicate nos generation for single vehicle
this criteria suppose vehicle no is GJ-1-ZZ-9789

begin transaction tranVehiclenoGeneration with Lock level
--trim 4 digit from rigthside 
--increase no by 1
--check in database GJ-1-ZZ-9790 new no exist if no then go ahead  ... same way for middle 'zz' need to generate next series so, it's dependancy at that time no oher record should be added in table else it can make calculaions wrong if procedure is meanwhile
--so this case we need to keep logic in transaction set with suitable lock concept
--at end commit/rollback transaction will realease lock


Happy Coding!
:)


Hi Check the links....

SQL Server Transactions and Error Handling[^]
Introduction to Transactions[^]
Managing Transactions in SQL Server Stored Procedures[^]
Using Transactions in SQL Server[^]
Regards,
GVPrabu


Transactions are to be used to ensure that the database is always in a consistent state.
Use Transaction when you need to changes in database completely done.and if error occurred all changes Rollbacked to its previous state.
you can also read this blogpost
http://blogs.msdn.com/b/florinlazar/archive/2005/10/04/476775.aspx[^]


这篇关于嗨什么时候在sql server中使用事务,请在插入,更新和删除记录时给出样本,我告诉我的朋友他们正在使用事务概念,即使...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆