代码如下:
只须调用BuildStoredProcParams(StoredProc1)即可设好参数.
const StoredProcParamType:array [0..5] of TParamType =(ptUnknown,ptInput,ptInputOutput,ptOutput,ptInputOutput,ptResult);
function GetParamsFieldType(TypeStr:string):TFieldType;
begin
if TypeStr='int' then
Result:=ftInteger
else if (TypeStr='char') or (TypeStr='varchar') or (TypeStr='sysname') then
Result:=ftString
else if TypeStr='datetime' then
Result:=ftDateTime
else if (TypeStr='image') or (TypeStr='text') then
Result:=ftBlob
else if (TypeStr='smallint') or (TypeStr='tinyint') then
Result:=ftSmallint
else if (TypeStr='binary') then
Result:=ftBytes
else if (TypeStr='varbinary') then
Result:=ftVarBytes
else if (TypeStr='bit') then
Result:=ftBoolean
else if (TypeStr='real') or (TypeStr='float') then
Result:=ftFloat
else if (TypeStr='numeric') or (TypeStr='decimal') then
Result:=ftBCD
else if (TypeStr='smallmoney') or (TypeStr='money') then
Result:=ftCurrency
else if (TypeStr='uniqueidentifier') then
Result:=ftString
else
Result:=ftUnknown;
//endif
end;
procedure BuildStoredProcParams(StoredProc:TStoredProc);
var tmpQuery;
i:integer;
begin
StroedProc.Params.Clear;
tmpQuery:=TQuery.Create(nil);
try
tmpQuery.DatabaseName=StroedProc.DatabaseName;
tmpQuery.SQL.Text:='sp_sproc_columns '''+StroedProc1.StoredProcName+'''';
tmpQuery.Open;
if not (tmpQuery.EOF and tmpQuery.BOF) then begin
tmpQuery.First;
while not tmpQuery.EOF do begin
i:=tmpQuery.FieldByName('COLUMN_TYPE').Value;
StroedProc1.Params.CreateParam(GetParamsFieldType(tmpQuery.FieldByName('TYPE_NAME').AsString),
tmpQuery.FieldByName('COLUMN_NAME').AsString,
StoredProcParamType);
tmpQuery.Next;
end;
end;
finally
tmpQuery.Free;
end;
end;