Thread: How to query by column names
This may be a simple but can I create a query such as select (select attname from pg_attribute where attrelid = (select relfilenode from pg_class where relname = 't1') and attisdropped = false and attnum > 0) from t1; I get ERROR: more than one row returned by a subquery used as an expression Thanks Richard
I think this is what you're looking for Richard: SELECT attname FROM pg_attribute pa, pg_class pc WHERE pc.relname = 't1' AND pa.attrelid = pc.relfilenode AND pa.attisdroppedIS FALSE AND pa.attnum > 0; Let me know if it doesn't do what you intended. On Mon, 22 Jan 2007, Richard Ray wrote: > This may be a simple but can I create a query such as > > select (select attname from pg_attribute where attrelid = (select relfilenode > from pg_class where relname = 't1') and attisdropped = false and attnum > 0) > from t1; > > I get > ERROR: more than one row returned by a subquery used as an expression > > Thanks > Richard > > ---------------------------(end of broadcast)--------------------------- > TIP 5: don't forget to increase your free space map settings > > -- Jeff Frost, Owner <jeff@frostconsultingllc.com> Frost Consulting, LLC http://www.frostconsultingllc.com/ Phone: 650-780-7908 FAX: 650-649-1954
This is not exactly what I need I want to return the data in t1 On Mon, 22 Jan 2007, Jeff Frost wrote: > I think this is what you're looking for Richard: > > SELECT attname FROM pg_attribute pa, pg_class pc > WHERE pc.relname = 't1' > AND pa.attrelid = pc.relfilenode > AND pa.attisdropped IS FALSE > AND pa.attnum > 0; > > Let me know if it doesn't do what you intended. > > On Mon, 22 Jan 2007, Richard Ray wrote: > >> This may be a simple but can I create a query such as >> >> select (select attname from pg_attribute where attrelid = (select >> relfilenode from pg_class where relname = 't1') and attisdropped = false >> and attnum > 0) from t1; >> >> I get >> ERROR: more than one row returned by a subquery used as an expression >> >> Thanks >> Richard >> >> ---------------------------(end of broadcast)--------------------------- >> TIP 5: don't forget to increase your free space map settings >> >> > > -- > Jeff Frost, Owner <jeff@frostconsultingllc.com> > Frost Consulting, LLC http://www.frostconsultingllc.com/ > Phone: 650-780-7908 FAX: 650-649-1954 > > ---------------------------(end of broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly >
Perhaps I should have asked this earlier. What information are you trying to extract? On Mon, 22 Jan 2007, Richard Ray wrote: > This is not exactly what I need > I want to return the data in t1 > > On Mon, 22 Jan 2007, Jeff Frost wrote: > >> I think this is what you're looking for Richard: >> >> SELECT attname FROM pg_attribute pa, pg_class pc >> WHERE pc.relname = 't1' >> AND pa.attrelid = pc.relfilenode >> AND pa.attisdropped IS FALSE >> AND pa.attnum > 0; >> >> Let me know if it doesn't do what you intended. >> >> On Mon, 22 Jan 2007, Richard Ray wrote: >> >>> This may be a simple but can I create a query such as >>> >>> select (select attname from pg_attribute where attrelid = (select >>> relfilenode from pg_class where relname = 't1') and attisdropped = false >>> and attnum > 0) from t1; >>> >>> I get >>> ERROR: more than one row returned by a subquery used as an expression >>> >>> Thanks >>> Richard >>> >>> ---------------------------(end of broadcast)--------------------------- >>> TIP 5: don't forget to increase your free space map settings >>> >>> >> >> -- >> Jeff Frost, Owner <jeff@frostconsultingllc.com> >> Frost Consulting, LLC http://www.frostconsultingllc.com/ >> Phone: 650-780-7908 FAX: 650-649-1954 >> >> ---------------------------(end of broadcast)--------------------------- >> TIP 1: if posting/reading through Usenet, please send an appropriate >> subscribe-nomail command to majordomo@postgresql.org so that your >> message can get through to the mailing list cleanly >> > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match > > -- Jeff Frost, Owner <jeff@frostconsultingllc.com> Frost Consulting, LLC http://www.frostconsultingllc.com/ Phone: 650-780-7908 FAX: 650-649-1954
All attributes of t1 Where (select attname from pg_attribute where attrelid = (select relfilenode from pg_class where relname = 't1') and attisdropped = false and attnum > 0) is a substitute for * On Mon, 22 Jan 2007, Jeff Frost wrote: > Perhaps I should have asked this earlier. What information are you trying to > extract? > > On Mon, 22 Jan 2007, Richard Ray wrote: > >> This is not exactly what I need >> I want to return the data in t1 >> >> On Mon, 22 Jan 2007, Jeff Frost wrote: >> >>> I think this is what you're looking for Richard: >>> >>> SELECT attname FROM pg_attribute pa, pg_class pc >>> WHERE pc.relname = 't1' >>> AND pa.attrelid = pc.relfilenode >>> AND pa.attisdropped IS FALSE >>> AND pa.attnum > 0; >>> >>> Let me know if it doesn't do what you intended. >>> >>> On Mon, 22 Jan 2007, Richard Ray wrote: >>> >>>> This may be a simple but can I create a query such as >>>> >>>> select (select attname from pg_attribute where attrelid = (select >>>> relfilenode from pg_class where relname = 't1') and attisdropped = false >>>> and attnum > 0) from t1; >>>> >>>> I get >>>> ERROR: more than one row returned by a subquery used as an expression >>>> >>>> Thanks >>>> Richard >>>> >>>> ---------------------------(end of broadcast)--------------------------- >>>> TIP 5: don't forget to increase your free space map settings >>>> >>>> >>> >>> -- >>> Jeff Frost, Owner <jeff@frostconsultingllc.com> >>> Frost Consulting, LLC http://www.frostconsultingllc.com/ >>> Phone: 650-780-7908 FAX: 650-649-1954 >>> >>> ---------------------------(end of broadcast)--------------------------- >>> TIP 1: if posting/reading through Usenet, please send an appropriate >>> subscribe-nomail command to majordomo@postgresql.org so that your >>> message can get through to the mailing list cleanly >>> >> >> ---------------------------(end of broadcast)--------------------------- >> TIP 9: In versions below 8.0, the planner will ignore your desire to >> choose an index scan if your joining column's datatypes do not >> match >> >> > > -- > Jeff Frost, Owner <jeff@frostconsultingllc.com> > Frost Consulting, LLC http://www.frostconsultingllc.com/ > Phone: 650-780-7908 FAX: 650-649-1954 >
So why are you avoiding "SELECT * FROM t1;" ? You'd probably also be happier using information_schema to get the column names. On Mon, 22 Jan 2007, Richard Ray wrote: > All attributes of t1 > Where (select attname from pg_attribute where attrelid = (select relfilenode > from pg_class where relname = 't1') and attisdropped = false and attnum > 0) > is a substitute for * > > On Mon, 22 Jan 2007, Jeff Frost wrote: > >> Perhaps I should have asked this earlier. What information are you trying >> to extract? >> >> On Mon, 22 Jan 2007, Richard Ray wrote: >> >>> This is not exactly what I need >>> I want to return the data in t1 >>> >>> On Mon, 22 Jan 2007, Jeff Frost wrote: >>> >>>> I think this is what you're looking for Richard: >>>> >>>> SELECT attname FROM pg_attribute pa, pg_class pc >>>> WHERE pc.relname = 't1' >>>> AND pa.attrelid = pc.relfilenode >>>> AND pa.attisdropped IS FALSE >>>> AND pa.attnum > 0; >>>> >>>> Let me know if it doesn't do what you intended. >>>> >>>> On Mon, 22 Jan 2007, Richard Ray wrote: >>>> >>>>> This may be a simple but can I create a query such as >>>>> >>>>> select (select attname from pg_attribute where attrelid = (select >>>>> relfilenode from pg_class where relname = 't1') and attisdropped = false >>>>> and attnum > 0) from t1; >>>>> >>>>> I get >>>>> ERROR: more than one row returned by a subquery used as an expression >>>>> >>>>> Thanks >>>>> Richard >>>>> >>>>> ---------------------------(end of broadcast)--------------------------- >>>>> TIP 5: don't forget to increase your free space map settings >>>>> >>>>> >>>> >>>> -- >>>> Jeff Frost, Owner <jeff@frostconsultingllc.com> >>>> Frost Consulting, LLC http://www.frostconsultingllc.com/ >>>> Phone: 650-780-7908 FAX: 650-649-1954 >>>> >>>> ---------------------------(end of broadcast)--------------------------- >>>> TIP 1: if posting/reading through Usenet, please send an appropriate >>>> subscribe-nomail command to majordomo@postgresql.org so that your >>>> message can get through to the mailing list cleanly >>>> >>> >>> ---------------------------(end of broadcast)--------------------------- >>> TIP 9: In versions below 8.0, the planner will ignore your desire to >>> choose an index scan if your joining column's datatypes do not >>> match >>> >>> >> >> -- >> Jeff Frost, Owner <jeff@frostconsultingllc.com> >> Frost Consulting, LLC http://www.frostconsultingllc.com/ >> Phone: 650-780-7908 FAX: 650-649-1954 >> > > -- Jeff Frost, Owner <jeff@frostconsultingllc.com> Frost Consulting, LLC http://www.frostconsultingllc.com/ Phone: 650-780-7908 FAX: 650-649-1954
On Mon, 22 Jan 2007, Jeff Frost wrote: > So why are you avoiding "SELECT * FROM t1;" ? > I was affeared that if I brought my total ignorance to light I would be band from the list but here goes. I work in UNIX/Linux environments. It's my habit to record my scripts. A simple example: #!/bin/bash CMD="psql -d test \"select * from t1\"" echo $CMD >> my_log eval $CMD | while read x; do do_something_with_x done In this example * expands to all files in the current working directory. I was attempting to get around this by enumerating the table attributes.
On Mon, 22 Jan 2007, Richard Ray wrote: > On Mon, 22 Jan 2007, Jeff Frost wrote: > >> So why are you avoiding "SELECT * FROM t1;" ? >> > I was affeared that if I brought my total ignorance to light I would be band > from the list but here goes. > I work in UNIX/Linux environments. > It's my habit to record my scripts. > A simple example: > > #!/bin/bash > CMD="psql -d test \"select * from t1\"" > echo $CMD >> my_log > eval $CMD | > while read x; do > do_something_with_x > done > > In this example * expands to all files in the current working directory. > I was attempting to get around this by enumerating the table attributes. Oh! Why didn't you just say that in the first place. You just need quotes. Try this: psql -c 'select * from t1' test or psql -c "select * from t1" test or echo "select * from t1" | psql test -- Jeff Frost, Owner <jeff@frostconsultingllc.com> Frost Consulting, LLC http://www.frostconsultingllc.com/ Phone: 650-780-7908 FAX: 650-649-1954
On Tue, 23 Jan 2007, Josh Williams wrote: > From: Jeff Frost <jeff@frostconsultingllc.com> >> On Mon, 22 Jan 2007, Richard Ray wrote: > ... > > That's definitely part of it. I'm assuming the above is an abridged example > and the OP is doing something dynamic with the query. The real trouble is > Bash likes to expand the asterisk into a list of every file in the current > directory when you try to push the command through a variable. So it's just > a matter of finding a way to escape the * character to keep Bash from > globbing, which unfortunately right now is escaping me (no pun intended.) Oh...you just need to put quotes around the variable like this: #!/bin/bash CMD="psql -c 'select * from products;' jefftest" echo "$CMD" >> my_log eval "$CMD" | while read x; do echo $x done discord:~ $ /tmp/test.sh productid | name | price -----------+-----------+------- 1 | Notepad | 1.99 3 | Legal Pad | 2.99 (2 rows) discord:~ $ cat my_log psql -c 'select * from products;' jefftest At any rate, if that's still a problem, you can turn off globbing, do what needs doing, then turn on globbing like the following: #!/bin/bash # # Turn off globbing # set -o noglob # ... # Do your * laden work here # ... # Turn on globbing set +o noglob > > Two reasonable workarounds come to mind: > 1. Turn off Bash's pathname expansion: #!/bin/bash -f > This will of course disable it script-wide, and thus will break any place you actually are trying to use this feature,if at all. > > 2. Don't put an * in the variable. > If all you're really doing is replacing the table name then only stick that into a variable, say tablename, and directlyexecute the rest: > psql -d test -c "SELECT * FROM $tablename" | while etc > Worst case, you'll end up with a messy $leftside and $rightside variable set. > > To answer the original question, the field must be hard coded either as a list or that perhaps over-used(?) asterisk. If you really need to pull and use that from the table definition you'll need two round trips to the server. -- Jeff Frost, Owner <jeff@frostconsultingllc.com> Frost Consulting, LLC http://www.frostconsultingllc.com/ Phone: 650-780-7908 FAX: 650-649-1954
From: Jeff Frost <jeff@frostconsultingllc.com> > On Mon, 22 Jan 2007, Richard Ray wrote: ... > > #!/bin/bash > > CMD="psql -d test \"select * from t1\"" > > echo $CMD >> my_log > > eval $CMD | > > while read x; do > > do_something_with_x > > done > > > > In this example * expands to all files in the current working directory. > > I was attempting to get around this by enumerating the table attributes. > > Oh! Why didn't you just say that in the first place. You just need quotes. ... That's definitely part of it. I'm assuming the above is an abridged example and the OP is doing something dynamic with thequery. The real trouble is Bash likes to expand the asterisk into a list of every file in the current directory whenyou try to push the command through a variable. So it's just a matter of finding a way to escape the * character tokeep Bash from globbing, which unfortunately right now is escaping me (no pun intended.) Two reasonable workarounds come to mind: 1. Turn off Bash's pathname expansion: #!/bin/bash -f This will of course disable it script-wide, and thus will break any place you actually are trying to use this feature, ifat all. 2. Don't put an * in the variable. If all you're really doing is replacing the table name then only stick that into a variable, say tablename, and directlyexecute the rest: psql -d test -c "SELECT * FROM $tablename" | while etc Worst case, you'll end up with a messy $leftside and $rightside variable set. To answer the original question, the field must be hard coded either as a list or that perhaps over-used(?) asterisk. Ifyou really need to pull and use that from the table definition you'll need two round trips to the server. Best of luck,- Josh Williams
Jeff Frost wrote: > On Tue, 23 Jan 2007, Josh Williams wrote: > >> From: Jeff Frost <jeff@frostconsultingllc.com> >>> On Mon, 22 Jan 2007, Richard Ray wrote: >> ... >> >> That's definitely part of it. I'm assuming the above is an abridged >> example and the OP is doing something dynamic with the query. The >> real trouble is Bash likes to expand the asterisk into a list of every >> file in the current directory when you try to push the command through >> a variable. So it's just a matter of finding a way to escape the * Oh, good grief Charlie Brown: backslash it: \* so:echo select \* from whatsit | psql test >> character to keep Bash from globbing, which unfortunately right now is >> escaping me (no pun intended.) -- Off-list replies unwelcome
John Summerfield wrote: > Jeff Frost wrote: >> On Tue, 23 Jan 2007, Josh Williams wrote: >> >>> From: Jeff Frost <jeff@frostconsultingllc.com> >>>> On Mon, 22 Jan 2007, Richard Ray wrote: >>> ... >>> >>> That's definitely part of it. I'm assuming the above is an abridged >>> example and the OP is doing something dynamic with the query. The >>> real trouble is Bash likes to expand the asterisk into a list of >>> every file in the current directory when you try to push the command >>> through a variable. So it's just a matter of finding a way to escape >>> the * > > Oh, good grief Charlie Brown: > > backslash it: > \* > > so: > echo select \* from whatsit | psql test Oh echo select \* from whatsit \; | psql test >