Hello all,

im writing a c++ program to interact with an SQLServer database.
my code will run a query and retrieve data fine.
for example SELECT data FROM TestTable returns all strings
from the data column.

but when i run the following statement

INSERT INTO TestTable (data) VALUES ('HELP')

i get the following error:

37000:1:170:[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect
syntax near ' '.

37000:2:8180::[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.

The allocate and prepare functions return success so im not
sure whats going on.
any help would be great.
using visual c++ with ODBC connection to SQL server 2003

// Allocate memory for the statement handle
retcode = SQLAllocHandle(SQL_HANDLE_STMT,hDBC,&hStmt);

if(retcode == SQL_SUCCESS)
     printf("\nAlloc Handle: SQL_SUCCESS");


// Prepare the SQL statement by assigning it to the statement handle
retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
if(retcode == SQL_SUCCESS)
     printf("\nPrepare: SQL_SUCCESS");



// Execute the SQL statement handle
printf("\nSending statement: %s",szSqlStr);
retcode = SQLExecute (hStmt);
if(retcode == SQL_ERROR)
{
     printf("\nStatement: SQL_ERROR\n");  
     ExtractError(hStmt, SQL_HANDLE_STMT);
}

How is szSqlStr declared and how do you initialize it?

its decleared as follows in my class header file

UCHAR   szSqlStr[128]; 

Then this is the start of the send statement.

bool CSQLHandler::SendStatement(char* statement)
{
    strcpy((char*)szSqlStr, statement); // SQL string

....
...


dbase.SendStatement("INSERT INTO TestTable (data) VALUES ('HELP')");

i dont understand why i get this error as a select statement works.
im sure my syntax is correct.
data is an nvarchar with length of 50 so this should be ok

sorry the error between the '' has two lines of ascii code 204


37000:1:170:[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect
syntax near '(twolines of ascii code 204) '.

Ive just done a test using an access 2003 database on my local machine and the code worked fine!

As im using SQLServer is there some special syntax to reference the tables or table elements.
I found a site using the following but im guessing thats just for oracle databases etc.

schemaname.tablename
schemaname.tablename.fieldname

cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.