Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Why isn't the second error message being shown?

Author  Topic 

kfluffie
Posting Yak Master

103 Posts

Posted - 2011-12-13 : 02:05:03
Hi,
BEGIN TRANSACTION
DECLARE @xmlDoc XML

SET @xmlDoc =(
SELECT * FROM OPENROWSET (
BULK 'D:\Studier\MCTS\ML326C - XML i Relationsdatabaser\Projektfiler\faktura_10.xml', SINGLE_BLOB
) AS xmlData
)

INSERT INTO Faktura.XMLFaktura(faktura)
SELECT @xmlDoc

IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
PRINT 'FAIL'
RETURN
END



/*Faktura_ID ska sättas in ifall allt gick bra ifrån XMLFaktura till faktura_id*/
INSERT INTO Faktura.Faktura(faktura_id, lev_orgnr, kopare, anst_id, fakturanr, datum, totalsum,totalmoms)
SELECT
SCOPE_IDENTITY(),
@xmlDoc.value('(/FAKTURA/FÖRETAG/ORGNR)[1]', 'char(15)'),
@xmlDoc.value('(/FAKTURA/KÖPARE/FÖRETAGSNAMN)[1]', 'char(10)'),
--@xmlDoc.value('(/FAKTURA/KÖPARE/BESTÄLLARE)[1]', 'char(41)') as namnXML,
(SELECT top 1 anst_id FROM HR.Anställd
WHERE LTRIM(RTRIM(fornamn)) + ' ' + LTRIM(RTRIM(efternamn)) = @xmlDoc.value('(/FAKTURA/KÖPARE/BESTÄLLARE)[1]', 'CHAR(41)')),
@xmlDoc.value('(/FAKTURA/FAKTURA_DETALJER/FAKTURA_NR)[1]', 'int'),
@xmlDoc.value('(/FAKTURA/FAKTURA_DETALJER/DATUM)[1]', 'smalldatetime'),
@xmlDoc.value('(/FAKTURA/FAKTURA_DETALJER/TOTAL_SUMMA)[1]', 'char(20)'),
@xmlDoc.value('(/FAKTURA/FAKTURA_DETALJER/TOTAL_MOMS)[1]', 'char(20)')

IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
PRINT 'FAIL2'
RETURN
END


COMMIT TRANSACTION
PRINT 'Faktura importerad!'


The first error message is being shown if there is any problem however not the second one. Why is that? -.-

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 02:13:55
you mean you need to see second error message also in case first error block gets executed?
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kfluffie
Posting Yak Master

103 Posts

Posted - 2011-12-13 : 02:26:34
Hi,
If the first INSERT statement is being executed correctly but the second not a ROLLBACK needs to be done on both i.e. no records should be inserted. If the first error message popups, which it does, the ROLLBACK has already been done. However when I am testing out this statement I got this error message:

(1 row(s) affected)
Msg 245, Level 16, State 1, Line 29
Conversion failed when converting the nvarchar value 'abc' to data type int.


I can see in my table that the first statement (INSERT INTO XMLFaktura) wasn't executed but I do not understand why I don't see the second fail message.
Go to Top of Page

kfluffie
Posting Yak Master

103 Posts

Posted - 2011-12-13 : 02:43:58
The thing the second message is being shown sometimes. For example when the value in my XML document is to big it is being shown:
(1 row(s) affected)
Msg 248, Level 16, State 1, Line 27
The conversion of the nvarchar value '123123123123123123' overflowed an int column.
The statement has been terminated.
FAIL2


However when it contains characters it doesn't show:

(1 row(s) affected)
Msg 245, Level 16, State 1, Line 27
Conversion failed when converting the nvarchar value 'abc' to data type int.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 04:10:22
The reason is this
Overflow error is statement abortion type of error in SQL server. That is, in case you've a conversion error happening in the code, it will only abort the statement causing error and continue with rest of statements
In latter case, its a type conversion error and its scope is batch (batch abortion error). So any such error happens in a batch of code it will abort entire batch and wont execute any statements further.

See the difference in illustration below

query 1
---------------------------
declare @x int

set @x=346664654376576868689797809000

select 'test'



this will abort only statement and you will get test message also printed

query 2
----------------

declare @x int

set @x='fergrtght5hyjuyjkutkuilyililioluil'

select 'test'

this will abort batch altogether and you will just get error message alone



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kfluffie
Posting Yak Master

103 Posts

Posted - 2011-12-13 : 05:36:06
Thanks for the explanation!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 06:12:59
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -