1

I am attempting a quick prototype-port of some old database code over to use the FreeTDS library. Currently, I am looking at a query similar to

SELECT x,y,z from MyTable WHERE id = @arg1

When I execute the query, I naturally get an error like Must declare the scalar variable "@arg1".

But one thing eludes me. How do I declare this variable? I have looked through the API docs and code examples over and over again and I can't seem to find how to solve this should-be-trivial task.

The code I currently use is:

if(dbcmd(proc, "SELECT x,y,z from MyTable WHERE id = @arg1") != SUCCEED) {
  return fail("Failed to dbcmd()"); 
}

if(dbsqlexec(proc) != SUCCEED) {
  return fail("Failed to dbsqlexec()");
}

while((retcode=dbresults(proc)) == SUCCEED) {
  while(dbnextrow(proc) != NO_MORE_ROWS) {
    int len = dbdatlen(proc, 1);
    char* data = (char*)dbdata(proc, 1);
    cout << string(data, len) << endl;
  }
}
4

1 回答 1

0

您可以尝试 "DECLARE @MyVariable int" 或 "@arg1", OleDbType.Integer" 添加为 dbcmd(dbproc, "HERE" ); 在 dbcmd(proc,"SELECT ....."); 之后

dbcmd(dbproc,"SELECT x,y,z from MyTable WHERE id = @arg1");
dbcmd(dbproc, "DECLARE @arg1 int" );

我不认为是否有任何 API,但我确信这是您可以使用它的方式。

于 2012-06-02T22:07:20.643 回答