Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
M2
Starting Member
22 Posts |
Posted - 2003-09-07 : 22:16:52
|
| I want handling error in following insertion operation. Could any one pls help me check whether it's correct or not ? Because I cant test it under Query Analyzer. DECLARE @RegionID intDECLARE @Name varchar(20)DECLARE @msg varchar (255)DECLARE @errorNumber intSet @RegionID = 'A1'Set @Name = '723k'BEGIN TRANSACTIONINSERT INTO Branch (RegionID,Name)VALUES (@RegionID,@Name) SET @errorNumber = @@error IF @errorNumber <> 0 BEGIN SET @msg = 'Error: ' + CAST(@errorNumber as varchar(10)) + 'occurred during insert branch record !' RAISERROR 50000 @msg Rollback Transaction RETURN ENDCOMMIT TRANSACTIONSelect ID,RegionID,Name from BranchOrder by ID descThanks ! M2 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-09-08 : 02:40:52
|
| There is no need for an explicit transaction as this is a single update statement. Apart from that it is fineDECLARE @RegionID intDECLARE @Name varchar(20)DECLARE @msg varchar (255)DECLARE @errorNumber intSet @RegionID = 'A1'Set @Name = '723k'INSERT INTO Branch (RegionID,Name)VALUES (@RegionID,@Name)SET @errorNumber = @@errorIF @errorNumber <> 0 BEGIN SET @msg = 'Error: ' + CAST(@errorNumber as varchar(10)) + 'occurred during insert branch record !'RAISERROR 50000 @msgRETURNENDSelect ID,RegionID,Name from BranchOrder by ID desc==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
dsdeming
479 Posts |
Posted - 2003-09-08 : 08:27:53
|
quote: I want handling error in following insertion operation. Could any one pls help me check whether it's correct or not ? Because I cant test it under Query Analyzer.
Why can't you test it in Query Analyzer? If either Branch.RegionID or Branch.Name is not nullable, passsing a NULL to either of them will generate an error you could trap.Dennis |
 |
|
|
|
|
|