473,700 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

psql - user defined SQL variables

I am new to PostgreSQL and am porting some scripts written for MySQL over
to psql. There is one MySQL feature which I cannot find in psql - user
defined SQL variables. In MySQL I can use these to hold the result
(numeric, string or null) of a select: e.g.
select @count = count(*) from mytable;
The @count variable now holds the result of that select, and I can use it
in where clauses, updates, inserts etc.
I checked out the \set psql variable but I haven't found a way to tweak it
to give me the result of a sql statement - is there any way to do that (I'm
using PostgreSQL version 7.4.3)?
Thanks in advance for any suggestions,
Grainne.

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
6 12770
Hi,

Am Mi, den 18.08.2004 schrieb Grainne Reilly um 6:22:
I am new to PostgreSQL and am porting some scripts written for MySQL over
to psql. There is one MySQL feature which I cannot find in psql - user
defined SQL variables. In MySQL I can use these to hold the result
(numeric, string or null) of a select: e.g.
select @count = count(*) from mytable;
The @count variable now holds the result of that select, and I can use it
in where clauses, updates, inserts etc.
I checked out the \set psql variable but I haven't found a way to tweak it
to give me the result of a sql statement - is there any way to do that (I'm
using PostgreSQL version 7.4.3)?
Thanks in advance for any suggestions,
Grainne.


Postgres supports subselects (for update and stuff) and you can insert
like that:

INSERT INTO foo (a,b,c) SELECT a,b,c FROM ...

If thats not enough, you have a bounch of pl/*
languages to write sophisticated procedures
in the database where you have variables for
single values as well as recordsets.

Regards
Tino Wildenhain
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
On Wed, Aug 18, 2004 at 12:22:27AM -0400, Grainne Reilly wrote:
I am new to PostgreSQL and am porting some scripts written for MySQL
over to psql.
The least work you'll do here is to refactor your app entirely.

PostgreSQL will just plain handle about 3/4 of what you used to have
to deal with up in middleware land. It also supports all kinds of
wizardry that will astound you as you run across it. :)
There is one MySQL feature which I cannot find in psql - user
defined SQL variables. In MySQL I can use these to hold the result
(numeric, string or null) of a select: e.g. select @count =
count(*) from mytable;
This is a MySQLism to work around their lack of subselects. But if
that turns out not to be enough, you have a broad choice of procedural
languages, from the ADA-like PL/PgSQL to PL/Perl, PL/Python and (soon)
PL/PHP.
Thanks in advance for any suggestions,


See about redoing your app. It will be less work than trying to graft
the MySQL design onto PostgreSQL, which is about like trying to hook
an ox cart to a jet engine :)

Cheers,
D
--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #3
Thanks for the response - pity PostgreSQL doesn't have this. These are a
bunch of quick and dirty data population scripts which I was hoping to
avoid rewriting to use temporary tables, subselects etc.
It is a useful feature for these kind of scripts (Sybase has it and it has
always had subselects) - and in Oracle I can use an anonymous PL/SQL block
to declare and use variables directly in sqlplus.
Are there any plans for PostgreSQL to support anonymous PL/pgSQL blocks
directly in psql - now that would be very nice!
Thanks again.
Grainne.
At 01:48 AM 8/18/2004, David Fetter wrote:
On Wed, Aug 18, 2004 at 12:22:27AM -0400, Grainne Reilly wrote:
I am new to PostgreSQL and am porting some scripts written for MySQL
over to psql.


The least work you'll do here is to refactor your app entirely.

PostgreSQL will just plain handle about 3/4 of what you used to have
to deal with up in middleware land. It also supports all kinds of
wizardry that will astound you as you run across it. :)
There is one MySQL feature which I cannot find in psql - user
defined SQL variables. In MySQL I can use these to hold the result
(numeric, string or null) of a select: e.g. select @count =
count(*) from mytable;


This is a MySQLism to work around their lack of subselects. But if
that turns out not to be enough, you have a broad choice of procedural
languages, from the ADA-like PL/PgSQL to PL/Perl, PL/Python and (soon)
PL/PHP.
Thanks in advance for any suggestions,


See about redoing your app. It will be less work than trying to graft
the MySQL design onto PostgreSQL, which is about like trying to hook
an ox cart to a jet engine :)

Cheers,
D
--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4
What do you mean varables do not exists?
Ever heard of SELECT INTO?
Grainne Reilly wrote:
Thanks for the response - pity PostgreSQL doesn't have this. These are a
bunch of quick and dirty data population scripts which I was hoping to
avoid rewriting to use temporary tables, subselects etc.
It is a useful feature for these kind of scripts (Sybase has it and it
has always had subselects) - and in Oracle I can use an anonymous PL/SQL
block to declare and use variables directly in sqlplus.
Are there any plans for PostgreSQL to support anonymous PL/pgSQL blocks
directly in psql - now that would be very nice!
Thanks again.
Grainne.
At 01:48 AM 8/18/2004, David Fetter wrote:
On Wed, Aug 18, 2004 at 12:22:27AM -0400, Grainne Reilly wrote:
> I am new to PostgreSQL and am porting some scripts written for MySQL
> over to psql.


The least work you'll do here is to refactor your app entirely.

PostgreSQL will just plain handle about 3/4 of what you used to have
to deal with up in middleware land. It also supports all kinds of
wizardry that will astound you as you run across it. :)
> There is one MySQL feature which I cannot find in psql - user
> defined SQL variables. In MySQL I can use these to hold the result
> (numeric, string or null) of a select: e.g. select @count =
> count(*) from mytable;


This is a MySQLism to work around their lack of subselects. But if
that turns out not to be enough, you have a broad choice of procedural
languages, from the ADA-like PL/PgSQL to PL/Perl, PL/Python and (soon)
PL/PHP.
> Thanks in advance for any suggestions,


See about redoing your app. It will be less work than trying to graft
the MySQL design onto PostgreSQL, which is about like trying to hook
an ox cart to a jet engine :)

Cheers,
D
--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if
your
joining column's datatypes do not match



---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5
On Wed, Aug 18, 2004 at 04:45:57PM -0400, Jean-Luc Lachance wrote:
What do you mean varables do not exists? Ever heard of SELECT INTO?
Yes, and for variable assignment, it works inside stored procedures,
but not elsewhere.

Oh, and please to be nice to the people who ask questions. :)

Cheers,
D
Grainne Reilly wrote:
Thanks for the response - pity PostgreSQL doesn't have this. These are a
bunch of quick and dirty data population scripts which I was hoping to
avoid rewriting to use temporary tables, subselects etc.
It is a useful feature for these kind of scripts (Sybase has it and it
has always had subselects) - and in Oracle I can use an anonymous PL/SQL
block to declare and use variables directly in sqlplus.
Are there any plans for PostgreSQL to support anonymous PL/pgSQL blocks
directly in psql - now that would be very nice!
Thanks again.
Grainne.
At 01:48 AM 8/18/2004, David Fetter wrote:
On Wed, Aug 18, 2004 at 12:22:27AM -0400, Grainne Reilly wrote:
I am new to PostgreSQL and am porting some scripts written for MySQL
over to psql.

The least work you'll do here is to refactor your app entirely.

PostgreSQL will just plain handle about 3/4 of what you used to have
to deal with up in middleware land. It also supports all kinds of
wizardry that will astound you as you run across it. :)

There is one MySQL feature which I cannot find in psql - user
defined SQL variables. In MySQL I can use these to hold the result
(numeric, string or null) of a select: e.g. select @count =
count(*) from mytable;

This is a MySQLism to work around their lack of subselects. But if
that turns out not to be enough, you have a broad choice of procedural
languages, from the ADA-like PL/PgSQL to PL/Perl, PL/Python and (soon)
PL/PHP.

Thanks in advance for any suggestions,

See about redoing your app. It will be less work than trying to graft
the MySQL design onto PostgreSQL, which is about like trying to hook
an ox cart to a jet engine :)

Cheers,
D
--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if
your
joining column's datatypes do not match



---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


--
David Fetter da***@fetter.or g http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #6
Can you use a temporary table for this?

You could wrap it in a stored proc (say SQL language) for getting and
setting varaibles.

Best Wishes,
Chris Travers
Metatron Technology COnsulting


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2036
by: Rob Sell | last post by:
Greetings all, Yesterday I upgraded from 7.3 to 7.4 now psql doesn't work! I get the following error. psql: relocation error: psql: undefined symbol: get_progname Any ideas out there? Rob
7
37770
by: Willem Herremans | last post by:
I am developing a client application for postgreSQL in Tcl/Tk (see http://gborg.postgresql.org/project/pfm ). It mainly uses PgTcl or pgintcl. I don't have any problems with those, but I am also trying to call psql from my application for SQL statements typed directly by the user. I have used the Tcl command set psqlChannel
4
2150
by: Brendan Jurd | last post by:
Hello all, I just wanted to pass on some information about compatibility between the psql client and the postgres server. On a particular network, my workstation and the server are both debian boxes, but the workstation is on a more frequent upgrade schedule. The server is still running postgres 7.3.4, but my workstation has been upgraded with the latest (7.4.1) postgres client tools.
6
1723
by: Ben | last post by:
I'm designing a fairly involved database system. As part fo the process, I use the \i command a great deal. I set up fairly involved queries, sometimes simply for the purpose of shortening column names so the output is reasonable. For example: SELECT longname AS abbr,othername as "V" FROM table WHERE how; ....a bunch of these can result in a single-line output on the console, which is a lot easier to deal with than a dump of the...
1
1657
by: Leo Leo | last post by:
Hi! How can I interpret a variable in psql, when the variable has to be quoted? for example: \set myVar myValue \echo :myVar The Result ist then "myValue" ==> ok
15
2505
by: Dino Vliet | last post by:
Hi folks, probably this is a question you've heard so many times but I wasn't able to find a solution to it. I'm using a shell script to create a textfile for me. It looks like #!/usr/local/bin/bash psql -c "select foo from bar;" -d database1 -t psql -c "\q" -d database1 exit 0
1
2188
by: Josué Maldonado | last post by:
Hello List, I'm having this issue with beta 8.0 C:\pgsql\bin>pg_dump -U postgres farmacia > xfar.sql Password: C:\pgsql\bin>psql -U postgres farmacia2 < xfar.sql Password: psql: FATAL: Password authentication failed for user "postgres" C:\pgsql\bin>psql -U postgres -W farmacia2 < xfar.sql
6
6999
by: Ron St-Pierre | last post by:
Is there a way 'within' psql to suppress output? One of our cron scripts calls a sql file which contains various database commands (ALTER TABLEs, UPDATEs, etc) and various user-defined functions. So within this sql file there are various SELECT * FROM myFunction(); which sends output to the user from cron. I can't see anyway to suppress this from the psql docs and I don't believe that I can suppress it from cron either (I'll do some...
33
14013
by: John Sidney-Woollett | last post by:
With the advent of postgres v8, would it be possible to change the default behaviour of psql from AUTOCOMMIT=ON to AUTOCOMMIT=OFF ? Although this might break backward compatibility, it might be acceptable on the basis that v8 is such a major release. Also adding a new command line parameter to control the AUTOCOMMIT setting for those users that will experience broken scripts executed (especially using the -c command) might help ease...
0
8712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9203
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8952
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4395
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.