I wanted to user RAISERROR in my SQL-Server codes but I noticed that some status code have interruption with built-in SQL Error statuses...
for example status code "1" is used for "Devide By Zero Error", And I dont wanna use it any more...
now I want to see what status codes are safe to use withour interruption ?
If you want to use your own msgid use a value of greater then 50000. The values below 50000 are reserved for built in messages. To see the list of existing messages you can do a 'select * from sys.messages'. 50000 is used when a value for msgid is not provided.
Related
While creating Kinesis Analytics application it successfully discovered my schema based on the data. However, when I hit save and continue, I get following error
Error updating application There was an issue updating your
application. Error message: 1 validation error detected: Value 'C' at
'input.inputSchema.recordColumns.2.member.name' failed to satisfy
constraint: Member must satisfy regular expression pattern:
[a-zA-Z][a-zA-Z0-9_]+
my sample record is below
{"reported": {"timestamp": "1482231365", "C": "40", "id": "D_aa-bb"}}
My bad, I overlooked the error message. Found the solution, hope it might help someone.
The auto detected schema name was the issue. From the sample record, the auto detected column name was C and the regex says it should contains atleast two characters. After editing the schema manually with two characters it succeeded.
There was another issue though, the auto detected column name timestamp is a reserved keyword, which we need to change.
I have a process and the first step is to check status of other process. If the other process is done I would want to run the rest steps and if not I would want to quit the job.
I have a table that looks into the status and if all done it will mark as 'done'. So what should I put in the first step so that when the status is not 'done', it makes step 1 fail?
There are lots of ways you can force sql-server to throw (or raise) an error but I would recommend using THROW for SQL-Server 2012 + or RAISERROR below that as that is your actual intention. So you could do something like:
IF EXISTS(SELECT * FROM StatusTable WHERE status <> 'done')
BEGIN
;THROW 51000, 'Process Is Not Finished', 1
END
You can use also "Select 1/0" this will parse but it will giving error while execution
I feel it is bad practice to fail a job-step to implement application logic. Better to use IF (some condition) THEN (some job-step action) ELSE (print an explanation why there was no job-step action). And reserve failures for operational issues.
I wanna know output list of sp_send_dbmail, for example when I run sp_send_dbmail and the output is 0 it was successful, also if the output is 20, it did not find the recipients.
So I want to know if there is a list of output for sp_send_dbmail
According to MS Documentation, sp_send_dbmail() returns 0 if the process of sending mails could be started without error. This means, mails have been set in the database mail queue, and for every mail the mailitem_id for the message is returned in the result set.
Any return value other than 0 means error. The precise error code of the failed statement can be seen in ##ERROR.
After using sp_send_dbmail(), the mail is sent asynchronously and it still can fail for many reasons. So if sp_send_dbmail() returns 0, it does not mean final success.
You might look in msdb.dbo.sysmail_faileditems as in the case of error, the mails would be there (http://www.sqlservercentral.com/Forums/Topic1030781-23-1.aspx).
SqlException has the property Number.
Then there is this: http://msdn.microsoft.com/en-us/library/cc645603.aspx
and this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
And it seems to be one or the other
QUESTION:
How is it decided which?
REASON FOR ASKING:
I need to catch certain SqlExceptions and decide how to deal with them based on the Number property but I don't know which list I should look at when it seems like the system is using messages from both, and I don't know what criteria is used for choosing.
For example:
Number 53 - from server error message list (exists on both)
Number 10054 - from system error message list (exists on both)
Number -1 - from server error message list (exists only on server list)
Number 121 - from system error message list (exists on both)
......
The theory goes that it's the SQL error number, eg. the server side ERROR_NUMBER(). In other words, the first list.
However there are a number of exceptions reported by SqlClient that occur on the client side, not on the server side. A typical example would be an error like failure to connect to the server, since you did not connect there is no server side error to speak of. For example a bad server name (does not resolve in DNS), in such cases the InnerException will point toward a Win32Exception with NativeErrorCode value of ERROR_BAD_NETPATH. In this case 53, the OS system error code, will be reported as SqlException error number.
Other cases the error reported by the SqlClient is a socket error, like an abrupt disconnect. Again, there is no 'server side' error to speak of, and the SqlException will wrap an InnerException of type SocketException (a subclass of Win32Error) with the NativeErrorCode of one of the well known WSA error numbers, like WSAECONNRESET. In this case the SqlException.ErrorNumber will be 10054, but it's the 10054 from the WSA range, not the 10054 from the SQL Server errors range. I know...
So what are you supposed to do? Make sure you check the InnerException, if it's a Win32Exception then the ErrorNumber is coming from a system error code (OS error). Otherwise it should be a SQL Server error number.
Oh, and then there is -1... I think that is reported by SqlClient itself (eg. some internal state errors) but I'm not sure.
I would look at the documentation for the SqlException.Number property.
This is what it says
This is a wrapper for the Number property of the first SqlError in the
Errors property. For more information on SQL Server engine errors, see
SQL Server Books Online.
Why does the PRINT statement in T-SQL seem to only sometimes work? What are the constraints on using it? It seems sometimes if a result set is generated, it becomes a null function, I assumed to prevent corrupting the resultset, but could it's output not go out in another result set, such as the row count?
So, if you have a statement something like the following, you're saying that you get no 'print' result?
select * from sysobjects
PRINT 'Just selected * from sysobjects'
If you're using SQL Query Analyzer, you'll see that there are two tabs down at the bottom, one of which is "Messages" and that's where the 'print' statements will show up.
If you're concerned about the timing of seeing the print statements, you may want to try using something like
raiserror ('My Print Statement', 10,1) with nowait
This will give you the message immediately as the statement is reached, rather than buffering the output, as the Query Analyzer will do under most conditions.
The Print statement in TSQL is a misunderstood creature, probably because of its name. It actually sends a message to the error/message-handling mechanism that then transfers it to the calling application. PRINT is pretty dumb. You can only send 8000 characters (4000 unicode chars). You can send a literal string, a string variable (varchar or char) or a string expression. If you use RAISERROR, then you are limited to a string of just 2,044 characters. However, it is much easier to use it to send information to the calling application since it calls a formatting function similar to the old printf in the standard C library. RAISERROR can also specify an error number, a severity, and a state code in addition to the text message, and it can also be used to return user-defined messages created using the sp_addmessage system stored procedure. You can also force the messages to be logged.
Your error-handling routines won’t be any good for receiving messages, despite messages and errors being so similar. The technique varies, of course, according to the actual way you connect to the database (OLBC, OLEDB etc). In order to receive and deal with messages from the SQL Server Database Engine, when you’re using System.Data.SQLClient, you’ll need to create a SqlInfoMessageEventHandler delegate, identifying the method that handles the event, to listen for the InfoMessage event on the SqlConnection class. You’ll find that message-context information such as severity and state are passed as arguments to the callback, because from the system perspective, these messages are just like errors.
It is always a good idea to have a way of getting these messages in your application, even if you are just spooling to a file, because there is always going to be a use for them when you are trying to chase a really obscure problem. However, I can’t think I’d want the end users to ever see them unless you can reserve an informational level that displays stuff in the application.
Query Analyzer buffers messages. The PRINT and RAISERROR statements both use this buffer, but the RAISERROR statement has a WITH NOWAIT option. To print a message immediately use the following:
RAISERROR ('Your message', 0, 1) WITH NOWAIT
RAISERROR will only display 400 characters of your message and uses a syntax similar to the C printf function for formatting text.
Please note that the use of RAISERROR with the WITH NOWAIT option will flush the message buffer, so all previously buffered information will be output also.
I recently ran into this, and it ended up being because I had a convert statement on a null variable. Since that was causing errors, the entire print statement was rendering as null, and not printing at all.
Example - This will fail:
declare #myID int=null
print 'First Statement: ' + convert(varchar(4), #myID)
Example - This will print:
declare #myID int=null
print 'Second Statement: ' + coalesce(Convert(varchar(4), #myID),'#myID is null')
For the benefit of anyone else reading this question that really is missing print statements from their output, there actually are cases where the print executes but is not returned to the client. I can't tell you specifically what they are. I can tell you that if you put a go statement immediately before and after any print statement, you will see if it is executed.
Do you have variables that are associated with these print statements been output? if so, I have found that if the variable has no value then the print statement will not be ouput.