Solved

Informix - How to return table from procedure

Posted on 2014-04-09
4
397 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

744 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