X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 44983D1B8B5 for ; Wed, 20 Aug 2003 20:02:10 -0300 (ADT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 41953-01 for ; Wed, 20 Aug 2003 20:02:01 -0300 (ADT) Received: from abe.micropat.com (abe.micropat.com [204.17.221.119]) by svr1.postgresql.org (Postfix) with ESMTP id B8009D1BAEA for ; Wed, 20 Aug 2003 20:01:54 -0300 (ADT) Received: from micropat.com ([204.17.221.90]) by abe.micropat.com (Netscape Messaging Server 4.15) with ESMTP id HJXV0V00.GNR for ; Wed, 20 Aug 2003 18:12:31 -0400 Message-ID: <3F43F615.D94FF744@micropat.com> Date: Wed, 20 Aug 2003 18:28:37 -0400 From: "Richard Hall" Organization: Micropatent, LLC X-Mailer: Mozilla 4.79 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 Cc: pgsql-sql@postgresql.org Subject: Re: [Newbie] migrating a stored procedure from MSSQL to postgresql References: Content-Type: multipart/alternative; boundary="------------EA2DF53696438223418E731F" X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/256 X-Sequence-Number: 14872 --------------EA2DF53696438223418E731F Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit As declared, your function returns TEXT, i.e. unlimited characters. >> CREATE FUNCTION UpdateOrder(INTEGER) RETURNS TEXT AS Since your variable >> r_SKUPrice RECORD; contains a number of columns >> SELECT SKU, Price INTO r_SKUPrice you could create a composite TYPE that matches those columns and since your variable can contain a number of such rows, (see the select above) the function needs to become a set returning function CREATE FUNCTION UpdateOrder(INTEGER) RETURNS SETOF AS Rick Bengali wrote: > Hi, > I am a postgresql and stored procedures beginner and I > would like to know if the stored procedure I am trying to migrate > to plpgsql from MSSQL is correct. > > Here 's the only table involved in the stored procedure: > create table ManufacturerOrders > ( > OrderNumber serial, > SKU int not null, > Make varchar(50) not null, > Model varchar(50) not null, > Price int not null, > Status varchar(20) not null, > primary key (OrderNumber) > ); > > Here 's the original MSSQL stored procedure: > create procedure UpdateOrder (@OrderNum int) > as > set nocount on > > update ManufacturerOrders set Status = "Shipped" where > OrderNumber = @OrderNum; > > SELECT SKU, Price FROM ManufacturerOrders > WHERE OrderNumber = @OrderNum > go > > Here 's the plpgsql version i wrote: > > CREATE FUNCTION UpdateOrder(INTEGER) RETURNS TEXT AS ' > DECLARE > i_ordernum ALIAS for $1; > r_SKUPrice RECORD; > BEGIN > update ManufacturerOrders set Status = ''Shipped'' where > OrderNumber = i_ordernum; > > SELECT SKU, Price INTO r_SKUPrice FROM ManufacturerOrders WHERE > OrderNumber = i_ordernum; > return r_SKUPrice; > > END; > ' LANGUAGE 'plpgsql'; > > I would like to know especially if the RETURNS statement is correct here > and if i can give a name to the record r_SKUPrice columns . > > Thanks in advance, > Bengali > > ---------------------------(end of broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings --------------EA2DF53696438223418E731F Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit As declared, your function returns TEXT, i.e. unlimited characters.
>>  CREATE FUNCTION UpdateOrder(INTEGER) RETURNS TEXT AS

Since your variable
>>  r_SKUPrice RECORD;
contains a number of columns
>>  SELECT SKU, Price INTO r_SKUPrice
you could create a composite TYPE that matches those columns

and
since your variable can contain a number of such rows, (see the select above)
the function needs to become a set returning function

CREATE FUNCTION UpdateOrder(INTEGER) RETURNS SETOF <your_type_here> AS

Rick
 

Bengali wrote:

Hi,
I am a postgresql and stored procedures beginner and I
would like to know if the stored procedure I am trying to migrate
to plpgsql from MSSQL is correct.

Here 's the only table involved in the stored procedure:
create table ManufacturerOrders
(
     OrderNumber serial,
     SKU     int not null,
     Make    varchar(50) not null,
     Model   varchar(50) not null,
     Price   int not null,
     Status varchar(20) not null,
     primary key (OrderNumber)
);

Here 's the original MSSQL stored procedure:
create procedure UpdateOrder (@OrderNum int)
as
        set nocount on

        update ManufacturerOrders set Status = "Shipped" where
        OrderNumber = @OrderNum;

        SELECT SKU, Price FROM ManufacturerOrders
        WHERE OrderNumber = @OrderNum
go

Here 's the plpgsql version i wrote:

CREATE FUNCTION UpdateOrder(INTEGER) RETURNS TEXT AS '
  DECLARE
   i_ordernum ALIAS for $1;
   r_SKUPrice RECORD;
  BEGIN
         update ManufacturerOrders set Status = ''Shipped'' where
OrderNumber = i_ordernum;

         SELECT SKU, Price INTO r_SKUPrice FROM ManufacturerOrders WHERE
OrderNumber = i_ordernum;
         return r_SKUPrice;

  END;
  ' LANGUAGE 'plpgsql';

I would like to know especially if the RETURNS statement is correct here
and if i can give a name to the record r_SKUPrice columns .

Thanks in advance,
Bengali

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

--------------EA2DF53696438223418E731F--