===================引 用 文 章===================
請將 SQL 語法 post 出來才知道是不是有問題
==============================================
程式裡改DBType來連不同的資料庫
DBtype=0 是postgresql
DBType=1 是access
case DBType of
0:SQL_GET_INT:='%';
1:SQL_GET_INT:='int';
end;
function fun_SQL_Year(FieldName, Y: string): string;
begin
case DBType of
0: Result := ' to_char(' FieldName ',''' 'yyyy' ''')=''' Y ''' ';
1: Result := ' ((year(' FieldName '))=' Y ') ';
end;
end;
function fun_SQL_Month(FieldName, M: string): string;
begin
case DBType of
0: Result := ' to_char(' FieldName ',''' 'mm' ''')=''' M ''' ';
1: Result := ' ((month(' FieldName '))=' M ') ';
end;
end;
function fun_SQL_M(FieldName, D: string): string;
begin
// D -- 1 或 -1 --是否要轉為負數
case decimalsM of {decimalsM 是小數位數}
0: result := ' ' SQL_GET_INT '((' FieldName ' 0.5)*1)*(' D ') ';
1: result := ' ' SQL_GET_INT '((' FieldName ' 0.05)*10)*(' D ')/10 ';
2: result := ' ' SQL_GET_INT '((' FieldName ' 0.005)*100)*(' D ')/100 ';
3: result := ' ' SQL_GET_INT '((' FieldName ' 0.0005)*1000)*(' D ')/1000 ';
end;
end;
with Q1,sql do begin
add(' select classno,cusno,sa,sum(p1) as p2 from ');
add('(');
add(' select c.classno,a.cusno, ');
add(fun_SQL_M('b.prqty*b.price','1') 'as p1,');
add(' 1 as sa from sale a,sales1 b,prod c,class d ');
add(' where b.prdno=c.prdno ');
add(' and a.salno=b.salno ');
add(' and c.classno=d.classno ');
if CLASS_S<>'' then
add(' and c.classno=:classno ');
add(' and a.cusno between :cusno1 and :cusno2 ');
add('and ' fun_SQL_Month('a.sdate',daM) ' and ' fun_SQL_Year('a.sdate',daY));
add(' union all ');
add(' select c.classno,a.cusno,');
add(fun_SQL_M('b.prqty*b.price','-1') 'as p1,');
add(' 2 as sb from sback a,sbacks1 b,prod c,class d ');
add(' where b.prdno=c.prdno ');
add(' and a.sbkno=b.sbkno ');
add(' and c.classno=d.classno ');
if CLASS_S<>'' then begin
add(' and c.classno=:classno ');
ParamByName('classno').AsString:=CLASS_S;
end;
add(' and a.cusno between :cusno1 and :cusno2 ');
add('and ' fun_SQL_Month('a.sbdate',daM) ' and ' fun_SQL_Year('a.sbdate',daY));
add(' ) as t ');
add(' group by cusno,classno,sa ');
ParamByName('cusno1').AsString:=CUST_Start;
ParamByName('cusno2').AsString:=CUST_End;
open;
end;
在條件視窗選擇年月 '2006/07'
daM ='07'
daY = '2006'
CUST_Start, CUST_End 客戶範圍
CLASS_S 商品類別
組合出來的語法 , 分別拿到access 跟 postgresql 單獨執行都沒問題..
可是連postgresql 在程式 run到open就會錯誤....
不過專案很多地方用這樣的寫法都沒問題....只有這會有錯誤