Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E1A8B632FEF for ; Thu, 5 Nov 2009 16:54:06 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 86884-07 for ; Thu, 5 Nov 2009 20:53:52 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-yx0-f181.google.com (mail-yx0-f181.google.com [209.85.210.181]) by mail.postgresql.org (Postfix) with ESMTP id 1AAB6632381 for ; Thu, 5 Nov 2009 16:53:55 -0400 (AST) Received: by yxe11 with SMTP id 11so350515yxe.15 for ; Thu, 05 Nov 2009 12:53:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=tSBKiLZEQIeufVV+ulftqb4mqFobAOj3BrOnHBMnblU=; b=hToMLdXs+nuI0N2kfNUY9wFRrr5aY66IzLaO9Hwf11ejUMRoAqkKPJeH5x9oBMRnl0 uZ5vN2GFgvNZBMW3uIU8eGLTqOxOcbWnTOlyroUNxqHJMkNkFhlnpai+R90QCdNTlL4t WxECwg0yUArFMWXl1A4g6GWCGHym7ZcAeyzY4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=d89DeDshlvF+iHMyc5Dr5rnTMuyJUrZF0u7+5qMEnCMWBLacG0u8UAt5P1lO9Z2az2 H6eY+pty8Sp8IXMgwM9XxPvivU3nTYYLkGwELBsntKfoeyy0PAYeaKWUZARhmsMUQjIe kANj1lIR37vn3/FiG5KPlanV3rzfeApDUuQgM= MIME-Version: 1.0 Received: by 10.150.166.3 with SMTP id o3mr6181907ybe.33.1257454434685; Thu, 05 Nov 2009 12:53:54 -0800 (PST) In-Reply-To: <26221691.post@talk.nabble.com> References: <26221691.post@talk.nabble.com> Date: Thu, 5 Nov 2009 21:53:54 +0100 Message-ID: <162867790911051253q2d18ffebw45ec20c710257a60@mail.gmail.com> Subject: Re: Converting T-SQL to PostgreSQL From: Pavel Stehule To: maboyz Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.583 tagged_above=-10 required=5 tests=AWL=0.016, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200911/26 X-Sequence-Number: 33377 Hello 2009/11/5 maboyz : > > I am trying to re write the following stored proc as a postgresql > function......but am new to postgres and it is kind of getting really > confusing trying to learn and let alone understand the syntax: > > CREATE PROCEDURE [dbo].[GetAppAvailability] > =C2=A0 =C2=A0 =C2=A0 =C2=A0@AuditAvailable =C2=A0 =C2=A0 =C2=A0 =C2=A0 bi= t output, > =C2=A0 =C2=A0 =C2=A0 =C2=A0@BillingAvailable =C2=A0 =C2=A0 =C2=A0 bit out= put, > =C2=A0 =C2=A0 =C2=A0 =C2=A0@ReturnValue =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0int output > AS > SET NOCOUNT ON > > set @AuditAvailable =3D (select app_Status from AppAvailability where > app_Functionality =3D 'Audit') > set @BillingAvailable =3D (select app_Status from AppAvailability where > app_Functionality =3D 'Billing') > > Set @ReturnValue =3D @@Error > > I have this in postgres but obviously it is not correct: > > CREATE OR REPLACE FUNCTION GetAppAvailability(OUT auditAvailable characte= r > varying, OUT billingAvailable character varying) > =C2=A0 AS > $BODY$ > =C2=A0 =C2=A0 set $1 =3D (select "app_Status" from "AppAvailability" wher= e > "app_Functionality" =3D 'Audit'); > =C2=A0 =C2=A0set $2 =3D (select "app_Status" from "AppAvailability" where > "app_Functionality" =3D 'Billing'); > =C2=A0 =C2=A0 =C2=A0 =C2=A0$BODY$ > =C2=A0LANGUAGE 'sql' VOLATILE > =C2=A0COST 100; > You can use SQL functions, but SQL has not assign statement. So you have to use plpgsql language. http://www.postgresql.org/docs/8.4/interactive/plpgsql.html You code should be CREATE OR REPLACE FUNCTION GetAppAvailability(OUT auditAvailable character varying, OUT billingAvailable character varying) AS $BODY$ $1 =3D (select "app_Status" from "AppAvailability" where "app_Functionality" =3D 'Audit'); $2 =3D (select "app_Status" from "AppAvailability" where "app_Functionality" =3D 'Billing'); $BODY$ LANGUAGE plpgsql VOLATILE Usually is better if you forgot on T-SQL and try to learn language again. there are some good advices: a) don't use camel notation for identifiers b) don't use case sensitive identifiers like "some" c) don't create "SELECT only" functions Why do you use function? use view: CREATE VIEW GetAppAvailability AS SELECT (SELECT "app_Status" FROM "AppAvailability" WHERE "app_Functionality" =3D 'Audit'), (SELECT "app_Status" FROM "AppAvailability" WHERE "app_Functionality" =3D 'Billing'); some link http://www.postgres.cz/index.php/PL/pgSQL_%28en%29 regards Pavel Stehule > > -- > View this message in context: http://old.nabble.com/Converting-T-SQL-to-P= ostgreSQL-tp26221691p26221691.html > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql >