Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Formula field in a SQL Expression Post Reply Post New Topic
Author Message
vipulbhatia29
Newbie
Newbie


Joined: 08 May 2009
Location: India
Online Status: Offline
Posts: 10
Quote vipulbhatia29 Replybullet Topic: Formula field in a SQL Expression
    Posted: 12 May 2009 at 6:04am
Can we use a formula field in the where clause of the SQL EXpression in the SQL Expression Editor of the crystal Reports XIR2.
 
For eg can I write such query like Select location from xxxloc where location_id = @location_idformula
 
where @location_idformula is a formula which return a single numeric value.
 
Currently when I go and write a SQL Expression in the editor , I can only see table and fields but cannot see any formulas which I have created. This is a nuisance as I am not able to use the value which is extracted from elsewhere on the basis of some conditions.
 
Is there any way to use formulas in SQL EXPRESSIONS
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 12 May 2009 at 6:30am
no.  formulas act on the data returned from the database.  SQL Expression fetch the data from the database.  You want to use parameters in the SQL expression.  You can get a list of values in a couple ways if you want to run off of data. 
 
Personally, I like to use stored procedures, way more flexible and robust than the simple selects that Crystal allows.
IP IP Logged
vipulbhatia29
Newbie
Newbie


Joined: 08 May 2009
Location: India
Online Status: Offline
Posts: 10
Quote vipulbhatia29 Replybullet Posted: 25 May 2009 at 3:05am
hi here i am again asking help on this stored proc issue.
 
I have developed a stored proc and in this there is an input 'IN' in the form loc_id from the location table and it returns the name as the output 'OUT'. Now the stored proc compiles fine on the sql editor and even it executes properly. But when I add the stored proc in the Crystal from Database Expert, a screen comes out prompting Enter Values for Loc_id.
 
I don't know the exact functionality or process how the stored proc is executed. According to my thought process I had thought that the stored proc's loc_id would be linked to the location table's loc_id and when the report will be executed the stored proc's loc_id would receive loc_id from the location table and then it would return the name on each page(my report is only of one page so it dispalys one page per loc_id) but now i suspect something else in the functionality.
 
Please guide me on the same topic and also suggest how to use the stored proc for the same purpose.
 
Given below is the stored proc that I have used
 
CREATE OR REPLACE PROCEDURE sps_name (
p_loc_id IN NUMBER,
p_name OUT VARCHAR2,
p_err_code_out OUT NUMBER,
p_err_mesg_out OUT VARCHAR2
)
IS
lv_sql VARCHAR2 (2000);
BEGIN
lv_sql :=
'((select name from (
select loc_id,name,row_number()over( order by r) rn from (
SELECT 0, loc_id, Misc1_txt NAME,''A'' STATUS ,rownum r
FROM location
WHERE loc_id ='
|| p_loc_id
|| '
union
SELECT parent_loc_id, loc_id, (SELECT a.Misc1_txt
FROM location a
WHERE a.loc_id =b.loc_id) NAME,''B'' ,ROWNUM
FROM locationpath b
START WITH b.loc_id = '
|| p_loc_id
|| '
CONNECT BY PRIOR parent_loc_id=loc_id
) where name is NOT NULL order by STATUS, R
) where rn = 1))';
EXECUTE IMMEDIATE lv_sql INTO p_name;
EXCEPTION
WHEN OTHERS
THEN
p_err_code_out := SQLCODE;
p_err_mesg_out :=
'Error in sps_provider_number ==> ' || SQLERRM;
raise_application_error (-20001, p_err_mesg_out, TRUE);
END sps_provider_number;
      
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 26 May 2009 at 6:17am
Since the stored proc is asking for a parameter, Crystal is prompting for the parameter, it will then execute the stored proc and display the tables and columns that the stored proc returns.
 
It sounds like you don't want the stored proc to take a parameter, you just want it to retrieve for all locations, if so, remove the IN parameter of your stored proc, then Crystal won't ask for it.
 
On the other hand, if you only want the information from 1 location, then your stored proc and Crystal are both correct.
 
Regardless, Crystal is trying to determine the structure of the data, so that it can display it for you to use in the report.  It doesn't 'know' what the data looks like from stored procs name or a table.  What it does is connect to the data and looks at what is returned, be it stored proc, data table or XML file.
IP IP Logged
vipulbhatia29
Newbie
Newbie


Joined: 08 May 2009
Location: India
Online Status: Offline
Posts: 10
Quote vipulbhatia29 Replybullet Posted: 26 May 2009 at 7:14am
Hi many many thanks to you but today i was able to complete the report not with the help of a stored proc but with the help of a view and a function.
 
i created this function with some help from google and then a view and added it to the crystal reports and finally i got the data i required on my crystal report.
The above stored proc needed some tuning to make it generic and not specific for just one loc_id and I tried doing the same but it  did not work then i thought why not functions you input one you get an output for one and then i wrote down a select query calling the function with input value as the loc_id column of the table and that worked perfectly and my view was created
 
But thanks a lot and i'd definitely try working the same thing with a stored proc.
 
Thanks a lot for the guidance. Big%20smile
 
 
 
IP IP Logged
aussie_1968
Newbie
Newbie


Joined: 14 May 2008
Online Status: Offline
Posts: 7
Quote aussie_1968 Replybullet Posted: 27 May 2009 at 10:49pm
...created new post...


Edited by aussie_1968 - 27 May 2009 at 11:10pm
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.016 seconds.