473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Stored procedure question

I'm still a bit new to MS SQL and was wondering if there was a better way to do what I want.

If the user supplies a location the LocID with be > 0 and I want to select the data where emp.LOCATION_ID = @LocID. If the LocID = 0 then I don't want to include that in the where clause.

I wrote something that works but seems like there should be an easier way to do this.

Any comments or suggestions are welcome.

Thanks!

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE GET_REPORT
  2.     -- Add the parameters for the stored procedure here
  3.     @startDate datetime,
  4.     @endDate datetime,
  5.     @LocID int
  6. AS
  7. BEGIN
  8.     -- SET NOCOUNT ON added to prevent extra result sets from
  9.     -- interfering with SELECT statements.
  10.     SET NOCOUNT ON;
  11.  
  12.     IF ( @LocID = 0)
  13.         BEGIN
  14.             SELECT
  15.             MAX(distinct qad.QA_DATA_ID), 
  16.             CONVERT(VARCHAR,qad.DATA_DATE, 101) DATA_DATE, 
  17.             SUM(qad.NEW_USERS) NEW_USERS, 
  18.             SUM(qad.TRANSACTIONS),  
  19.             SUM(qad.CORP_TRANSACTIONS), 
  20.             SUM(qad.CALLS_QUEUE) CALLS, 
  21.             SUM(qad.CORP_CALLS) CORP_CALLS, 
  22.             SUM(qad.CALLS_DN) DN_CALLS,  
  23.             SUM(qad.DN_TRANSFERS) DN_TRANS, 
  24.             SUM(qad.CALLS_SHORT) SHORT_CALLS, 
  25.             dbo.GET_CONVERSION (SUM(qad.TRANSACTIONS), 
  26.                     SUM(qad.CORP_TRANSACTIONS),
  27.                     (SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4)AND 
  28.                     (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))), 
  29.                     SUM(qad.CALLS_QUEUE),  
  30.                     SUM(qad.CALLS_DN), 
  31.                     SUM(qad.CORP_CALLS), 
  32.                     SUM(qad.CALLS_SHORT), 
  33.                     SUM(qad.DN_TRANSFERS)) ttlCon, 
  34.             CONVERT(VARCHAR, SUM(qad.SALES_COBRAND), 1) SALES_COBRAND, 
  35.             CONVERT(VARCHAR, SUM(qad.SALES_GOLD), 1) SALES_GOLD, 
  36.             CONVERT(VARCHAR, SUM(qad.SALES_CORP), 1) SALES_CORP,  
  37.             CONVERT(VARCHAR, (SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4)AND 
  38.             (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))), 1) AS SALES_LS, 
  39.             (SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4)AND 
  40.             (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))) AS LS_TRANSACTIONS, 
  41.             CONVERT(VARCHAR, dbo.TOTAL_TS( 
  42.                 SUM(qad.SALES_COBRAND),
  43.                 SUM(qad.SALES_GOLD), 
  44.                 SUM(qad.SALES_CORP),
  45.                 (SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND 
  46.             (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE)))), 1) TTS
  47.             FROM QA_DATA qad JOIN EMPLOYEE emp 
  48.                 ON emp.EMPLOYEE_ID = qad.EMPLOYEE_ID 
  49.         WHERE qad.DATA_DATE >= @startDate AND 
  50.         qad.DATA_DATE <  @endDate AND 
  51.         emp.REPORTABLE = 1 AND 
  52.         emp.ENABLED > 0 AND 
  53.         (dbo.IS_IN_DEPT(emp.DEPARTMENT_ID, 'inside sales') = 'true') 
  54.         GROUP BY qad.DATA_DATE
  55.  
  56.         END
  57.     ELSE
  58.         BEGIN
  59.  
  60.             SELECT
  61.             MAX(distinct qad.QA_DATA_ID), 
  62.             CONVERT(VARCHAR,qad.DATA_DATE, 101) DATA_DATE, 
  63.             SUM(qad.NEW_USERS) NEW_USERS, 
  64.             SUM(qad.TRANSACTIONS),  
  65.             SUM(qad.CORP_TRANSACTIONS), 
  66.             SUM(qad.CALLS_QUEUE) CALLS, 
  67.             SUM(qad.CORP_CALLS) CORP_CALLS, 
  68.             SUM(qad.CALLS_DN) DN_CALLS,  
  69.             SUM(qad.DN_TRANSFERS) DN_TRANS, 
  70.             SUM(qad.CALLS_SHORT) SHORT_CALLS, 
  71.             dbo.GET_CONVERSION (SUM(qad.TRANSACTIONS), 
  72.                     SUM(qad.CORP_TRANSACTIONS),
  73.                     (SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND 
  74.                     (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))), 
  75.                     SUM(qad.CALLS_QUEUE),  
  76.                     SUM(qad.CALLS_DN), 
  77.                     SUM(qad.CORP_CALLS), 
  78.                     SUM(qad.CALLS_SHORT), 
  79.                     SUM(qad.DN_TRANSFERS)) ttlCon,
  80.             CONVERT(VARCHAR, SUM(qad.SALES_COBRAND), 1) SALES_COBRAND, 
  81.             CONVERT(VARCHAR, SUM(qad.SALES_GOLD), 1) SALES_GOLD, 
  82.             CONVERT(VARCHAR, SUM(qad.SALES_CORP), 1) SALES_CORP,  
  83.             CONVERT(VARCHAR, (SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 OR tl.TRANSACTION_ID IS NULL)AND 
  84.             (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE)OR tl.TRANSACTION_ID IS NULL)), 1) AS SALES_LS, 
  85.             (SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND 
  86.             (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))) AS LS_TRANSACTIONS, 
  87.             CONVERT(VARCHAR, dbo.TOTAL_TS( 
  88.                 SUM(qad.SALES_COBRAND),
  89.                 SUM(qad.SALES_GOLD), 
  90.                 SUM(qad.SALES_CORP),
  91.                 (SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND 
  92.             (dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE)))), 1) TTS
  93.             FROM QA_DATA qad JOIN EMPLOYEE emp 
  94.                 ON emp.EMPLOYEE_ID = qad.EMPLOYEE_ID 
  95.             WHERE qad.DATA_DATE >= @startDate AND 
  96.             qad.DATA_DATE <  @endDate AND 
  97.             emp.REPORTABLE = 1 AND 
  98.             emp.ENABLED > 0 AND 
  99.             (dbo.IS_IN_DEPT(emp.DEPARTMENT_ID, 'inside sales') = 'true') AND
  100.                 emp.LOCATION_ID = @LocID
  101.                 GROUP BY qad.DATA_DATE
  102.         END
  103. END
Aug 10 '07 #1
0 1201

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ryan.Chowdhury | last post by:
This is a general question regarding the use of view and stored procedures. I'm fairly new to databases and SQL. I've created a SQL database using an Access Data Project ("ADP") and I'm...
1
by: Raquel | last post by:
Have a question on the Stored procedure method code generated by DB2 development center for Java stored procedures. Suppose I have a requirement to return the resultset consisting of FIRSTNME,...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
5
by: Rhino | last post by:
This question relates to DB2 Version 6 on OS/390. Can a (COBOL) stored procedure on this platform do file I/O, i.e. write to a sequential file? I am trying to debug a stored procedure. As far...
5
by: Andy G | last post by:
I have a registration page that captures 75% of the users data. After they enter that info they are redirected to one of two pages depending on how they answered a question on the registation...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
5
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored...
11
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.