Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: dynamic SQL Post Reply Post New Topic
Author Message
kirandb
Groupie
Groupie
Avatar

Joined: 10 Jun 2009
Location: United States
Online Status: Offline
Posts: 69
Quote kirandb Replybullet Topic: dynamic SQL
     Posted: 24 Dec 2010 at 7:49am
I have a parameter which is a string of one value or comma seperated value. This should be appended to the SQL before execution to display the report.
Ex:  if the param = 'Iowa'
My SQL will be
select id,name, population from table a where a.city = 'Iowa' and name like 'A*' order by id
 
 
if the param = 'Iowa,Texas'
My SQL will be
select id,name, population from table a where (a.city = 'Iowa' or a.city = 'Texas') and name like 'A*' order by id
 
How do i solve this issue.
I was trying to create a formula and use the formula in the command prompt to create the SQL. But I guess the SQL gets executed first before the formula gets calculated. So it failed. Any Suggestions.
 
Thanks,
Kiran
 
share your knowledge
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 28 Dec 2010 at 3:18am
if you are using a stored proc, you can create a table function that will parse the comma delimited string into a table, then you can join on the table to accomplish the same thing.
 
if you are just joining to the tables directly, you might try to see if the city name exists in the string using INSTR() during the record selection process.
 
HTH
IP IP Logged
kirandb
Groupie
Groupie
Avatar

Joined: 10 Jun 2009
Location: United States
Online Status: Offline
Posts: 69
Quote kirandb Replybullet Posted: 28 Dec 2010 at 3:24am
I can use the stored proc but i am trying to avoid writing one since it involves not much coding. So wondering if i can use a dynamic SQL in the Database expert command prompt. my input parameter will be a comma seperated value based on which my "Where Clause" changes.
Is there a way to implement a formula field in the SQL ?
 
Thanks,
Kiran
share your knowledge
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 28 Dec 2010 at 3:51am
I haven't bothered with database expert in years, as all of my reports run off of stored procs...just easier to maintain when they're all set up the same and sp are way more flexible...and most of my reports could not be created without them.  They are just not simple table listings even with joins.
 
 if you don't want to use a sp, I would put the filter in the data selection criteria.  You might be able to change the where clause to check if the city is in the string, but you cannot use IN. For SQL Server it would be CHARINDEX(city, {@parameter}) > 0
 
HTH


Edited by lockwelle - 28 Dec 2010 at 3:54am
IP IP Logged
kirandb
Groupie
Groupie
Avatar

Joined: 10 Jun 2009
Location: United States
Online Status: Offline
Posts: 69
Quote kirandb Replybullet Posted: 28 Dec 2010 at 4:01am
Is it possible to implement dynamic SQL in Crystal ?
Any suggestions.
 
Kiran
share your knowledge
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 28 Dec 2010 at 5:18am
you can try to use something that finds the city in the parameter, like charindex....you are passing in a 1 string, you need to have your sql conform to finding what you want in the string, it is not a field. 
 
No you cannot do as you outlined in the initial question, CR doesn't operate that way, and it would be terribly inefficent way to code SQL if your user entered 50 cities.
IP IP Logged
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