Solved

Informix - How to return table from procedure

Posted on 2014-04-09
4
403 Views
Last Modified: 2014-10-17
Hi All

(Warning:  SQL expert, Informix N00b)

I'm attempting to create a procedure where it returns a table of data, based on my experience with SQL Server doing the same, and could use some help.

From the '-- up to this point the proc' on down, can someone show me the syntax for a SELECT statement to be returned as a set?

Thanks in advance.
Jim

CREATE PROCEDURE rpt_sales_distribution (iYear int, iMonth int) AS

BEGIN

define iYear integer = 2014;
define iMonth integer = 4;
define iWeekNumber integer = 1;

define dtStart DATETIME YEAR TO DAY;
define dtEnd DATETIME YEAR TO DAY;

if iWeekNumber = 1 then
   let dtStart = to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-01"); 
   let dtEnd = to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-07");
elseif iWeekNumber = 2 then
   let dtStart = to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-08") ;
   let dtEnd = to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-14");
elseif iWeekNumber = 3 then
   let dtStart = to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-15") ;
   let dtEnd = to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-21");
elseif iWeekNumber = 4 then
   let dtStart = to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-22"); 
   let dtEnd = LAST_DAY(to_date(to_char(iYear) || "-" || to_char(iMonth) + || "-07"));
end if;

-- up to this point the proc saves fine, but I'd like to make it return the below set
select 1 as week_id,
       11 as src_id, 
       '11 SY Agt Portal' as src_name 
      ,count(*) as seg
      ,sum(a.unitrate) as rev
      ,round(sum(a.unitrate)/count(*)) as avg
  from wsord_det a
      ,wsord_hdr b
      ,wsord_tkt c
 where b.orddate between dtStart_week1 and dtEnd_week1 

Open in new window

0
Comment
Question by:Jim Horn
  • 2
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 500 total points
ID: 39992753
By using dtStart and dtEnd in the WHERE clause, Are you not getting what you want?
select 1 as week_id,
       11 as src_id, 
       '11 SY Agt Portal' as src_name 
      ,count(*) as seg
      ,sum(a.unitrate) as rev
      ,round(sum(a.unitrate)/count(*)) as avg
  from wsord_det a
      ,wsord_hdr b
      ,wsord_tkt c
 where b.orddate between dtStart and dtEnd

Open in new window

0
 
LVL 65

Author Comment

by:Jim Horn
ID: 40006792
(Sorry for the delay)

The below SQL returns a set
select 1 as week_id,
       11 as src_id, 
       '11 SY Agt Portal' as src_name 
      ,count(*) as seg
      ,sum(a.unitrate) as rev
      ,round(sum(a.unitrate)/count(*)) as avg
  from wsord_det a
      ,wsord_hdr b
      ,wsord_tkt c
 where b.orddate between dtStart and dtEnd

Open in new window

When I try to use variables here's the result..Returns multiple 'A syntax error has occured' lines
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 40013263
try this.
define dtStart DATETIME YEAR TO DAY, dtEnd DATETIME YEAR TO DAY

let dtStart = '2017-04-01'
let dtEnd = '2014-04-07'

select 1 as week_id,
       11 as src_id, 
       '11 SY Agt Portal' as src_name 
      ,count(*) as seg
      ,sum(a.unitrate) as rev
      ,round(sum(a.unitrate)/count(*)) as avg
  from wsord_det a
      ,wsord_hdr b
      ,wsord_tkt c
 where b.orddate between dtStart and dtEnd

Open in new window

0
 
LVL 65

Author Closing Comment

by:Jim Horn
ID: 40387308
I've moved to a different gig and can't continue this issue, so I'm going to close the question and spread the wealth around.  Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now