Solved

Changing data type between Provider and Clientdataset

Posted on 2013-01-31
6
729 Views
Last Modified: 2013-02-01
I have a stored procedure (sp_who2) that is returning a varchar containing a number (CPUTime).  I am pulling the dataset into a ClientDataSet for use in my program, but would like the column to be an integer so that indexing on it will be in the correct order.  

How can I best do that?
0
Comment
Question by:GNiessen
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 38841414
you mean sorting, not indexing ?
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 38841587
You can do this on server side by casting filed:

select cast(CPUTime as int) as CPUTime, Field2, Field3 from table1....

Open in new window

0
 

Author Comment

by:GNiessen
ID: 38841714
#Geert  The ClientDataSet has Indexes for sorting.

#sinesav  You can't select from a stored procedure.  If it were a view or a table it would be easy.  And as this is a system stored procedure I can't modify it to run the way I want.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 38842724
you could  (or ask to) add a sql zone to your Q with request attention

have you tried with a wrapper procedure ?
create a procedure which puts the output of the system proc in a temp table,
then run your select from that temp table

it's been over a decade since i did mssql but here goes ...
http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/61dfc83d-65a7-4df0-9410-400e70b77f9c/

if you put a select statement last in your proc it will behave as a query in delphi
0
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 250 total points
ID: 38842955
I missed "stored procedure". You cannot change field type on the fly because it allocate memory as string. But...

1. add all field defs + one additional 'ID' field defined as Integer and must be InternalCalc of kind.

http://edn.embarcadero.com/article/28959

2. Add procedure SortCustomClientDataSet from page: http://edn.embarcadero.com/article/29056 ... and add following code with new OnCalcField event on dataset (where you convert string to integer on the fly):

procedure TForm1.Button1Click(Sender: TObject);
begin
  ClientDataSet1.Close;
  ClientDataSet1.Open;
  SortCustomClientDataSet(ClientDataSet1, 'ID');
end;

procedure TForm1.ClientDataSet1CalcFields(DataSet: TDataSet);
begin
  ClientDataSet1.FieldByName('ID').AsInteger :=
    StrToIntDef(ClientDataSet1.FieldByName('CPUTime').AsString, 0);
end;

Open in new window

0
 

Author Closing Comment

by:GNiessen
ID: 38843325
Setting up the an internal calc field worked and I was able to index on it.  I had hoped there was a way to control more the data transformation between the stored procedure and the Clientdataset, but it is working good enough for this purpose.

Thanks all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now