Results 1 to 3 of 3

Thread: [RESOLVED] Avoiding duplicate stored procedures

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Recently moved from Europe to Panama
    Posts
    292

    Resolved [RESOLVED] Avoiding duplicate stored procedures

    Hi,

    The reporting area of an app I'm working on, needs to report results either by day, week, month or year. The resulting grid comes from a series of selects & joins, but has the same layout for each of the reports.

    To be efficient, and avoid duplication, I thought to create a single stored procedure, and pass the full date part of the WHERE-clause as a single string parameter like in the code below:

    Code:
    SELECT Prijslijst.SoortIndex, Prijslijst.Omschrijving, Inkoop.X As 'Gewicht KG (Inkoop)', Inkoop.Y As 'Bedrag (Inkoop)', Verkoop.X As 'Gewicht KG (Verkoop)'
    
    FROM 
    	(SELECT Omschrijving, SUM(NettoGewicht) As X, SUM(TotaalPrijs) As Y
    	FROM BonRegels
    	WHERE LEFT(Bonnummer, 2) = 'IK' And @DateCriteria
    	GROUP BY Omschrijving) Inkoop
    
    FULL OUTER JOIN
    
    	(SELECT Omschrijving, SUM(NettoGewicht) As X, SUM(TotaalPrijs) As Y
    	FROM BonRegels
    	WHERE LEFT(Bonnummer, 2) = 'VK' And @DateCriteria
    	GROUP BY Omschrijving) Verkoop
    	
    ON Inkoop.Omschrijving = Verkoop.Omschrijving
    
    LEFT JOIN Prijslijst
    ON (Inkoop.Omschrijving = Prijslijst.Omschrijving) Or (Verkoop.Omschrijving = Prijslijst.Omschrijving)
    
    ORDER BY SoortIndex
    Now this is not accepted by SQL Server.

    The question is:

    1. Do I create 4 stored procedures each with a specific WHERE clause, running the risk having to update 4 times when a change in layout is requested?

    or

    2. Is there a smart way to create a single stored procedure?

    or

    3. Should I go for a single view without the WHERE-clause, and the create 4 different stored procedures for each of the day/week/month/year reports?

    Thanks in advance for your help!

    Regards,
    Erwin

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Avoiding duplicate stored procedures

    Write a sproc that accepts parameters for an arbitrary date range and then pass the appropriate parameter values for the range you want. You could even then four separate additional sprocs that do nothing but execute the first one but pass the appropriate calculated date values for the day, week, month or year ranges you want for your reports. That actually means one extra sproc but no duplication of code because the last four will be so simple.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Recently moved from Europe to Panama
    Posts
    292

    Re: Avoiding duplicate stored procedures

    Thanks for the feedback! I decided to go with a view that had the joins in it, and 4 stored procedures that simply do a SELECT * on the view with a specific WHERE clause for each of the options (day, weeknumber, etc.).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width