Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Informix - How to return table from procedure

Posted on 2014-04-09
4
Medium Priority
?
448 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 41

Assisted Solution

by:Sharath
Sharath earned 2000 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 66

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 41

Accepted Solution

by:
Sharath earned 2000 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 66

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

877 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