Board index » delphi » TADOQuery parameters and MS SQL Server problem

TADOQuery parameters and MS SQL Server problem

I am trying to INSERT values into an MS SQL Server 2000 with TADOQuery
in Delphi 5. When executed i get an ODBC error.

Here is the code:

//////
 Query: TADOQuery;
  FolderID: Integer;
  a, SQL: string;
begin
  Query := TADOQuery.Create(nil);
  try
    Query.Connection := FConnection;

    Query.SQL.Add('INSERT INTO Folder (ParentID,Name,CreateUserID,');
    Query.SQL.Add('CreateDate,ChangeUserID,ChangeDate) ');
    Query.SQL.Add('VALUES(1,:NameParam,:UserIDParam1,:DateParam,:UserIDParam2,:DateParam)');
    Query.Parameters.ParamValues['NameParam'] := FolderName;
    Query.Parameters.ParamValues['UserIDParam1'] := UserID;
    Query.Parameters.ParamValues['UserIDParam2'] := UserID;

    a := DateTimeToStr(Now);
    Query.Parameters.ParamValues['DateParam'] := a;

    Query.ExecSQL; // Results in an error in MS SQL but not in mySQL

Please help
Steve Carrigan

 

Re:TADOQuery parameters and MS SQL Server problem


Hi there.

You shouldn't use named parameters with the same name (see your DateParam),
so try ti do it in this way:

.....
Query.SQL.Add('VALUES(1,:NameParam,:UserIDParam1,:DateParam1,:UserIDParam2,:
DateParam2)');
.....
a := DateTimeToStr(Now);
Query.Parameters.ParamValues['DateParam1'] := a;
Query.Parameters.ParamValues['DateParam2'] := a;

Best regards.
 Evgeny.

Quote
> I am trying to INSERT values into an MS SQL Server 2000 with TADOQuery
> in Delphi 5. When executed i get an ODBC error.

> Here is the code:

> //////
>  Query: TADOQuery;
>   FolderID: Integer;
>   a, SQL: string;
> begin
>   Query := TADOQuery.Create(nil);
>   try
>     Query.Connection := FConnection;

>     Query.SQL.Add('INSERT INTO Folder (ParentID,Name,CreateUserID,');
>     Query.SQL.Add('CreateDate,ChangeUserID,ChangeDate) ');

Query.SQL.Add('VALUES(1,:NameParam,:UserIDParam1,:DateParam,:UserIDParam2,:D
ateParam)');

- Show quoted text -

Quote
>     Query.Parameters.ParamValues['NameParam'] := FolderName;
>     Query.Parameters.ParamValues['UserIDParam1'] := UserID;
>     Query.Parameters.ParamValues['UserIDParam2'] := UserID;

>     a := DateTimeToStr(Now);
>     Query.Parameters.ParamValues['DateParam'] := a;

>     Query.ExecSQL; // Results in an error in MS SQL but not in mySQL

Other Threads