Solved

Changing data type between Provider and Clientdataset

Posted on 2013-01-31
6
734 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
QRReport  TQrmemo vertical stretching 1 45
Performance of SQL statement 37 111
Delphi Yen format 3 33
how to resize animated Gif image in delphi ? 1 30
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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