SQL 작성 시 아래와 같이 파라메터를 많이 이용합니다.
SELECT * FROM EMPLOYEE WHERE EMP_ID = :EMP_ID
하지만, SQL을 로그로 기록할 경우 파라메터 명(:EMP_ID) 대신 파라메터 값이 들어가야 할 경우 아래와 같이 파라메터 값이 적용된 SQL을 만들어 사용할 수 있습니다.
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
SQLStr, ParamName, ParamValue: string;
begin
Query1.Close;
Query1.ParamByName('Name').AsString := '홍길동';
Query1.ParamByName('JUNHYEONG').AsString := '2';
Query1.ParamByName('yundo').AsString := '2016';
Query1.Open;
SQLStr := Query1.SQL.Text;
for I := 0 to Query1.ParamCount - 1 do
begin
ParamName := ':' + Query1.Params[I].Name;
ParamValue := Query1.Params[I].AsString;
if Query1.Params[I].DataType in [ftString, ftWideString, ftMemo, ftWideMemo] then
ParamValue := '''' + ParamValue + '''';
SQLStr := SQLStr.Replace(ParamName, ParamValue);
end;
Memo1.Lines.Text := SQLStr;
end;위와 같이 처리하면 메모에 아래와 같이 파라메터 명에 파라메터 값이 치환된 쿼리가 표시됩니다.
select *
from IPSI_MAST
where MAST_SUHEOM_BUNHO like '' || '%'
and MAST_NAME like '홍길동' || '%'
and MAST_JIMANG = '0'
AND MAST_GUBUN = '2'
and MAST_JUNHYEONG like '2' || '%'
and MAST_YUNDO ='2016'
order by MAST_SUHEOM_BUNHO