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
Page  of 2 Next >>
Author Message
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Topic: Using parameters in an SQL Expression
    Posted: 11 Jul 2007 at 7:35am
Hi,
 
I noticed in one of the recent posts Brian said
 
You can use parameters to pass different values to the WHERE clause, but not actually change the conditions in it.
 
 
Does anyone have any examples of how to do this?
 
I've got an SQL Expression field which works fine when I hard code a value, but if i try and use a parameter i get an error that says
 
Error in compiling SQL Expression:
Failed to retrieve data from the database.
Details: ADO Error Code : 0x80040e10
Source: Microsoft OLE DB Provider for SQL Server
Description: No value given for one or more required parameters..
 
 
The code i'm using in the SQL Expression is
 
(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} )
 
Like I say if i replace the {?AccountCode} with an account name it works.
 
Is it possible to do this? If so any ideas where i'm going wrong?
 
I'm using MS SQL and CR XI.
 
Thanks.
 
 
 
 
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 11 Jul 2007 at 12:16pm

SQL Expressions are for small bits that can be put into an existing Select statement.  For example, I have a stored function that formats user names based on a key field in the database so I can put this in a SQL Expression:

FormatUserLNameFirst({EMPLOYEE.EMPLOYEE_KEY})
 
For a full select statement like you have, you need to make it a "Command Text".  To do this, go to your connection in the Database Expert and see if the option to "Add Command" is available (Crystal doesn't allow this for all database types...).  If it is, put your SQL in there and use the expert to create the parameter.
 
-Dell
IP IP Logged
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Posted: 12 Jul 2007 at 8:36am
Smile
thanks
IP IP Logged
kwsanders
Newbie
Newbie


Joined: 10 Jul 2007
Online Status: Offline
Posts: 5
Quote kwsanders Replybullet Posted: 25 Jul 2007 at 7:07pm
I read this thread today and went off and created a couple of commands in my database... all the time not thinking about the result that would give me when I went to test the report against a different database of the same schema.  It doesn't have the commands. Cry

Oh well... back to the drawing board.


Edited by kwsanders - 25 Jul 2007 at 7:07pm
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 25 Jul 2007 at 9:23pm
If you create the Command objects inside your report, then they don't need to be on the database. They are simply SQL statements that CR sends to the database. It will works with any compatible database that has the same table structures.
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
kwsanders
Newbie
Newbie


Joined: 10 Jul 2007
Online Status: Offline
Posts: 5
Quote kwsanders Replybullet Posted: 26 Jul 2007 at 7:08am
When you say to create the commands in my report, are you referring to using SQL Expressions?  If so, then I guess I am doing something wrong that I need to try and figure out.

I am using Crystal 10 Advanced Full (10.0.0.533).  I need to create a report that is based on a single table.  The report has three columns.  Information for the current month, the previous month, and a summarization of year-to-date.

The first two columns are easy.  The third column is where I am getting tripped up and it is probably to do with my record selection formula.  I am not getting proper numbers for the roll-up on the year-to-date values.

One thing that I forgot to mention is that I need to determine a value based on a particular parameter.  I didn't think you can use parameters in SQL Expressions.


Edited by kwsanders - 26 Jul 2007 at 7:19am
IP IP Logged
GreyDog
Newbie
Newbie
Avatar

Joined: 13 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 11
Quote GreyDog Replybullet Posted: 26 Jul 2007 at 7:34am
Hi,
 
SQL commands are set up in the Database Expert.
Click on the plus next to the Database name in your connection, it's the first option under it, if it's available for your database.
If you drag 'Add Command' to the 'Selected Tables' pane on the right it opens a window for your SQL. Just make sure the SQL is enclosed in brackets or else it will complain.
 
SQL Expressions are different. I think they are for minor SQL things, like SQL functions. Not entirely sure as i'm new to adding SQL into a report myself. If you add a 'command' i think the SQL Expression option is removed.
 
As for the last column in your report, how is the table set out?
I've had to do a similar thing in reports, I had a separate field for each month and used a Crystal formula field to add them up after doing a 'select case' on the current month.
 
Sorry if that's made it more confusing, I normally ask questions rather than answer them as you can see why!Embarrassed
 
 
IP IP Logged
kwsanders
Newbie
Newbie


Joined: 10 Jul 2007
Online Status: Offline
Posts: 5
Quote kwsanders Replybullet Posted: 26 Jul 2007 at 7:36pm
I have to retype this based on memory... just lost everything I had typed.  Cry

Anyway... I thought that SQL commands on the Database Expert screen are on the database and do not migrate with the report.  Is that not right?  At least it is not what I am seeing here.  I have to be able to dynamically change the datasource on the report at runtime.

As for my final column, it is a summarized view of the year-to-date data.   I need to be able to pull in a list of records based on a range of numbers.  From that dataset, I needed to calculate some summary totals across several values.

The big problem that I am having trouble understanding with Crystal Reports is how the records get loaded and calculated when the report is executed.  I had setup a formula to pick off a date value from a record if its ID field had the same value as the first value in my parameter range.  When the report runs, however, I see an empty field.  If I browse the data on that formula field, I see two values... a blank line and the second is the date that I expected.

I had setup the formula as follows:

dateTimeVar myDate;
IF {MYTABLE.ID} = {?TEST_ID} THEN
   myDate := {MYTABLE.SOME_DATE};

Any help someone could give me would be great.  I ordered a book from Buy.com recently for Crystal 10, but they sent me the book for Crystal 9.  I got an RMA, but I am waiting for them to send the correct book.

I need to go out and purchase Brian's book.  I had seen it a while back, but I did not pick it up at that time.
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 26 Jul 2007 at 11:01pm
The data source connection is separate from the Command object. You can create a command object and change the data source dynamically. As long as the tables structures are the same in both databases, your report will run fine.

Re your second question, I'm not sure how to answer because my brain is fried right now. Sorry. If you see a blank line when you browse the data, then that is why the record id is an empty field. You have to check your SQL to make sure it returns valid data (for the formula to work).

Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
hilfy
Admin Group
Admin Group
Avatar

Joined: 20 Nov 2006
Online Status: Offline
Posts: 3702
Quote hilfy Replybullet Posted: 27 Jul 2007 at 9:01am
Add this line at the end of your formula:
 
MyDate
 
-Dell
IP IP Logged
Page  of 2 Next >>
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.