Thread: Dynamic sql example

Dynamic sql example

From
"ramasubramanian"
Date:
Dear All.
    Can any one give me dynamic sql in postgres stored procedure using "USING CLAUSE"
Regards,
Ram

Re: Dynamic sql example

From
Pavel Stehule
Date:
2009/11/24 ramasubramanian <ramasubramanian.g@renaissance-it.com>:
> Dear All.
>     Can any one give me dynamic sql in postgres stored procedure using
> "USING CLAUSE"

CREATE TABLE tab(a integer);

CREATE OR REPLACE FUNCTION foo(_a integer)
RETURNS void AS $$
DECLARE r record;
BEGIN
  FOR r IN EXECUTE 'SELECT * FROM tab WHERE a = $1' USING _a LOOP
    RAISE NOTICE '%', r.a;
  END LOOP;
END;
$$ LANGUAGE plpgsql;

regards
Pavel Stehule



> Regards,
> Ram

Sql result b where condition

From
"ramasubramanian"
Date:
Hi all,
    I have a table emp. using where condition can i get the result
prioritized.
Take the example below.

select ENAME,ORIG_SALARY from employee where (ename='Tom' and
orig_salary=2413)or(orig_salary=1234 )

if the fist condition(ename='Tom' and orig_salary=2413) is satified then 10
rows will be returned, for the second condition (orig_salary=1234 ) there
are 20 rows will be returned.
The order of display should be

The first 10 rows then
next 20 rows.
Thanks & Regards,
Ram



Re: Sql result b where condition

From
"A. Kretschmer"
Date:
In response to ramasubramanian :

Please, create a new mail for a new topic and don't hijack other
threads.


> Hi all,
>    I have a table emp. using where condition can i get the result
> prioritized.
> Take the example below.
>
> select ENAME,ORIG_SALARY from employee where (ename='Tom' and
> orig_salary=2413)or(orig_salary=1234 )
>
> if the fist condition(ename='Tom' and orig_salary=2413) is satified then 10
> rows will be returned, for the second condition (orig_salary=1234 ) there
> are 20 rows will be returned.
> The order of display should be
>
> The first 10 rows then
> next 20 rows.
> Thanks & Regards,
> Ram

For instance:

select ENAME,ORIG_SALARY, 1 as my_order from employee where (ename='Tom' and
orig_salary=2413) union all select ENAME,ORIG_SALARY, 2 employee where
(orig_salary=1234 ) order by my_order.

other solution (untested):

select ENAME,ORIG_SALARY, case when (ename='Tom' and orig_salary=2413)
then 1 else 2 end as my_order from employee where (ename='Tom' and
orig_salary=2413)or(orig_salary=1234 ) order by my_order;


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

Re: Sql result b where condition

From
Matthew Wakeling
Date:
On Mon, 25 Jan 2010, A. Kretschmer wrote:
> In response to ramasubramanian :
>
> Please, create a new mail for a new topic and don't hijack other
> threads.

Even more so - this isn't probably the right mailing list for generic sql
help questions.

>> select ENAME,ORIG_SALARY from employee where (ename='Tom' and
>> orig_salary=2413)or(orig_salary=1234 )
>>
>> if the fist condition(ename='Tom' and orig_salary=2413) is satified then 10
>> rows will be returned, for the second condition (orig_salary=1234 ) there
>> are 20 rows will be returned.
>> The order of display should be
>>
>> The first 10 rows then
>> next 20 rows.

> select ENAME,ORIG_SALARY, 1 as my_order from employee where (ename='Tom' and
> orig_salary=2413) union all select ENAME,ORIG_SALARY, 2 employee where
> (orig_salary=1234 ) order by my_order.

Or just:

select ENAME,ORIG_SALARY from employee where (ename='Tom' and
orig_salary=2413)or(orig_salary=1234 ) ORDER BY orig_salary DESC

as there is going to be only two values for orig_salary.

Matthew

--
 The early bird gets the worm. If you want something else for breakfast, get
 up later.

Re: Sql result b where condition

From
"A. Kretschmer"
Date:
In response to Matthew Wakeling :
> On Mon, 25 Jan 2010, A. Kretschmer wrote:
> >In response to ramasubramanian :
> >
> >Please, create a new mail for a new topic and don't hijack other
> >threads.
>
> Even more so - this isn't probably the right mailing list for generic sql
> help questions.

ACK.

> >select ENAME,ORIG_SALARY, 1 as my_order from employee where (ename='Tom'
> >and
> >orig_salary=2413) union all select ENAME,ORIG_SALARY, 2 employee where
> >(orig_salary=1234 ) order by my_order.
>
> Or just:
>
> select ENAME,ORIG_SALARY from employee where (ename='Tom' and
> orig_salary=2413)or(orig_salary=1234 ) ORDER BY orig_salary DESC
>
> as there is going to be only two values for orig_salary.

hehe, yes, overseen that fact ;-)


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