Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys i need your help, i am trying to generate custom sql error messages, but its not working very wull.
There are 2 custom generated messages ErrorMsg and ErrorMsg1, ErrorMsg workes fine, but ErrorMsg1 doesn't. Somehow it keep getting overwritten by the default sql error message.

And sorry about the sql, the space allotted is not sufficient for proper spacing of the sql.

SQL
USE [AssetTracker]
GO
/****** Object:  StoredProcedure [dbo].[Populate_Asset_Table]    Script Date: 11/11/2013 13:17:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--DROP Procedure [dbo].[Populate_Asset_Table]
ALTER Procedure [dbo].[Populate_Asset_Table]

@AssetIdentifier varchar(50),
@AssetCondition int,
@SchoolID int,
@AssetCategoryID int,
@VendorID int,
@AssingedPersonID int,
@AssetStatusID  int,
@ManufacturerID int,
@ModelDetail varchar(50),
@CreatedOn datetime,
@Notes varchar(max)

AS

DECLARE @CreatedByIdentifier nvarchar(50),@ModifiedByIdentifier nvarchar(50),@ModifiedOn datetime,@ErrorMsg nvarchar(500)

    SET @CreatedByIdentifier  =(SELECT SUSER_NAME())
    SET @ModifiedByIdentifier =(SELECT SUSER_NAME())
    SET @ModifiedOn           = (SELECT GETDATE())
    SET @ErrorMsg = 'The Asset with this identifier:'+@AssetIdentifier +',already exists in this table.'
    
BEGIN

    
	IF EXISTS(SELECT AssetIdentifier FROM AssetTracker_Asset WHERE AssetIdentifier = @AssetIdentifier)
	   BEGIN
		RAISERROR(@ErrorMsg, 10, 1) --change to > 10
		RETURN --exit now
	   END
    ELSE
	   BEGIN	
	      BEGIN TRY
                   INSERT INTO AssetTracker_Asset VALUES(@AssetIdentifier,@AssetCondition,@SchoolID,@AssetCategoryID,@VendorID,@AssingedPersonID,
				@AssetStatusID,@ManufacturerID,@ModelDetail,@CreatedOn,@CreatedByIdentifier,@ModifiedOn,@ModifiedByIdentifier,@Notes)
	      END TRY
	      BEGIN CATCH
		DECLARE @ErrorNum int, @ErrorMessage nvarchar(4000), @ErrorMsg1 nvarchar(500)
		SELECT @ErrorNum = ERROR_NUMBER()
		SELECT @ErrorMessage = ERROR_MESSAGE()
		SET @ErrorMsg1 ='Error Number:'+@ErrorNum + 'And Error Message:'+@ErrorMessage +'. Occured in Populate_Asset_Table while inserting these elments'
			    
	        RAISERROR (@ErrorMsg1,10,1) 
    	        RETURN
	      END   CATCH
             END
END


Thanks for your help
Posted
Updated 12-Nov-13 2:46am
v2

1 solution

You need to convert varchar to nvarchar when concatenating, like this:

SQL
SET @ErrorMsg = 'The Asset with this identifier:'+CONVERT(NVARCHAR(100),@AssetIdentifier)+',already exists in this table.'
 
Share this answer
 
Comments
rudolph098 12-Nov-13 8:45am    
No, I said @ErrorMsg works fine, its @ErrorMsg1 that doesn't
chaau 12-Nov-13 9:54am    
What are you doing to generate the erroneous situation? Obviously it is not an integrity violation. Are you generating a deadlock situation?
chaau 12-Nov-13 9:59am    
If not the custom error, what error are you receiving?
rudolph098 12-Nov-13 10:21am    
I want to my custom message to catch when a bad date is entered (1900-1-0). Now MSQL management studio already does this by outputting : Msg 8114 , Error converting data type nvarchar to datetime. But I want my custom error to also output the bad data that caused it. Because my boss is going to dump a lot of queries for execution and some will have this bad data.
chaau 12-Nov-13 13:41pm    
I think in this case the try catch block will not be executed, as the severity of the error is too low. Try to use @@ERROR http://technet.microsoft.com/en-us/library/ms188790.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900