1

I'm very familiar with MSSQL but I need to work with some postgres stuff today. The end goal is to return the new id of a table for use elsewhere but since I'm unfamiliar with the syntax of postgress, I seem to be having some trouble.

Problem 1:

in MSSQL I would:

delcare @test_number int
select @test_number = 42
print @test_number

in Postgres I'm trying:

test_number integer;
test_number := 42
print test_number

but it doesnt like the first line:

ERROR:  syntax error at or near "test_number"
LINE 1: test_number integer;

If I can figure this out, I think I've got my real problem solved which would be to do something like:

Problem 2:

in MSSQL I would:

declare @new_id int
insert into some_table (data1. data2)
select @new_id = @@identity
-- now use @new_id for w/e you want

in Postgres I'm trying:

new__id integer;
new_id := insert into some_table (data1, data2) returning id;
-- now use new_id for w/e you want
aarona
  • 35,986
  • 41
  • 138
  • 186

1 Answers1

1

A quick search on Google gives the following answer.

How do you use variables in a simple PostgreSQL script?

It looks like you basically declare an anonymous inline pgsql function in your query. Also looks like it's new to Postgres 9.

Community
  • 1
  • 1
digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • what did you use a for search criteria? Also I googled postgres variables and found postgres docs that looked nothing like the syntax used in that link (however this did work, thanks!) – aarona May 14 '13 at 19:47
  • my search terms in Google were `postgres declare variable query`. Glad it worked. – digitaljoel May 14 '13 at 20:35