Solved

Convert SQL Server stored procedure to Postgres

Posted on 2007-04-07
7
992 Views
Last Modified: 2008-09-19
Hi experts,

I would like to convert a Microsoft SQL Sever 2000 stored procedure to a postgres procedure.

The procedure inserts a null value in the Description field in the Client table, then returns the auto incrementing integer field ID.

Here's the stored procedure:

CREATE PROCEDURE GetNextClientID
@ID int OUTPUT
AS
SET NOCOUNT ON
INSERT INTO Audio ("Description")
VALUES (null)
SELECT @ID = @@IDENTITY
RETURN @ID
GO

If it's possible to input the table name and field name to enter the null value that would be fantastic, though if too hard don't worry aboutn it.

Please add your solution!
0
Comment
Question by:undyshelts
  • 4
7 Comments
 
LVL 22

Accepted Solution

by:
earth man2 earned 250 total points
ID: 18875201
create table audio ( id serial primary key, "Description" text );

create or replace function GetNextClientID() returns int as $$
declare
the_next_id int;
begin
  insert into Audio("Description") values ( null ) returning id into the_next_id;
  return the_next_id;
end;
$$ language plpgsql;

CREATE FUNCTION
treacle=> select GetNextClientID();
 getnextclientid
-----------------
               1
(1 row)

treacle=> select * from Audio;
 id | Description
----+-------------
  1 |
(1 row)
0
 
LVL 1

Author Comment

by:undyshelts
ID: 18879515
Thanks earthman2, any way to pass the table name (audio), null column (description) and PK field (ID) as variables?
0
 
LVL 22

Expert Comment

by:earth man2
ID: 18897284
I suspect that it cannot be done.

http://www.postgresql.org/docs/8.2/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

"SELECT INTO is not currently supported within EXECUTE."

0
 
LVL 22

Expert Comment

by:earth man2
ID: 18897307
But then maybe it is try.
create or replace function GetNextClientID( tablename text, colname text ) returns int as $$
declare
the_next_id int;
begin
  EXECUTE 'insert into "' || tablename||"("'|| colname || '") values ( null ) returning id' into the_next_id;
  return the_next_id;
end;
$$ language plpgsql;

0
 
LVL 22

Expert Comment

by:earth man2
ID: 18897316
create or replace function GetNextClientID( tablename text, colname text ) returns int as $$
declare
the_next_id int;
begin
  EXECUTE 'insert into "' || tablename||'"("'|| colname || '") values ( null ) returning id' into the_next_id;
  return the_next_id;
end;
$$ language plpgsql;


0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to query date ranges with SQL 6 38
Query 14 54
Need help debbuging stored procedure 21 34
Update data using formula 22 21
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now