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 |
Eagle_f90
Constraint Violating Yak Guru
424 Posts |
Posted - 2012-05-05 : 06:20:28
|
I have a proc that I have used try/catch for error handeling and it is was giving me an error MSG of "Can not insert null into SectionID" with a line error of 63. The problem is that lin 68 does not call an insert, it is the @@ErrorSeverity line. Can anyone helpme modify the script so I can get more accurate details of where the error is at?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) = NULLASSET 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 COMMITEND TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); IF ((XACT_STATE()) = -1) BEGIN ROLLBACK; END ELSE IF ((XACT_STATE()) = 1) BEGIN COMMIT; ENDEND CATCH -- If I get used to envying others...Those things about my self I pride will slowly fade away.-Stellvia |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-05 : 09:58:56
|
What I have observed (and it is just an observation, I have not seen it documented anywhere) is that the line numbers start immediately after the GO statement preceding the create statement of the stored proc.So if you script the stored procedure (right-click on the stored proc name in object explorer in SSMS and select Script Procedure as -> Create To -> New Window) and delete everything starting at the first line up to the GO statement immediately preceding the CREATE PROCEDURE statement (but keep the blank lines between the GO and the CREATE statements), then the line numbers should match up what you see in the error message. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-05 : 10:35:24
|
Also it would make more sense for us if post how you're executing this proc ie what are values passed for parameters------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Eagle_f90
Constraint Violating Yak Guru
424 Posts |
Posted - 2012-05-07 : 09:48:57
|
quote: Originally posted by sunitabeck What I have observed (and it is just an observation, I have not seen it documented anywhere) is that the line numbers start immediately after the GO statement preceding the create statement of the stored proc.So if you script the stored procedure (right-click on the stored proc name in object explorer in SSMS and select Script Procedure as -> Create To -> New Window) and delete everything starting at the first line up to the GO statement immediately preceding the CREATE PROCEDURE statement (but keep the blank lines between the GO and the CREATE statements), then the line numbers should match up what you see in the error message.
Do you know how to compinsate for this? In VS 11 when I right click and do view code it does not have a go statment the code is just as displayed in my first postquote: Originally posted by visakh16 Also it would make more sense for us if post how you're executing this proc ie what are values passed for parameters------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
The proc is called from asp.net page (I can do an exec proc also) passing the parameters "Insert", null, 1, "Test1", null, "Test1"-- 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-05-07 : 17:13:22
|
are you sure that you've mapped parameters in correct order in place where app passes it to procedure? Also are any of those parameters of OUTPUT type?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Eagle_f90
Constraint Violating Yak Guru
424 Posts |
Posted - 2012-05-08 : 16:31:41
|
I am sure I mapped them right and there are no output parameters. I am also sure there is a fundmental flaw in my SQL code but with out better error details I can not find it, which is why my original request was for improving the error handeling.-- If I get used to envying others...Those things about my self I pride will slowly fade away.-Stellvia |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-05-08 : 16:37:54
|
There are a couple of things you might try:1. If you know that a parameter/column has a constraint (like it cannot be NULL), then you can perform validation and raise an error accordingly.2. You could add another variable (like Step or something) and assign it a value before each Select/Insert/Update/Delete. Then you can add that to you error message to help show where the error occured.3. If you have Visual Studio or other playform that you can use for debugging, you can debug the stored procedure and see exactly where it is failing. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-08 : 16:46:10
|
Actual your posted code doesnt seem to have any obvious errors. But we have some dark areas like dbo.spAddSectionMenuItem where we dont know what exactly is happening so not sure whether error is getting generated from it------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Eagle_f90
Constraint Violating Yak Guru
424 Posts |
Posted - 2012-05-10 : 13:49:49
|
But why is the error message returning the wrong line number?dbo.spAddSEctionMenuItemCREATE PROC dbo.spAddSectionMenuItem @SectionID AS smallint, @MenuType AS char(5), @LinkRoute AS varchar(50), @LinkName AS varchar(50), @LinkTitle AS varchar(250)ASSET NOCOUNT ON;SET XACT_ABORT ON;BEGIN TRY IF NOT EXISTS (SELECT 1 FROM dbo.SectionMenuItems WHERE SectionID = @SectionID and MenuType = @MenuType) BEGIN INSERT INTO dbo.SectionMenuItems (SectionID, MenuType, LinkRoute, LinkName, LinkTitle) VALUES (@SectionID, @MenuType, @LinkRoute, @LinkName, @LinkTitle); ENDEND TRYBEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);END CATCH -- If I get used to envying others...Those things about my self I pride will slowly fade away.-Stellvia |
 |
|
|
|
|
|
|