Dynamic SQL with pgsql, how to?

Lists: pgsql-general
From: Andre Lopes <lopes80andre(at)gmail(dot)com>
To: postgresql Forums <pgsql-general(at)postgresql(dot)org>
Subject: Dynamic SQL with pgsql, how to?
Date: 2010-05-03 08:44:28
Message-ID: n2g18f98e681005030144x80e99b9n6155a56c55ce4372@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi,

I need to write some dynamic SQL in pgsql.

I have to do something like this:

[code=SQL Server]
SET @STRINGN = @STRINGN + ' AND A.' + @CAMPOFECINI + ' IN (SELECT
MAX(B.' + @CAMPOFECINI + ')
FROM ' + @TABLA + ' B
WHERE B.ID_SOCIEDAD = A.ID_SOCIEDAD
AND B.ID_EMPREGAD = A.ID_EMPREGAD'
IF @F_ALTA IS NOT NULL
SET @STRINGN = @STRINGN + ' AND B.DAT_INI_ACT_EMP =
A.DAT_INI_ACT_EMP'
SET @STRINGN = @STRINGN + ')'
EXEC sp_executesql @STRINGN,
N'@FINI2 datetime out, @FFIN2 datetime out, @CAMP2
varchar(50) out',
@FINI out, @FFIN out , @CAMP out
[/code]

There is documentation on how can I do this in pgsql?

Best Regards,


From: Jorge Arevalo <jorgearevalo(at)gis4free(dot)org>
To: Andre Lopes <lopes80andre(at)gmail(dot)com>
Cc: postgresql Forums <pgsql-general(at)postgresql(dot)org>
Subject: Re: Dynamic SQL with pgsql, how to?
Date: 2010-05-03 08:55:47
Message-ID: y2td321138e1005030155nff63077axa5920c8b61da0b62@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, May 3, 2010 at 10:44 AM, Andre Lopes <lopes80andre(at)gmail(dot)com> wrote:
> Hi,
>
> I need to write some dynamic SQL in pgsql.
>
> I have to do something like this:
>
> [code=SQL Server]
>     SET @STRINGN = @STRINGN + ' AND A.' + @CAMPOFECINI + ' IN (SELECT
> MAX(B.' + @CAMPOFECINI + ')
>             FROM ' + @TABLA + ' B
>             WHERE B.ID_SOCIEDAD = A.ID_SOCIEDAD
>             AND B.ID_EMPREGAD = A.ID_EMPREGAD'
>     IF @F_ALTA IS NOT NULL
>         SET @STRINGN = @STRINGN + ' AND B.DAT_INI_ACT_EMP =
> A.DAT_INI_ACT_EMP'
>     SET @STRINGN = @STRINGN + ')'
>        EXEC sp_executesql @STRINGN,
>                 N'@FINI2 datetime out, @FFIN2 datetime out, @CAMP2
> varchar(50) out',
>                 @FINI out, @FFIN out , @CAMP out
> [/code]
>
> There is documentation on how can I do this in pgsql?
>
> Best Regards,
>

Hi Andre,

This may helps

http://www.postgresql.org/docs/8.4/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Best regards,
Jorge


From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic SQL with pgsql, how to?
Date: 2010-05-03 09:05:21
Message-ID: 20100503090520.GA4780@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

In response to Andre Lopes :
> Hi,
>
> I need to write some dynamic SQL in pgsql.
> There is documentation on how can I do this in pgsql?

Sure,
http://www.postgresql.org/docs/8.4/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Regards, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99


From: Andre Lopes <lopes80andre(at)gmail(dot)com>
To: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic SQL with pgsql, how to?
Date: 2010-05-03 21:50:06
Message-ID: n2z18f98e681005031450p91819fdawda1e72bec956ff0a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thanks for the reply's,

I need to do a Dynamic SELECT INTO. There is a way of doing it?

Best Regards,

On Mon, May 3, 2010 at 10:05 AM, A. Kretschmer <
andreas(dot)kretschmer(at)schollglas(dot)com> wrote:

> In response to Andre Lopes :
> > Hi,
> >
> > I need to write some dynamic SQL in pgsql.
> > There is documentation on how can I do this in pgsql?
>
> Sure,
>
> http://www.postgresql.org/docs/8.4/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
> Regards, Andreas
> --
> Andreas Kretschmer
> Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
> GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>


From: Maximilian Tyrtania <maximilian(dot)tyrtania(at)byte-employer(dot)de>
To: Andre Lopes <lopes80andre(at)gmail(dot)com>
Cc: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic SQL with pgsql, how to?
Date: 2010-05-04 06:44:34
Message-ID: BB360E2A-C056-41C5-A2C5-AC909BDE9B6D@byte-employer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Am 03.05.2010 um 23:50 schrieb Andre Lopes:

> Thanks for the reply's,
>
> I need to do a Dynamic SELECT INTO. There is a way of doing it?

Yes. Plpgsql supports this:

EXECUTE command-string [ INTO [STRICT] target ] [ USING expression [, ... ] ];

See http://developer.postgresql.org/pgdocs/postgres/plpgsql-statements.html

Max

Maximilian Tyrtania Software-Entwicklung
Dessauer Str. 6-7
10969 Berlin
Tel.: ++49/30/48827-952
Mobil: 0152/292 707 36
email: maximilian(dot)tyrtania(at)byte-employer(dot)de


From: Andre Lopes <lopes80andre(at)gmail(dot)com>
To: Maximilian Tyrtania <maximilian(dot)tyrtania(at)byte-employer(dot)de>
Cc: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic SQL with pgsql, how to?
Date: 2010-05-04 08:16:34
Message-ID: k2q18f98e681005040116zd215905dr7266926c5bbf5091@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thanks for the reply,

It is working now.

Best Regards.

On Tue, May 4, 2010 at 7:44 AM, Maximilian Tyrtania <
maximilian(dot)tyrtania(at)byte-employer(dot)de> wrote:

> Am 03.05.2010 um 23:50 schrieb Andre Lopes:
>
> > Thanks for the reply's,
> >
> > I need to do a Dynamic SELECT INTO. There is a way of doing it?
>
> Yes. Plpgsql supports this:
>
> EXECUTE command-string [ INTO [STRICT] target ] [ USING expression [, ... ]
> ];
>
> See
> http://developer.postgresql.org/pgdocs/postgres/plpgsql-statements.html
>
> Max
>
> Maximilian Tyrtania Software-Entwicklung
> Dessauer Str. 6-7
> 10969 Berlin
> Tel.: ++49/30/48827-952
> Mobil: 0152/292 707 36
> email: maximilian(dot)tyrtania(at)byte-employer(dot)de