一个简单一问题,在DELPHI中有没有宏替换的函数?(50分)

  • 主题发起人 主题发起人 beadly
  • 开始时间 开始时间
B

beadly

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure Tifleave.BitBtn1Click(Sender: TObject);
begin
case RadioGroup1.ItemIndex of
0:WhyLeft:='L';
1:WhyLeft:='D';
2:WhyLeft:='F';
end;
LeftDate:=DateToStr(datetimepicker1.Date);
OtherThing:=Memo1.Text;
with Frm_Datain1.ADOCommand1 do begin
CommandText:='insert into salarout select *,'+LeftDate+' as levdate,'+WhyLeft+' as whylev from salaryin where no=:NoParam';
Parameters[0].Value:=Frm_Datain1.DBEdit1.Text;
Execute;
CommandText:='delete salaryin where no=:NoParam';
Parameters[0].Value:=Frm_Datain1.DBEdit1.Text;
Execute;
end;
Frm_Datain1.AdoDataset1.Requery;
Frm_Datain1.bAppend:=false;
end;
几个变量在前面都已经定义;
但这句有错CommandText:='insert into salarout select *,'+LeftDate+' as levdate,'+WhyLeft+' as whylev from salaryin where no=:NoParam';
比如当WhyLeft='L'时,报错:列名'L'无效,表结构是没错的,
我故意换成CommandText:='insert into salarout select *,'+LeftDate+' as levdate,'+'''L'' as whylev from salaryin where no=:NoParam';
就没错!为什么会这样啊!
 

哦不好意思看的太快了,呵呵,你可以这样,
const
SQL = 'select a, b ,%s from abc';

然后
LeftDate := 'L';
str := format(SQL, [L]);
xxx.SQL.Text := str;
xxx.Open;
 
多谢kKYY,呵呵,麻烦帮我看一下报错原因!
 
'insert into salarout select *,'+LeftDate+' as levdate,'+WhyLeft+' as whylev from salaryin where no=:NoParam'
生成的是:
'insert into salarout select *,02/11/19 as levdate,L as whylev from salaryin where no=:NoParam'
知道错在哪了吗? (我敢打赌你的levdate也不会对)
 
谢Another_eYes:
levdate 是对的,我说过CommandText:='insert into salarout select *,'+LeftDate+' as levdate,'+'''L'' as whylev from salaryin where no=:NoParam';
就没错!为什么会这样啊!
 
在Form上再放一个memo,把你生成后的commandtext作为memo.text输出,看看出现的是什么,
然后再查错!
另外,Another_eYes说的没错,可能是你的系统日期格式有所不同,所以能在另一种情况下骗过
SQL,让它以为是对的!

 
''L''
你这里的 'L' 是个常量,当然不会错!!!
 
到底是怎么回事?字段都是字符型的呀!
 
to beadly:
你想要的结果应当是:
insert into salarout select *,'02/11/19' as levdate,'L' as whylev from salaryin where no=:NoParam 吧
而你现在得到的是:
insert into salarout select *,02/11/19 as levdate,L as whylev from salaryin where no=:NoParam
所以你的语句应当写成:
CommandText:='insert into salarout select *,'''+LeftDate+''' as levdate,'''+WhyLeft+''' as whylev from salaryin where no=:NoParam';

to dlnew:
不是他的系统日期格式不同而“骗过”SQL, 根本没骗, 只是SQL认为02/11/19是个数学表达式: 2除以11再除以19
所以最后获得的levdate字段是个浮点数字段而不会是日期字段(所以我说levdate肯定不对)
 
太多谢各位了........
 
后退
顶部