In SQL SERVER 2008 how can i return error messages as select statement
LIKE
SELECT ** FROM emp
Will return the following error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '*'.
i just need the 2nd line to be returned as SELECT statement
I’ve tried @@ERROR but looks like it return just the error code
What i’m doing is validating sql statements from the client side, so if there is a way of doing this with out hitting the server this will be good too i’m using VB
Thanks
You do see
Incorrect syntax near '*'., don’t you? That means it was returned!You may want to know how to retrieve the error message(s) text, and the answer is that you can only capture error that occur during execution (syntax errors are compilation and cannot be captured inside the same batch) and you must use the BEGIN … TRY/ BEGIN … CATCH block. Inside a catch block the
ERROR_MESSAGE()function will return the text of the exception caught.So, to give an example base don your case, wrap the code in a BEGIN TRY/BEGIN CATCH and have the incorrect syntax in a different batch: