dshi15
asked on
RAISERROR function in Sql server
Hi Expert,
When I run
execute InsertPerson_Test 1234, 1234, '11/1/2013','11/1/2013'
1. Why no error? firstname is nvarchar, but I put number.
2. How to use RAISERROR function?
thank you in advance.
SP as following
Alter PROCEDURE [dbo].[InsertPerson_Test]
@LastName nvarchar(50),
@FirstName nvarchar(50),
@HireDate datetime,
@EnrollmentDate datetime
AS
BEGIN TRY
BEGIN TRAN
INSERT INTO dbo.Person (LastName,
FirstName,
HireDate,
EnrollmentDate)
VALUES (@LastName,
@FirstName,
@HireDate,
@EnrollmentDate);
COMMIT
SELECT SCOPE_IDENTITY() as NewPersonID;
End TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK
DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE(),@ErrorSeve rity = ERROR_SEVERITY(),@ErrorSta te = ERROR_STATE();
SET @ErrorMessage = '<span style=''color:red''>' + @ErrorMessage + '</span>'
RAISERROR( @ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH
When I run
execute InsertPerson_Test 1234, 1234, '11/1/2013','11/1/2013'
1. Why no error? firstname is nvarchar, but I put number.
2. How to use RAISERROR function?
thank you in advance.
SP as following
Alter PROCEDURE [dbo].[InsertPerson_Test]
@LastName nvarchar(50),
@FirstName nvarchar(50),
@HireDate datetime,
@EnrollmentDate datetime
AS
BEGIN TRY
BEGIN TRAN
INSERT INTO dbo.Person (LastName,
FirstName,
HireDate,
EnrollmentDate)
VALUES (@LastName,
@FirstName,
@HireDate,
@EnrollmentDate);
COMMIT
SELECT SCOPE_IDENTITY() as NewPersonID;
End TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK
DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE(),@ErrorSeve
SET @ErrorMessage = '<span style=''color:red''>' + @ErrorMessage + '</span>'
RAISERROR( @ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH
ASKER
I add int Phonenumber to the table and tested
with no error either.
execute InsertPerson_Test 1234, 1234, '11/1/2013','11/1/2013','1 23456789'
with no error either.
execute InsertPerson_Test 1234, 1234, '11/1/2013','11/1/2013','1
There is an implicit conversion from a numeric datatype to a string type. Obviously as Raj mentions above it doesn't work in reverse.
How would you like it to respond. You could use ISNUMERIC function to check that specific case but trying to validate someone's name is futile.
If you want something more specific on Raiserror please be more specific in your request.
How would you like it to respond. You could use ISNUMERIC function to check that specific case but trying to validate someone's name is futile.
2. How to use RAISERROR function?http://msdn.microsoft.com/en-us/library/ms178592.aspx
If you want something more specific on Raiserror please be more specific in your request.
ASKER
thank you BriCrowe, could you please chang SP, so I can see where and how the error displayed?
for example I run
execute InsertPerson_Test 1234, 1234, 11/1/2013,'11/1/2013','123 456789'
the error show on Message panel is following.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '/'.
for example I run
execute InsertPerson_Test 1234, 1234, 11/1/2013,'11/1/2013','123
the error show on Message panel is following.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '/'.
ASKER
Here is line I changed in SP, color from red to blue, but in Message panel it is still red.
SET @ErrorMessage = '<span style=''color:blue''>' + @ErrorMessage + '</span>'
SET @ErrorMessage = '<span style=''color:blue''>' + @ErrorMessage + '</span>'
You are missing the opening quote on the third parameter.
execute InsertPerson_Test 1234, 1234, '11/1/2013,'11/1/2013','12 3456789'
You also appear to have more parameters (5) listed than the procedure takes (4). I would also recommend being explicit in your parameters though it's not critical in test situation such as this.
EXECUTE InsertPerson_Test @LastName = 1234,
@FirstName = 1234,
@HireDate = '11/1/2013',
@EnrollmentDate = '11/1/2013'
execute InsertPerson_Test 1234, 1234, '11/1/2013,'11/1/2013','12
You also appear to have more parameters (5) listed than the procedure takes (4). I would also recommend being explicit in your parameters though it's not critical in test situation such as this.
EXECUTE InsertPerson_Test @LastName = 1234,
@FirstName = 1234,
@HireDate = '11/1/2013',
@EnrollmentDate = '11/1/2013'
ASKER
I think you misunderstand my question.
I know where is wrong. I like to know how to use RAISERROR to display the error.
here is SP.
Alter PROCEDURE [dbo].[InsertPerson_Test]
@LastName nvarchar(50),
@FirstName nvarchar(50),
@HireDate datetime,
@EnrollmentDate datetime,
@PhoneNo int
AS
BEGIN TRY
BEGIN TRAN
INSERT INTO dbo.Person (LastName,
FirstName,
HireDate,
EnrollmentDate,
PhoneNumber)
VALUES (@LastName,
@FirstName,
@HireDate,
@EnrollmentDate,
@PhoneNo);
COMMIT
SELECT SCOPE_IDENTITY() as NewPersonID;
End TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK
DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE(),@ErrorSeve rity = ERROR_SEVERITY(),@ErrorSta te = ERROR_STATE();
SET @ErrorMessage = '<span style=''color:blue''>' + @ErrorMessage + '</span>'
RAISERROR( @ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH
I know where is wrong. I like to know how to use RAISERROR to display the error.
here is SP.
Alter PROCEDURE [dbo].[InsertPerson_Test]
@LastName nvarchar(50),
@FirstName nvarchar(50),
@HireDate datetime,
@EnrollmentDate datetime,
@PhoneNo int
AS
BEGIN TRY
BEGIN TRAN
INSERT INTO dbo.Person (LastName,
FirstName,
HireDate,
EnrollmentDate,
PhoneNumber)
VALUES (@LastName,
@FirstName,
@HireDate,
@EnrollmentDate,
@PhoneNo);
COMMIT
SELECT SCOPE_IDENTITY() as NewPersonID;
End TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK
DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE(),@ErrorSeve
SET @ErrorMessage = '<span style=''color:blue''>' + @ErrorMessage + '</span>'
RAISERROR( @ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH
ASKER
I changed line again, but I didn't see where it is displayed
SET @ErrorMessage = 'My message' + '<span style=''color:blue''>' + @ErrorMessage + '</span>'
SET @ErrorMessage = 'My message' + '<span style=''color:blue''>' + @ErrorMessage + '</span>'
I think I see where you're going with this. You are trying to define error message formatting in SQL Server. Where are you trying to display the error? Are you calling this procedure from code somewhere?
ASKER
yes. in C# code, I try to learn how to use RAISERROR function, you mean RaiseError in sql then I display it in other place, not sql anymore.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
got it. for example, I run
execute InsertPerson_Test 1234, 1234, 11/1/2013,'11/1/2013','123 456789'
the error in message panel (Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '/'. ) can be formatted and display in my c# code ( for example a label)
execute InsertPerson_Test 1234, 1234, 11/1/2013,'11/1/2013','123
the error in message panel (Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '/'. ) can be formatted and display in my c# code ( for example a label)
If you pass a number value to varchar procedure parameter, there won't be problem I guess. But if you pass a string value (non numeric) to a numeric parameter, there would be a error
Raj