implementationvar database:string; //定义一个存储过程用于选择数据库procedure tf_main.choisedatabase ;
begin
if trim(databank.Text)='AA' then
database:='AIS20070116193457'
else
if trim(databank.Text)='BB' then
database:='AIS20070116193641'
else
if trim(databank.Text)='CC' then
database:='AIS20060426141617'
else
database:='AIS20070123133449'
end;
...
--------------------------------------------------
choisedatabase; //执行过程,把值赋给变量database;
WITH datamodule1.ADOQuery1 do
begin
close;
sql.Clear ;
sql.Add('use :a'); //使用数据库
sql.Add('EXEC __DelViewTable') ; //执行存储过程
parameters.ParamByName('a').Value :=database; //把变量database的值赋给参数A,用于使用数据库
execsql;
end;以上代码在执行过程中出现‘@P1’附近有语法错误,请问得怎么解决,我上面的语法有问题吗?
begin
if trim(databank.Text)='AA' then
database:='AIS20070116193457'
else
if trim(databank.Text)='BB' then
database:='AIS20070116193641'
else
if trim(databank.Text)='CC' then
database:='AIS20060426141617'
else
database:='AIS20070123133449'
end;
...
--------------------------------------------------
choisedatabase; //执行过程,把值赋给变量database;
WITH datamodule1.ADOQuery1 do
begin
close;
sql.Clear ;
sql.Add('use :a'); //使用数据库
sql.Add('EXEC __DelViewTable') ; //执行存储过程
parameters.ParamByName('a').Value :=database; //把变量database的值赋给参数A,用于使用数据库
execsql;
end;以上代码在执行过程中出现‘@P1’附近有语法错误,请问得怎么解决,我上面的语法有问题吗?
var
DtatBase:string;
begin
WITH ADOCommand1 do
begin
CommandType:= cmdStoredProc;
CommandText:= '__DelViewTable';
Parameters.CreateParameter('@Parameter',ftString,pdInput,100,DtatBase);
Execute;
end;
end;
这句上。
:a 作为字符串参数,传进去的时候,变成了(假设database变量的值是“DB1”):
use 'DB1'
多了个单引号。因为是字符串
所以会错。
@P1就是指这个参数这里不要用参数,直接用字符串组合好了。
WITH datamodule1.ADOQuery1 do
begin
close;
sql.Clear ;
//sql.Add('use :a'); //使用数据库
//换成
sql.Add(Format('use [%s]', [database]));
sql.Add('EXEC __DelViewTable') ; //执行存储过程
parameters.ParamByName('a').Value :=database; //把变量database的值赋给参数A,用于使用数据库
execsql;
end;