Correct way to set up Variables [Was - Simple Accumulating Number Loop?]

Started by Ubence Quevedoover 20 years ago2 messagesgeneral
Jump to latest
#1Ubence Quevedo
r0d3nt@pacbell.net

What would be the correct way to set up a dynamic
variable? I did some further research on my question
below, and I still don't have a definite answer to
whether or not use \set or SET. I did read at
http://www.commandprompt.com/ppbook/x19832 about using
variables in a Function, but that seems like an awful
lot to do [or does MS SQL make it easier than
PostgreSQL?]. Any help, ideas, or clarification are
much appreciated.

-Ubence

Show quoted text
--- Ubence Quevedo <r0d3nt@pacbell.net> wrote:

Date: Thu, 29 Dec 2005 15:26:07 -0800 (PST)
From: Ubence Quevedo <r0d3nt@pacbell.net>
Subject: Simple Accumulating Number Loop?
To: pgsql-novice@postgresql.org,
pgsql-general@postgresql.org

A friend of mine has created this simple
accumulating
loop query for MS SQL 2k5 Express Edition. I am
trying to reproduce the same results with

PostgreSQL

8.1, but am not able to find much useful help on

how

to properly set up a variable of both int and

char.

The PostgreSQL documentation is great if you have

an

idea of what you are doing, but I'm still really

new

to PostgreSQL. If someone can just point out some
hints or clarifications as to wether to use the

SET

command or the psql \set command. Below is the
query
in question.

Many thanx to any that can help.

-Ubence

declare @variableint int
declare @desc char (50)
set @variableint = 0
create table counter (countid int , description
varchar(50))
while @variableint < 500
begin
set @variableint = @variableint + 1
set @desc = 'The Counter is Now' +' '+ cast
(@variableint as char(50))
insert into counter values (@variableint,@desc)
end
select * from counter

#2Tino Wildenhain
tino@wildenhain.de
In reply to: Ubence Quevedo (#1)
Re: Correct way to set up Variables [Was - Simple Accumulating

Ubence Quevedo schrieb:

What would be the correct way to set up a dynamic
variable? I did some further research on my question
below, and I still don't have a definite answer to
whether or not use \set or SET. I did read at
http://www.commandprompt.com/ppbook/x19832 about using
variables in a Function, but that seems like an awful
lot to do [or does MS SQL make it easier than
PostgreSQL?]. Any help, ideas, or clarification are
much appreciated.

Dont mix frontend and backend in your approach.
You can specify variables in whatever way your
stored function handles it (e.g. it depends on
the language you use. Examples for pl/pgsql
are in the docs.)
In Postgres you dont write arbitrary code
to the server. You write stored functions
(server side) or you write SQL statements.

Any further variable handling is up to your
host/application language.

psql, the command line client application
does not have variables as such nor any
flow control.

Regards
Tino Wildenhain