|
|
Home » U++ Library support » U++ SQL » MSSQL error management
MSSQL error management [message #49477] |
Fri, 16 February 2018 10:36  |
Giorgio
Messages: 218 Registered: August 2015
|
Experienced Member |
|
|
Hi there,
to execute SQL queries I use code like this:
bool ImportData::InsertBody(myS_Table record)
{
Sql sql(mssql);
sql.ClearError();
try { sql * Insert(My_Table)
(My_Id, record.Id)
(My_Row, record.Row)
(My_Description, record.Description);
return true;
} catch(SqlExc) {
ErrorOK(t_("Failed adding data to the database due to the following error: ") + SQL.GetLastError());
return false;
}
return true;
}
In this way, (1) if there are errors in executing the query the user is notified of the specific reason and (2) I can control the program flow using the true/false value returned by the method.
This is working as expected for MySql, PostgreSql and SQLite, but here it comes Microsoft and its @#!&$ MS Sql Server.
When there is an error (e.g. a duplicate key) the user is NOT notified and the query returns always true. The only way to catch errors is looking at the log file activated using .LogErrors() and SetTrace(). This is not really user friendly. In case of an error what I got in the log file is the following:
ERROR [Microsoft][SQL Server Native Client 11.0][SQL Server]Violation of PRIMARY KEY constraint 'Id'. Cannot insert duplicate key in object 'dbo.X_DORIG'. The duplicate key value is (180107, 1, 'Art. number' ).(0): insert into X_DORIG(Id, Row, Description,) values (180107, 1, 'Art. number')
I tried using both Driver={SQL Server Native Client 11.0}; and Driver={SQL Server}; with the same result.
Is there a workaround for this issue?
Thanks,
Gio
[Updated on: Fri, 16 February 2018 10:36] Report message to a moderator
|
|
|
Re: MSSQL error management [message #49532 is a reply to message #49477] |
Sat, 24 February 2018 19:21   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
Giorgio wrote on Fri, 16 February 2018 10:36Hi there,
to execute SQL queries I use code like this:
bool ImportData::InsertBody(myS_Table record)
{
Sql sql(mssql);
sql.ClearError();
try { sql * Insert(My_Table)
(My_Id, record.Id)
(My_Row, record.Row)
(My_Description, record.Description);
return true;
} catch(SqlExc) {
ErrorOK(t_("Failed adding data to the database due to the following error: ") + SQL.GetLastError());
return false;
}
return true;
}
In this way, (1) if there are errors in executing the query the user is notified of the specific reason and (2) I can control the program flow using the true/false value returned by the method.
This is working as expected for MySql, PostgreSql and SQLite, but here it comes Microsoft and its @#!&$ MS Sql Server.
When there is an error (e.g. a duplicate key) the user is NOT notified and the query returns always true. The only way to catch errors is looking at the log file activated using .LogErrors() and SetTrace(). This is not really user friendly. In case of an error what I got in the log file is the following:
ERROR [Microsoft][SQL Server Native Client 11.0][SQL Server]Violation of PRIMARY KEY constraint 'Id'. Cannot insert duplicate key in object 'dbo.X_DORIG'. The duplicate key value is (180107, 1, 'Art. number' ).(0): insert into X_DORIG(Id, Row, Description,) values (180107, 1, 'Art. number')
I tried using both Driver={SQL Server Native Client 11.0}; and Driver={SQL Server}; with the same result.
Is there a workaround for this issue?
Thanks,
Gio
You do have exception active (SqlSession::ThrowOnError), right?
Mirek
|
|
|
|
|
|
|
|
|
Re: MSSQL error management [message #49634 is a reply to message #49632] |
Mon, 19 March 2018 12:58   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
Giorgio wrote on Mon, 19 March 2018 12:40Hi Mirek,
mssql is defined as MSSQLSession: this application basically retrieves data from a sqlite db and put them into a MS SQL db, so I have two different sql sessions and I do not use the global SQL session.
The SqlSession:: before the ThrowOnError was a typo and I removed it.
I tried to put the DDUMP but I get a really weird error from the compiler: "error C2018: unknown character '0x40'". This error pops up on the lines "DDUMP(b);" and "DDUMP(session.throwonerror);", if I remove those lines the application compiles. I opened the .cpp file with an hex editor and did not find any 0x40 char. Also I updated the compiler to VS2017 (previously I used 2015), bu the error still appears.
Regards,
gio
This is because you are compiling in release mode. "DDUMP" is only supposed to be temporary debugging thing, in debug mode only.
Either compile as debug, or use RDUMP instead.
(Overview:
DDUMP, DLOG - logs in debug, prevents compilation in release mode - that is to force you to remove temporary dumps before the release
LOG, DUMP - logs in debug, NOP in release
RLOG, RDUMP - logs in both debug and release
)
Mirek
|
|
|
|
Re: MSSQL error management [message #49636 is a reply to message #49635] |
Mon, 19 March 2018 16:35   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
Giorgio wrote on Mon, 19 March 2018 14:50Hi Mirek,
this is the log:
b = false
session.throwonerror = true
Regards,
gio
Well, if you check the next line, 'throw' should have happened...
I would say something else must be going on here. Please try to add more logs to your code, e.g.:
SqlSchema sch(MSSQL);
DLOG("A");
StdStatementExecutor se(SQL.GetSession());
DLOG("B"); All_Tables(sch);
ODBCPerformScript(sch.Upgrade(), se);
DLOG("C");
ODBCPerformScript(sch.Attributes(), se);
DLOG("D");
mssql.ThrowOnError();
DLOG("E");
try {
DLOG("F");
for(int i = 0; i < 10; i++)
SQL * Insert(TEST)(ID, i)(TEXT, String('A' + i, 1));
DLOG("G"); }
catch(SqlExc) {
DLOG("ERROR");
}
DLOG("H");
S_TEST tst;
Sql sql;
sql * Select(tst).From(TEST);
while(sql.Fetch(tst))
DDUMP(tst.ID);
and send the whole log....
Mirek
|
|
|
Re: MSSQL error management [message #49640 is a reply to message #49636] |
Tue, 20 March 2018 11:15   |
Giorgio
Messages: 218 Registered: August 2015
|
Experienced Member |
|
|
Hi mirek,
this is the log:
b = true
session.throwonerror = false
A
B
create table TEST ( ID integer primary key identity )
ERROR [Microsoft][ODBC SQL Server Driver][SQL Server]There is already an object named 'TEST' in the database.(0): create table TEST ( ID integer primary key identity )
b = false
session.throwonerror = false
alter table TEST add TEXT varchar(200)
ERROR [Microsoft][ODBC SQL Server Driver][SQL Server]Column names in each table must be unique. Column name 'TEXT' in table 'TEST' is specified more than once.(0): alter table TEST add TEXT varchar(200)
b = false
session.throwonerror = false
alter table TEST alter column TEXT varchar(200)
b = true
session.throwonerror = false
C
alter table TEST add constraint PK_TEST$ID primary key (ID) create index PKX_TEST$ID on TEST(ID)
ERROR [Microsoft][ODBC SQL Server Driver][SQL Server]Table 'TEST' already has a primary key defined on it.
[Microsoft][ODBC SQL Server Driver][SQL Server]Could not create constraint or index. See previous errors.(0): alter table TEST add constraint PK_TEST$ID primary key (ID) create index PKX_TEST$ID on TEST(ID)
b = false
session.throwonerror = false
create index IDX_TEST$TEXT on TEST(TEXT)
ERROR [Microsoft][ODBC SQL Server Driver][SQL Server]The operation failed because an index or statistics with name 'IDX_TEST$TEXT' already exists on table 'TEST'.(0): create index IDX_TEST$TEXT on TEST(TEXT)
b = false
session.throwonerror = false
D
E
F
SQL* insert into TEST(ID, TEXT) values (0, 'A')
ERROR [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert explicit value for identity column in table 'TEST' when IDENTITY_INSERT is set to OFF.(0): insert into TEST(ID, TEXT) values (0, 'A')
b = false
session.throwonerror = true
ERROR
H
select ID, TEXT from TEST
b = true
session.throwonerror = true
tst.ID = 1
tst.ID = 2
tst.ID = 3
tst.ID = 4
tst.ID = 5
tst.ID = 6
tst.ID = 7
tst.ID = 8
tst.ID = 9
tst.ID = 10
I did this using the SQL_MSSQL application that comes as reference with upp. As far as I understand, there is no trace of the problem. The only difference I notice, is that in my application I do not have the following lines:
SqlSchema sch(MSSQL);
StdStatementExecutor se(SQL.GetSession());
All_Tables(sch);
ODBCPerformScript(sch.Upgrade(), se);
ODBCPerformScript(sch.Attributes(), se);
It may be the origin of the problem?
Regards,
gio
|
|
|
|
Re: MSSQL error management [message #49645 is a reply to message #49642] |
Wed, 21 March 2018 13:14   |
Giorgio
Messages: 218 Registered: August 2015
|
Experienced Member |
|
|
Hi Mirek,
this is the log:
InsertBody
A
TRY1
insert into X_DORIG(Id, Row, Description) values (180107, 1, 'Art. number')
ERROR [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_X_DORIG'. Cannot insert duplicate key in object 'dbo.X_DORIG'. The duplicate key value is (180107, 1).(0): insert into X_DORIG(Id, Row, Description) values (180107, 1, 'Art. number')
b = false
session.throwonerror = true
CATCH1
Regards,
gio
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Apr 26 14:48:52 CEST 2025
Total time taken to generate the page: 0.01056 seconds
|
|
|