Jaime Stuardo | 23 Feb 2012 04:05

Calling a store procedure

Hello… I am trying to call a SQL Server 2005 stored procedure by mean of a C++ application running under RedHat Linux. unixODBC is installed correctly since I can connect to the database.

 

When I run SQLExecDirect, the returned value is an error. So, here are the questions:

 

·         Why does an error occur?

·         Why does the SQLGetDiagRec function cannot return the actual error? If I generate an error passing a string instead of a date in the second parameter, the error is: [FreeTDS][SQL Server]Error converting data type varchar to datetime. That means ExtractError function I implemented is correct.

 

This is the code with SQLExecDirect:

 

    ret = SQLExecDirect(stmt, (SQLCHAR *) query.c_str(), SQL_NTS);

    if (!SQL_SUCCEEDED(ret)) {

        StoreSocket::ExtractError("SQLExecDirect", stmt, SQL_HANDLE_STMT);

        return false;

    }

 

Where query is:

execute usp_contrato_actualiza_aclaracion 'uno', '2012-02-22', 'dos', 'tres', 'cuatro', '2012-02-14', 'cinco', 'seis', 'siete', 'ocho', 'nueve', 'diez', 'once'

 

And the body of ExtractError is:

 

**************************

void StoreSocket::ExtractError(

        char *fn,

        SQLHANDLE handle,

        SQLSMALLINT type) {

    SQLINTEGER i = 0;

    SQLINTEGER native;

    SQLCHAR state[ 7 ];

    SQLCHAR text[256];

    SQLSMALLINT len;

    SQLRETURN ret;

 

    do {

        ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,

                sizeof (text), &len);

        if (SQL_SUCCEEDED(ret)) {

            printf("%s:%ld:%ld:%s\n", state, i, native, text);

        }

    } while (ret == SQL_SUCCESS);

}

**************************

 

Any help will be greatly appreciated,

 

Jaime Stuardo - Gerente General

DESYTEC - Development, Systems & Technologies

 


   La Concepción 81 Of. 608, Providencia, Santiago

   + 56 2 264 9399                    + 56 2 264 1558

   http://www.desytec.com

 

 

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev
Nick Gorham | 23 Feb 2012 09:29
Favicon

Re: Calling a store procedure

On 23/02/2012 03:05, Jaime Stuardo wrote:

Hello… I am trying to call a SQL Server 2005 stored procedure by mean of a C++ application running under RedHat Linux. unixODBC is installed correctly since I can connect to the database.

 

When I run SQLExecDirect, the returned value is an error. So, here are the questions:

 

·         Why does an error occur?

·         Why does the SQLGetDiagRec function cannot return the actual error? If I generate an error passing a string instead of a date in the second parameter, the error is: [FreeTDS][SQL Server]Error converting data type varchar to datetime. That means ExtractError function I implemented is correct.

 

This is the code with SQLExecDirect:

 

    ret = SQLExecDirect(stmt, (SQLCHAR *) query.c_str(), SQL_NTS);

    if (!SQL_SUCCEEDED(ret)) {

        StoreSocket::ExtractError("SQLExecDirect", stmt, SQL_HANDLE_STMT);

        return false;

    }

 

Where query is:

execute usp_contrato_actualiza_aclaracion 'uno', '2012-02-22', 'dos', 'tres', 'cuatro', '2012-02-14', 'cinco', 'seis', 'siete', 'ocho', 'nueve', 'diez', 'once'

 

And the body of ExtractError is:


HI,

Not sure I understand the problem, maybe if you could create a full test program with definition of the procedure and the expected and actual error it may help. I would guess its going to eventually be a question for the driver writers, but not sure at the moment. If you had a test program I could run I could try with another driver and see what the problem is.

--
Nick
_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev
Martin J. Evans | 23 Feb 2012 10:12
Gravatar

Re: Calling a store procedure

On 23/02/2012 03:05, Jaime Stuardo wrote:
> Hello… I am trying to call a SQL Server 2005 stored procedure by mean of
> a C++ application running under RedHat Linux. unixODBC is installed
> correctly since I can connect to the database.
>
> When I run SQLExecDirect, the returned value is an error. So, here are
> the questions:
>
> ·Why does an error occur?
>
> ·Why does the SQLGetDiagRec function cannot return the actual error? If
> I generate an error passing a string instead of a date in the second
> parameter, the error is: [FreeTDS][SQL Server]Error converting data type
> varchar to datetime. That means ExtractError function I implemented is
> correct.
>
> This is the code with SQLExecDirect:
>
>      ret = SQLExecDirect(stmt, (SQLCHAR *) query.c_str(), SQL_NTS);
>
>      if (!SQL_SUCCEEDED(ret)) {
>
>          StoreSocket::ExtractError("SQLExecDirect", stmt, SQL_HANDLE_STMT);
>
>          return false;
>
>      }
>
> Where query is:
>
> execute usp_contrato_actualiza_aclaracion 'uno', '2012-02-22', 'dos',
> 'tres', 'cuatro', '2012-02-14', 'cinco', 'seis', 'siete', 'ocho',
> 'nueve', 'diez', 'once'
>
> And the body of ExtractError is:
>
> **************************
>
> void StoreSocket::ExtractError(
>
>          char *fn,
>
>          SQLHANDLE handle,
>
>          SQLSMALLINT type) {
>
>      SQLINTEGER i = 0;
>
>      SQLINTEGER native;
>
>      SQLCHAR state[ 7 ];
>
>      SQLCHAR text[256];
>
>      SQLSMALLINT len;
>
>      SQLRETURN ret;
>
>      do {
>
>          ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
>
>                  sizeof (text), &len);
>
>          if (SQL_SUCCEEDED(ret)) {
>
>              printf("%s:%ld:%ld:%s\n", state, i, native, text);
>
>          }
>
>      } while (ret == SQL_SUCCESS);
>
> }
>
> **************************
>
> Any help will be greatly appreciated,
>

In my experience freeTDS sometimes returns an error and then there are 
no diagnostics. I added a special case to Perl's DBD::ODBC because of it 
which raises an error when an ODBC call fails and there are no diagnostics.

Martin

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

Jaime Stuardo | 23 Feb 2012 13:14

Re: Calling a store procedure

Hello and thanks for answering...

SQLExecDirect returns 100. By looking at the header files, that code means
SQL_NO_DATA according to this:

#if (ODBCVER >= 0x0300)
#define SQL_NO_DATA              100
#endif

Since I am using ODBC version 3, I am getting this code which is not an
error actually, but a result condition. The stored procedure does only an
UPDATE and currently it is not updating nothing.

I think that "#define SQL_SUCCEEDED(rc) (((rc)&(~1))==0)" macro should be
updated to include SQL_NO_DATA as a succeeded condition. That is why the
diagnostics does not return any message.

By the way, I used tsql and isql command with the stored procedure and it
worked, so I discarded problem with FreeTDS.

What do you think?

Thanks
Jaime

-----Original Message-----
From: unixodbc-dev-bounces <at> mailman.unixodbc.org
[mailto:unixodbc-dev-bounces <at> mailman.unixodbc.org] On Behalf Of Martin J.
Evans
Sent: jueves, 23 de febrero de 2012 6:13
To: Development issues and topics for unixODBC
Subject: Re: [unixODBC-dev] Calling a store procedure

On 23/02/2012 03:05, Jaime Stuardo wrote:
> Hello. I am trying to call a SQL Server 2005 stored procedure by mean of
> a C++ application running under RedHat Linux. unixODBC is installed
> correctly since I can connect to the database.
>
> When I run SQLExecDirect, the returned value is an error. So, here are
> the questions:
>
> .Why does an error occur?
>
> .Why does the SQLGetDiagRec function cannot return the actual error? If
> I generate an error passing a string instead of a date in the second
> parameter, the error is: [FreeTDS][SQL Server]Error converting data type
> varchar to datetime. That means ExtractError function I implemented is
> correct.
>
> This is the code with SQLExecDirect:
>
>      ret = SQLExecDirect(stmt, (SQLCHAR *) query.c_str(), SQL_NTS);
>
>      if (!SQL_SUCCEEDED(ret)) {
>
>          StoreSocket::ExtractError("SQLExecDirect", stmt,
SQL_HANDLE_STMT);
>
>          return false;
>
>      }
>
> Where query is:
>
> execute usp_contrato_actualiza_aclaracion 'uno', '2012-02-22', 'dos',
> 'tres', 'cuatro', '2012-02-14', 'cinco', 'seis', 'siete', 'ocho',
> 'nueve', 'diez', 'once'
>
> And the body of ExtractError is:
>
> **************************
>
> void StoreSocket::ExtractError(
>
>          char *fn,
>
>          SQLHANDLE handle,
>
>          SQLSMALLINT type) {
>
>      SQLINTEGER i = 0;
>
>      SQLINTEGER native;
>
>      SQLCHAR state[ 7 ];
>
>      SQLCHAR text[256];
>
>      SQLSMALLINT len;
>
>      SQLRETURN ret;
>
>      do {
>
>          ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
>
>                  sizeof (text), &len);
>
>          if (SQL_SUCCEEDED(ret)) {
>
>              printf("%s:%ld:%ld:%s\n", state, i, native, text);
>
>          }
>
>      } while (ret == SQL_SUCCESS);
>
> }
>
> **************************
>
> Any help will be greatly appreciated,
>

In my experience freeTDS sometimes returns an error and then there are 
no diagnostics. I added a special case to Perl's DBD::ODBC because of it 
which raises an error when an ODBC call fails and there are no diagnostics.

Martin

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

Nick Gorham | 23 Feb 2012 13:52
Favicon

Re: Calling a store procedure

On 23/02/12 12:14, Jaime Stuardo wrote:
> Hello and thanks for answering...
>
> SQLExecDirect returns 100. By looking at the header files, that code means
> SQL_NO_DATA according to this:
>
> #if (ODBCVER>= 0x0300)
> #define SQL_NO_DATA              100
> #endif
>
> Since I am using ODBC version 3, I am getting this code which is not an
> error actually, but a result condition. The stored procedure does only an
> UPDATE and currently it is not updating nothing.
>
> I think that "#define SQL_SUCCEEDED(rc) (((rc)&(~1))==0)" macro should be
> updated to include SQL_NO_DATA as a succeeded condition. That is why the
> diagnostics does not return any message.

I see what you are saying, but if that change was made in unixODBC it 
would diverge from the Microsoft implementation, compatibility with that 
is one of unixODBC goals.

I can also see situations where SQL_NO_DATA could be considered a error 
condition, depending on the use in question.
> By the way, I used tsql and isql command with the stored procedure and it
> worked, so I discarded problem with FreeTDS.
>
> What do you think?

Not sure what you mean?

I would think that changing your code to be

      if (!SQL_SUCCEEDED(ret)&&  ret != SQL__NO_DATA ) {

Would give you want you want and work across platforms as well.

--

-- 
Nick

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

Jaime Stuardo | 23 Feb 2012 15:54

Re: Calling a store procedure

Hello...

That was actually what I did :-)

Thanks
Jaime

-----Original Message-----
From: unixodbc-dev-bounces <at> mailman.unixodbc.org
[mailto:unixodbc-dev-bounces <at> mailman.unixodbc.org] On Behalf Of Nick Gorham
Sent: 23 de febrero de 2012 9:53
To: Development issues and topics for unixODBC
Subject: Re: [unixODBC-dev] Calling a store procedure

I would think that changing your code to be

      if (!SQL_SUCCEEDED(ret)&&  ret != SQL__NO_DATA ) {

Would give you want you want and work across platforms as well.

--

-- 
Nick

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

Jaime Stuardo | 23 Feb 2012 17:24

how to know which odbc.ini and odbcinst.ini are being used?


Hello..

I have a simple question..... I have 3 odbc.ini and odbcinst.ini files in my
RHEL5 system. How can I know which ones are used by unixODBC?

Thanks
Jaime

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

Nick Gorham | 23 Feb 2012 18:44
Favicon

Re: how to know which odbc.ini and odbcinst.ini are being used?

On 23/02/12 16:24, Jaime Stuardo wrote:
> Hello..
>
>
> I have a simple question..... I have 3 odbc.ini and odbcinst.ini files in my
> RHEL5 system. How can I know which ones are used by unixODBC?
>
> Thanks
> Jaime

New way

odbc_config --odbcini

Older way

odbcinst -j

Debug way, use strace and isql and see what files are being opened.

Remember you can override usign ODBCINI and ODBCSYSINI env variables.

--

-- 
Nick

_______________________________________________
unixODBC-dev mailing list
unixODBC-dev <at> mailman.unixodbc.org
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev


Gmane