I am trying to execute the following code:
THROW 51051, 'I come from the THROW construct :)', 1 ;
The error I am getting is:
Could not find stored procedure 'THROW'.
Isn't the THROW procedure a sytem procedure? Why can't it find it?
Furthhermore, what is the difference between unsing THROW and ErrorState ? Is one older/newer/better than the other?
And what do "ErrorSeverity" and "ErrorState" mean by ErrorState? Can I define them as I will or they are predefined?
According to the Differences Between RAISERROR and THROW in Sql Server:
Both RAISERROR and THROW statements are used to raise an error in Sql Server.
The journey of RAISERROR started from Sql Server 7.0; whereas the journey of the THROW statement has just begun with Sql Server 2012.
Microsoft is suggesting we start using the THROW statement instead of RAISERROR. The THROW statement seems to be simpler and easier to use than RAISERROR.
Yes, it is, but only since 2012. If you're using 2008R2, then it didn't exist.
The definitions of state and severity are clearly documented in the raiserror documentation
Related
I did search for exception 208 (invalid object name) identified by SQL Profiler and found many hints regarding "Deferred Name Resolution". Nevertheless I still did not find an asnwer related to TVP (table valued parameter) and SQL Server 2019.
I´m going to migrate an application from SQL Server 2016 to SQL Server 2019 and I´m wondering why Profiler is starting throwing of hundrets of exceptions per hour with same code. It turned out that all of them are Exception 208 (invalid object name).
Steps for reproduction:
Set compatbility level of database to 140 (2017);
SSMS as well as Profiler runs without any exception.
Set compatbility level of database to 150 (2019);
SSMS runs fine but SQL Profiler throws exception 208 (invalid object name).
Is there any way to get rid of this? If I´m looking for any unexpected exceptions in database I get blind due to that many useless exceptions.
--ALTER DATABASE [...] SET COMPATIBILITY_LEVEL = 140;
ALTER DATABASE [...] SET COMPATIBILITY_LEVEL = 150;
GO
CREATE TYPE dbo.LocationTableType AS TABLE ( LocationName VARCHAR(20) );
GO
DECLARE #LocationTVP AS dbo.LocationTableType;
SELECT * FROM #LocationTVP; -- Throws the exception in profiler
--INSERT INTO #LocationTVP (LocationName) SELECT 'MyLocation'; -- Throws the exception in profiler
GO
DROP TYPE dbo.LocationTableType;
GO
Either INSERT or SELECT statement is throwing an exception. Could anyone let me know how to turn this off in SQL-Server 2019 to be able to further use of SQL profiler.
In Oracle functions we can use Exception handling in the following way
EXCEPTION
WHEN OTHERS THEN
// do some error handling
RETURN NULL;
(Then END for function end -- also function return value returned before the exception part)
Now to execute the same thing in sql server.. we have try and catch logic
How can I mention a condition that will work as 'WHEN OTHERS THEN' as in oracle in sql server.
In oracle 'WHEN OTHERS THEN' catches all other exceptions than the one's mentioned excplicitly
Is there a way to catch all exceptions in this way in sql server
Thanks for the help!
Use TRY CATCH as follows
BEGIN TRY
-- statements that may cause exceptions
END TRY
BEGIN CATCH
-- statements that handle exception
END CATCH
Case Statement and IIF should help
A Java Hibernate application call my stored procedure in SQL Server 2019, through JDBC
The stored procedure has a begin/catch block
Despite I catch the error, the error is reported back to Java as an Exception
and then the application finishes with an error
From the database (and business) point of view, the error was caught, and everything is fine
I need Java application to not trigger an exception, since the error was correctly treated by the stored procedure
Is there any configuration or something (in the SP) I can add?
NOTE: I'm on the DB side, the Java code is not in my scope, it can't be modified
ADDED
I mean, whatever kind of error, for example:
CREATE PROCEDURE SP_TEST
AS
BEGIN TRY
SELECT 1/0 as x
PRINT 'no problem'
END TRY
BEGIN CATCH
SELECT 1 as x
PRINT 'don´t pay attention'
END CATCH
PRINT 'good bye'
return 0 -- always 0 = OK
Despite the error was managed by the Stored Procedure
in some way the client application receives it
I really don't know if it is normal to the driver or the Engine
I am curious to know what these two statements as displayed show a syntax error and cause a syntax error during runtime but run fine when run indepedently. Also adding or removing the semicolon displays a different syntax error in SSMS. Using SSMS 2014 with AventureWorks database. (Statement is for testing purposes. Don't pay attention to the values)
select * from Person.Address;
HumanResources.uspUpdateEmployeeHireInfo 2221,'d', '3/4/1992','3/4/1992', 3,3,1
It is specific to client (SSMS). You can execute stored proc without EXEC when the statement is single statement in the batch.
To avoid the error use:
select * from Person.Address;
EXEC HumanResources.uspUpdateEmployeeHireInfo 2221,'d', '3/4/1992','3/4/1992', 3,3,1
I have encapsulated a backup database command in a Try/Catch and it appears that the error message is being lost somewhere. For example:
BACKUP DATABASE NonExistantDB TO DISK = 'C:\TEMP\NonExistantDB.bak'
..gives error:
Could not locate entry in sysdatabases for database 'NonExistantDB'. No entry found with that name. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally.
Whereas:
BEGIN TRY
BACKUP DATABASE NonExistantDB TO DISK = 'C:\TEMP\NonExistantDB.bak'
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH
... only gives error: BACKUP DATABASE is terminating abnormally.
Is there a way to get the full error message or is this a limitation of try/catch?
It's a limitation of try/catch.
If you look carefully at the error generated by executing
BACKUP DATABASE NonExistantDB TO DISK = 'C:\TEMP\NonExistantDB.bak'
you'll find that there are two errors that get thrown. The first is msg 911, which states
Could not locate entry in sysdatabases for database 'NonExistantDB'. No entry
found with that name. Make sure that the name is entered correctly.
The second is the 3013 message that you are displaying. Basically, SQL is only returning the last error.
It is a limitation, that I just ran into myself, of the try/catch block in SQL 2005. I don't know if it still exists or not in 2008.
SQL 2005 Error Handling