Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: T-SQL in Crystal Post Reply Post New Topic
Author Message
jordonshaw
Newbie
Newbie
Avatar

Joined: 07 Aug 2009
Online Status: Offline
Posts: 9
Quote jordonshaw Replybullet Topic: T-SQL in Crystal
     Posted: 07 Aug 2009 at 8:34am
Ok, so here is the issue that I have.  I'm a DBA.  I know SQL very well.  I wrote a T-SQL script that adds employee's salary based on the months selected.  It works perfectly in SQL.  We have a Crystal Report Manager who knows Crystal Reports "kind of" :-).  She doesn't know SQL at all.  So, between the two of us, we are as lost as last year's easter egg!  Basically, I need to run my t-sql script inside of Crystal, in order to generate a report.  So, here is my T-SQL script:
 
DECLARE  @StartMonth INT,
   @EndMonth   INT,
   @Year INT
SET @StartMonth = 1
SET @EndMonth = 12
SET @Year = 2009;
WITH EmployeeCte(EMPLOYID,MONTH,YEAR,GROSWAGS)
   AS (SELECT EMPLOYID,
               1,
               YEAR1,
              GROSWAGS_1
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               2,
               YEAR1,
              GROSWAGS_2
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               3,
               YEAR1,
              GROSWAGS_3
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               4,
               YEAR1,
              GROSWAGS_4
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               5,
               YEAR1,
              GROSWAGS_5
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               6,
               YEAR1,
              GROSWAGS_6
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               7,
               YEAR1,
              GROSWAGS_7
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               8,
               YEAR1,
              GROSWAGS_8
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               9,
               YEAR1,
              GROSWAGS_9
       FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               10,
               YEAR1,
             GROSWAGS_10
      FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               11,
               YEAR1,
             GROSWAGS_11
      FROM   [UPR00900]
       UNION ALL
       SELECT EMPLOYID,
               12,
               YEAR1,
             GROSWAGS_12
      FROM   [UPR00900])
SELECT   cast(sum(GROSWAGS) AS MONEY) AS TotalWage
FROM     EmployeeCte
WHERE    [Month] BETWEEN @StartMonth AND @EndMonth AND [Year] = @Year
group by EMPLOYID;
 
I need to be able to select in Crystal to set the varible @StartMonth and @EndMonth, and @Year, based on the selection from the user.  Then show me what the TotalWage is.
 
Is what I'm wanting to do possible?
 
Thanks,
Jordon Shaw
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 07 Aug 2009 at 9:55am
Can you create a Stored Procedure in the DB that youa re calling the report from?
If so ceate your SP using your above process and use it as the datasource (via an ODBC connection you can add the SP the same as a table, hopefully your Crystal person knows that much).
Once connected, @StartMonth and @EndMonth, and @Year should appear as parameters in crystal and act like a parma that was made ihn crystal.
Hope this helps.
IP IP Logged
jordonshaw
Newbie
Newbie
Avatar

Joined: 07 Aug 2009
Online Status: Offline
Posts: 9
Quote jordonshaw Replybullet Posted: 10 Aug 2009 at 5:49am
That worked perfectly, kind of.  I'm able to run the SP inside of SQL and get all my results; however, when I put that field in crystal, I'm only getting one result!  The result is correct and is exactly what I want, only I want it for every record in the table, not just one.  Any ideas on this?
 
Jordon
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 10 Aug 2009 at 6:44am
Where are you placing it in Crystal?
If you place it on any location other than the detail section it will only show you the first row from your query.
The detail section will display al rows.
Also male sure there are no select statements filtering the data in Crystal.
Is it still only one record?
IP IP Logged
jordonshaw
Newbie
Newbie
Avatar

Joined: 07 Aug 2009
Online Status: Offline
Posts: 9
Quote jordonshaw Replybullet Posted: 10 Aug 2009 at 6:55am
It is very strange.  It is in the details section and there is no select statement on the field.  I run it in SQL, works perfectly, I get all of my results.  I can even right click on the field of the report and click Browse Field Data and it will give me all the results; however, when I run the report, I'm only getting one row and its not even the first row.
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 10 Aug 2009 at 6:58am
Did you join this SP to any table (or view or other sp) in crystal?
IP IP Logged
jordonshaw
Newbie
Newbie
Avatar

Joined: 07 Aug 2009
Online Status: Offline
Posts: 9
Quote jordonshaw Replybullet Posted: 10 Aug 2009 at 7:25am
I got it to work.  I deleted my linking and redone it and now it works.  Weird, but I'm glad its fixed!!!  Thanks for your help!!!
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