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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Feed back on my first try catch SQL script please

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-04-14 : 19:19:06
So I converted an existing script to have Try Catch and would like the experts thoughts on it please

quote:
CREATE PROC dbo.spRaceInfo
@UpdateType AS char(6),
@RaceID AS int = NULL,
@SectionID AS smallint = NULL,
@RaceName AS varchar(50) = NULL,
@ImagePath AS varchar(50) = NULL,
@RaceDescription AS varchar(MAX) = NULL
AS

SET NOCOUNT ON;
SET XACT_ABORT ON;

BEGIN TRY
BEGIN TRANSACTION;

--Process a new race
IF (@UpdateType = 'Insert')
BEGIN
INSERT INTO dbo.RaceInfo (SectionID, RaceName, ImagePath, RaceDescription)
VALUES (@SectionID, @RaceName, @ImagePath, @RaceDescription);

EXEC dbo.spAddSectionMenuItem @SectionID, 'Info', 'Races', 'Races', 'Race Information';

INSERT INTO dbo.SiteUpdates (UpdatedSectionID, UpdatedSubSectionName, UpdatedItemName, UpdateType)
VALUES (@SectionID, 'Races', @RaceName, @UpdateType);
END
ELSE IF (@UpdateType = 'Update')
--Process a race update
BEGIN
UPDATE dbo.RaceInfo
SET SectionID = @SectionID,
RaceName = @RaceName,
ImagePath = COALESCE(@ImagePath, ImagePath),
RaceDescription = @RaceDescription
WHERE RaceID = @RaceID;

INSERT INTO dbo.SiteUpdates (UpdatedSectionID, UpdatedSubSectionName, UpdatedItemName, UpdateType)
VALUES (@SectionID, 'Races', @RaceName, @UpdateType);
END
ELSE IF (@UpdateType = 'Delete')
--Process race removal
BEGIN
DELETE dbo.RaceInfo
WHERE RaceID = @RaceID;
END

COMMIT

END TRY

BEGIN CATCH

IF ((XACT_STATE()) = -1)
BEGIN
ROLLBACK;
END
ELSE IF ((XACT_STATE()) = 1)
BEGIN
COMMIT;
END

END CATCH


--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-15 : 15:08:27
code looks fine to me

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-04-15 : 17:54:32
quote:
Originally posted by visakh16

code looks fine to me

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Thanks asked a lot of questions on here to get that script done. Glad to this board is so helpfull

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page
   

- Advertisement -