Error 262145 while trying to insert in SQL db with TwinCat 2

389 views Asked by At

I am trying to insert data into a SQL Server database from TwinCat 2 program. When the trigger is activated, I am getting the following error: 262145

Error codes

racer_name : STRING(256);
racer_name := 'CS1';

FB_FormatStringC1_lap_db(
    sFormat:='INSERT INTO demo VALUES (%S, %D, %D)',
    arg1:= F_STRING(racer_name),
    arg2:= F_DWORD(C1_Lap_Minutes),
    arg3:= F_DWORD(C1_Lap_Seconds),
    bError=> ,
    nErrId=> ,
    sOut=>sInsertStringC1_lap_db );


FB_DBRecordInsertC1_lap_db(
    sNetID:= ,
    hDBID:=1 ,
    sInsertCmd:=sInsertStringC1_lap_db ,
    bExecute:=  dummy,
    tTimeout:=T#30s ,
    bBusy=> bBusyInsert,
    bError=> bErrInsert,
    nErrID=> nErridInsert,
    sSQLState=> );

Db design

The communication to the server is running, because i tried to import only integers and it's working without any problem.

Maybe I am not formatting the string variables properly?

Thanks in advance.

1

There are 1 answers

0
GMB On BEST ANSWER

This is your query:

INSERT INTO demo VALUES (%S, %D, %D)

You are giving 3 columns for insert while your table has 4. Assuming that id is autogenerated, you can explictly not provide it by enumerating the target columns:

INSERT INTO demo (racer, minuteslap, secondslap) VALUES (%S, %D, %D)

This is one of the reasons why always enumerating the target columns in a insert statement is a best practice.