Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Using parameters in an SQL Expression Post Reply Post New Topic
<< Prev Page  of 2
Author Message
ve2yu
Newbie
Newbie
Avatar

Joined: 03 Nov 2008
Location: Canada
Online Status: Offline
Posts: 4
Quote ve2yu Replybullet Posted: 03 Nov 2008 at 11:22am
Question: I have a parameter being brought into a crystal report XI from  a .net code. The parameter is MyCategory.
I have created a command with the following
 
select * from inventory where category = '{?myCategory}'
 
this works fine  but I would like to do the following so I can use the report for partial or full.
 
if '{?mycategory}' = "Complete List" then select * from inventory
else
 
select * from inventory where category = '{?myCategory}'
 
cannot seem to get it to work in the command what syntax do I have wrong?
 
Clifford Sutton
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 04 Nov 2008 at 9:53am
You can't use an IF statement in SQL.  I responded to you on this issue in another thread.  The solution is:
 
Select * from inventory
where ({?mycategory} = 'Complete List')
  or ({category} = {?mycategory}
 
-Dell
IP IP Logged
nani2020nani
Newbie
Newbie


Joined: 13 May 2009
Online Status: Offline
Posts: 14
Quote nani2020nani Replybullet Posted: 13 May 2009 at 11:45pm
 If you add a 'command' i think the SQL Expression field is vanished
 
y is it so??/
 
if v add a sql command...v cant go fr sql expression field na...waz d reason behind that??
 
plz help me out...repli as soon as possible frnds...tnx in advance
avsr
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 14 May 2009 at 5:37am
You can't do a Select in a SQL Expression, so you have to use a command.  Yes, it's one or the other - you can't have both SQL Expressions and Commands in the same report.
 
-Dell
IP IP Logged
nani2020nani
Newbie
Newbie


Joined: 13 May 2009
Online Status: Offline
Posts: 14
Quote nani2020nani Replybullet Posted: 14 May 2009 at 7:10pm

v can select using sql expression na

select emp_id from employees where empid=__
this is an sql expression returning single field rite !!
 
and can u plz tell abt ...enforce from and enforce to options ....plz repli as soon as possible !!
avsr
IP IP Logged
nani2020nani
Newbie
Newbie


Joined: 13 May 2009
Online Status: Offline
Posts: 14
Quote nani2020nani Replybullet Posted: 14 May 2009 at 8:20pm

if suppose i have 2 tables jobs and emp having 2 common fields whic are linked....fields:jobid and empid...

table linking is like...from emp to jobs
 
then using enforce from..v can select only 'to table' fields rite ...
 
but im not getting the values....
 
using enforce to
(select empname from employees where emp.jobid=jobs.jobid )
 
using enforce from
(select jobdesc from jobs where emp.jobid=jobs.jobid)
 
is this two queries rite..??...
but for me...oly if i select fields from to table i.e; jobs table den oly its displaying values...or else nothing is being displayed
 
so plz help me out....
 
 
avsr
IP IP Logged
4past12
Newbie
Newbie
Avatar

Joined: 11 Jun 2010
Online Status: Offline
Posts: 2
Quote 4past12 Replybullet Posted: 11 Jun 2010 at 6:39am
You can easily get this to work.
 
The problem is CR parameters are a lot like the C++ preprocesor's token text replacement. It just takes whatever value is inside the parameter and places it in the expression without any regard to data type (anyway, that' the best way I can explain it).
 
So, if your SQL expression is expecting a string, you must quote the CR parameter. Your SQL query should look like the following:
 
(Select "CONTRACT_REF_SEG_DESC"."CS_DESCRIPTION"
FROM "CONTRACT_REF_SEG_DESC"
WHERE LEFT("INVOICE_LINES"."IL_CUSTOMER_REF",(CHARINDEX('/',"INVOICE_LINES"."IL_CUSTOMER_REF",0)-1)) = "CONTRACT_REF_SEG_DESC"."CS_CODE"
AND
"CONTRACT_REF_SEG_DESC"."CS_ACCOUNT"="{?AccountCode}" )
 
Notice that I put quotation marks around the {?AccountCode} parameter. This should now work.
 
Regards,

Craig S.
IP IP Logged
4past12
Newbie
Newbie
Avatar

Joined: 11 Jun 2010
Online Status: Offline
Posts: 2
Quote 4past12 Replybullet Posted: 11 Jun 2010 at 6:45am
Try this:
 
IF '{?mycategory}' = "Complete List"
  BEGIN
    SELECT * FROM inventory
  END
ELSE
  BEGIN
    SELECT * FROM inventory WHERE category = '{myCategroy}'
  END
 
 
This will work as a CR Database Command object since it is all SQL. Also, if you are allowing custom values for the {?mycategory} parameter, you may also want to do the following check on it before running the query:
 
IF '{?mycategory}' != ''
  BEGIN
    -- Do some stuff.
  END
 
Note that I checked for the empty string. Parameters are always converted to a string type when replaced into the SQL command (at least, that's been my experience...but I could be wrong on that). So you should not use this code, as it will never be true:
 
IF {?mycategory} IS NULL
  BEGIN
    -- Do some stuff
  END
 
HTH.
 
Regards,

Craig S.
IP IP Logged
RumeshChanchal
Newbie
Newbie


Joined: 15 Sep 2011
Location: India
Online Status: Offline
Posts: 2
Quote RumeshChanchal Replybullet Posted: 15 Sep 2011 at 1:21am
rumeshchanchal
IP IP Logged
<< Prev Page  of 2
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.