Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 02:20 AM
Sim Zacks
Guest
 
Posts: n/a
Default sql script confusion

I am in the process of testing PostGreSQL for our sales and product
database and I am a little confused about SQL scripting and various
available languages. (My background is mostly MSSQL Server).

I am using the PGAdmin tool and I would like to write a script to
update all sequences to the current max value in the table.

Do I need to use a specific language in a function to do this or does
it work as native SQL, as it would in T-SQL?

What I would like to do is something like -
(pseudo code)
declare cursor for select relname from pg_statio_user_sequences
open cursor
fetch next into var_relname
while not cursor.eof
set var_tblname=substring(var_relname, "0 until _")
set var_fieldname=substring(var_relname,"first _ until 2nd _")
select var_maxID=max(var_fieldname) from var_tblname
ALTER SEQUENCE var_relname
RESTART WITH var_maxID+1;
fetch next into var_relname
end loop

Can dynamic statements be written in "raw sql" or do they need to be
encompassed in a language? Do all language scripts have to be
functions or can I do something like:
Start Language Processing Here
....Code
End Language Processing


Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax


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

  #2  
Old November 23rd, 2005, 02:20 AM
Richard Huxton
Guest
 
Posts: n/a
Default Re: sql script confusion

Sim Zacks wrote:[color=blue]
> Do I need to use a specific language in a function to do this or does
> it work as native SQL, as it would in T-SQL?[/color]

You need to use a specific language.
[color=blue]
> What I would like to do is something like -
> (pseudo code)
> declare cursor for select relname from pg_statio_user_sequences
> open cursor
> fetch next into var_relname
> while not cursor.eof
> set var_tblname=substring(var_relname, "0 until _")
> set var_fieldname=substring(var_relname,"first _ until 2nd _")
> select var_maxID=max(var_fieldname) from var_tblname
> ALTER SEQUENCE var_relname
> RESTART WITH var_maxID+1;
> fetch next into var_relname
> end loop[/color]

For this plpgsql might be a good choice. Think sql with a few
loop/control structures and variables.
[color=blue]
> Can dynamic statements be written in "raw sql" or do they need to be
> encompassed in a language? Do all language scripts have to be
> functions or can I do something like:
> Start Language Processing Here
> ...Code
> End Language Processing[/color]

You need to create a function and then call it. You can create dynamic
SQL as a string and then EXECUTE it with plpgsql. The other languages
offer various ways too.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 3: 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles