5

i am trying to use parameterized queries with ADO. Executing the Command object throws the error:

Must declare the variable '@filename'

i declare the parameter @filename using CreateParameter/Append:

sql := 'INSERT INTO Sqm(Filename, data) VALUES(@filename, @data)';

command := CoCommand.Create;
command.Set_ActiveConnection(Connection.ConnectionObject);
command.Set_CommandText(sql);
command.Set_CommandType(adCmdText);
command.Parameters.Append(Command.CreateParameter('@filename', adLongVarWChar, adParamInput, -1, Filename));
command.Parameters.Append(Command.CreateParameter('@data', adLongVarWChar, adParamInput, -1, xml);

command.Execute({out}recordsAffected, EmptyParam, adCmdText or adExecuteNoRecords);

What am i doing wrong?

4
  • Try with : instead of @ before the params in the query. And remove the @ from where you add the parameters. Commented May 23, 2012 at 17:51
  • 1
    @MikaelEriksson: Line 1: Incorrect syntax near ':' You'd be surprised, i certainly was, that nobody has ever posted an example of ADO parameterized queries. (plenty to be found with ADO.NET) Commented May 23, 2012 at 17:57
  • Looks like you are using the ado interfaces directly instead of tadocommand. Try with a ? in the query where you put the variable name values (?, ?). I'm a couple of hours away from a computer so I can't verify that it will work yet. Commented May 23, 2012 at 18:15
  • @MikaelEriksson You're right. ADO doesn't support named parameters; that was my (third) problem. Commented May 23, 2012 at 18:27

2 Answers 2

14

As far i know ADO doesn't supports named parameters in SQL sentences (SELECT, INSERT, UPDATE), so you must use the ? char to indicate the parameter

sql := 'INSERT INTO Sqm(Filename, data) VALUES(?, ?)';

and then assign the parameters values in the same order as are used in the sql sentence.

ADO 2.6 Introduces the NamedParameters property, but it seems which only works with stored procedures.

Sign up to request clarification or add additional context in comments.

1 Comment

Well that certainly explains why named parameters aren't working in ADO. oi vay Microsoft
2

try this

uses ADODB, DB;
...
...
... and then in some event handler (e.g. button click),
var 
  aCommand :TADOCommand;
begin
  aCommand := TADOCommand.create(self);
  aCommand.ConnectionString := 'build the connection string or use TADOConnection and assign to Connection property instead of ConnectionString property';
  aCommand.commandText := 'INSERT INTO Sqm(Filename, data) VALUES(:filename, :data);';
  aCommand.parameters.paramByName('filename').value := 'test';
  aCommand.parameters.paramByName('data').value := 'some data';
  aCommand.execute;
  aCommand.free;
end;

I have been using parameter by names this way for TADOCommand and TADOQuery with no problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.