Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 8382C632FEF for ; Thu, 5 Nov 2009 16:56:05 -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 87092-07 for ; Thu, 5 Nov 2009 20:55:51 +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 CCBAA632381 for ; Thu, 5 Nov 2009 16:55:54 -0400 (AST) Received: by yxe11 with SMTP id 11so351910yxe.15 for ; Thu, 05 Nov 2009 12:55:53 -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=RiJx/VcdCIsHG1J7oeDisaBzoJmUzQEiaEvx5GRs2oM=; b=U9qHriTPOkfRF/MsPzIarLoj25Btkg/737b809yClG/hUCpd9HzqjpQFq+nFBk5PA2 yRzWlKL9B0OLmrDwxTeszSwGByESsI1ZIJYRo/3lO8NB/Rrup6tljFdzacI8+khSrwWk mVXELhkRgNy1NH9ErC9UGiwL6dpkwATzKvGqc= 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=rYV1YUGhLENLaODkGLd1sxYXkwTDQuMHQXv1OiCyBGPAeun+3jWdaNMsmVB/6HLxE7 QTTLHI5vgyG7Q6j3RNkUTu3WSo/nc+GwPFi2v7r8otGMQ5YzP1GVfccYbgbvizmllsDr R+tdnZsujxEYVaZPGJoQxGjQQYMJJw4FbSnOg= MIME-Version: 1.0 Received: by 10.150.234.2 with SMTP id g2mr6122649ybh.199.1257454553621; Thu, 05 Nov 2009 12:55:53 -0800 (PST) In-Reply-To: <162867790911051253q2d18ffebw45ec20c710257a60@mail.gmail.com> References: <26221691.post@talk.nabble.com> <162867790911051253q2d18ffebw45ec20c710257a60@mail.gmail.com> Date: Thu, 5 Nov 2009 21:55:53 +0100 Message-ID: <162867790911051255v337a9bc7h8c8cba7577c84075@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/27 X-Sequence-Number: 33378 2009/11/5 Pavel Stehule : > 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 b= it output, >> =C2=A0 =C2=A0 =C2=A0 =C2=A0@BillingAvailable =C2=A0 =C2=A0 =C2=A0 bit ou= tput, >> =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 charact= er >> varying, OUT billingAvailable character varying) >> =C2=A0 AS >> $BODY$ >> =C2=A0 =C2=A0 set $1 =3D (select "app_Status" from "AppAvailability" whe= re >> "app_Functionality" =3D 'Audit'); >> =C2=A0 =C2=A0set $2 =3D (select "app_Status" from "AppAvailability" wher= e >> "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 characte= r > =C2=A0varying, OUT billingAvailable character varying) > =C2=A0 AS > =C2=A0$BODY$ BEGIN > =C2=A0 =C2=A0 $1 =3D (select "app_Status" from "AppAvailability" where > =C2=A0"app_Functionality" =3D 'Audit'); > =C2=A0 =C2=A0$2 =3D (select "app_Status" from "AppAvailability" where > =C2=A0"app_Functionality" =3D 'Billing'); END > =C2=A0 =C2=A0 =C2=A0 =C2=A0$BODY$ > =C2=A0LANGUAGE plpgsql VOLATILE > sorry, I left out main block. plpgsql function have to have block etc BEGIN .... END; > 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 > =C2=A0SELECT (SELECT "app_Status" > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FROM "AppA= vailability" > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0WHERE "app_= Functionality" =3D 'Audit'), > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(SELECT "app_Statu= s" > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0FROM= "AppAvailability" > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 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-= PostgreSQL-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 >> >