PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old April 4th, 2008, 12:36 PM
kevind0718 kevind0718 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 2 kevind0718 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 16 sec
Reputation Power: 0
Error on INSERT INTO

hello:

I am getting an error on a simple INSERT INTO within a function using POSTGRES 8.2. I have tried several variations in order to get some thing to work. But no success.

Please be kind and take a look at the following

CREATE OR REPLACE FUNCTION bmtrades(fundinst_bm integer, fundinst_ma integer, tdate date)
RETURNS character AS
$BODY$

DECLARE

r_str char(8);

BEGIN
select rand_str() into r_str;

insert into temp_bm(unqkey, secid_sort, bm_fundinstance, bm_pbaccount, tdate, bm_secid, bm_secdesc, bm_qty, bm_price, bm_buysell, bm_isocurr)
select r_str, cast(t.securitykey as text)||'BM', fundinstancekey, pbaccount, transactiondate , t.securitykey, s.securitydesc, qty, price, buy_sell, currisocode
from transaction t, security s
where t.fundinstancekey = fundinst_bm and t.transactiondate = tdate and
t.securitykey = s.securitykey;

--(unqkey, secid_sort, bm_fundinstance, bm_pbaccount, tdate, bm_secid, bm_secdesc, bm_qty, bm_price, bm_buysell, bm_isocurr)

insert into temp_bm
select r_str, cast(t.securitykey as text)||'MA', fundinstancekey, pbaccount, transactiondate , t.securitykey, s.securitydesc, qty, price, buy_sell, currisocode
from transaction t, security s
where t.fundinstancekey = fundinst_ma and t.transactiondate = tdate and
t.securitykey = s.securitykey;



return r_str;
END


$BODY$
LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION bmtrades(bm_finst integer, ma_finst integer, tdate date) OWNER TO postgres;

when I complie I get the following:


ERROR: syntax error at or near "$1"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "bmtrades" near line 13

Tables are defined as follows:

CREATE TABLE temp_bm
(
secid_sort character(15),
bm_secid integer,
bm_secdesc character(25),
bm_qty integer,
bm_price numeric(19,6),
bm_ratio numeric(10,6),
ma_secid integer,
ma_qty integer,
ma_price numeric(19,6),
unqkey character(8),
bm_fundinstance integer,
ma_fundinstance integer,
tdate date,
bm_pbaccount character(25),
ma_pbaccount character(25),
bm_buysell character(1),
ma_buysell character(1),
bm_isocurr character(3),
ma_isocurr character(3)
)
WITHOUT OIDS;
ALTER TABLE temp_bm OWNER TO postgres;



CREATE TABLE "transaction"
(
transactionkey serial NOT NULL,
securitykey integer,
fundinstancekey integer,
qty integer,
price numeric(20,6),
transactiondate date,
entrydate date,
sourcekey integer,
taskrunkey integer NOT NULL,
buy_sell character(1),
currisocode character(3),
pbaccount character(25),
CONSTRAINT pk_transaction PRIMARY KEY (transactionkey),
CONSTRAINT fk_transaction_fundinstance FOREIGN KEY (fundinstancekey)
REFERENCES fundinstance (fundinstancekey) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_transaction_securitykey FOREIGN KEY (securitykey)
REFERENCES "security" (securitykey) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_transaction_source FOREIGN KEY (sourcekey)
REFERENCES source (sourcekey) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_transaction_taskrun FOREIGN KEY (taskrunkey)
REFERENCES taskrun (taskrunkey) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITHOUT OIDS;
ALTER TABLE "transaction" OWNER TO postgres;



Thanks for your assistance.

Best regards

kd

Reply With Quote
  #2  
Old April 4th, 2008, 01:12 PM
pgFrank's Avatar
pgFrank pgFrank is offline
So help me Codd
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Brazil
Posts: 129 pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 45 m 18 sec
Reputation Power: 18
The name tdate is used as a parameter and as a column-name as well. That explains the error-message.

By the way, the function is hard to read with all SQL-statements in lowercase... I prefer SQL in UPPERCASE and my table- and column-names in lowercase. Makes it easier to understand and debug.

Reply With Quote
  #3  
Old April 4th, 2008, 01:30 PM
kevind0718 kevind0718 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 2 kevind0718 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 16 sec
Reputation Power: 0
Quote:
Originally Posted by pgFrank
The name tdate is used as a parameter and as a column-name as well. That explains the error-message.

By the way, the function is hard to read with all SQL-statements in lowercase... I prefer SQL in UPPERCASE and my table- and column-names in lowercase. Makes it easier to understand and debug.


Thanks for being so observant. A second pair of eyes always helps.

But I wonder why it was bauking about $1.

kd

Reply With Quote
  #4  
Old April 4th, 2008, 01:39 PM
pgFrank's Avatar
pgFrank pgFrank is offline
So help me Codd
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Brazil
Posts: 129 pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level)pgFrank User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 45 m 18 sec
Reputation Power: 18
I agree, a little bit weird it's complaining about the first parameter ($1) when you have a problem with the third one. $3 would make sense.

This was the message in pgAdmin3:
Quote:
ERROR: syntax error at or near "$1"
LINE 1: ...qkey, secid_sort, bm_fundinstance, bm_pbaccount, $1 , bm_se...
^
QUERY: INSERT INTO temp_bm( unqkey, secid_sort, bm_fundinstance, bm_pbaccount, $1 , bm_secid, bm_secdesc, bm_qty, bm_price, bm_buysell, bm_isocurr ) SELECT $2 , CAST(t.securitykey AS text) || 'BM', fundinstancekey, pbaccount, transactiondate , t.securitykey, s.securitydesc, qty, price, buy_sell, currisocode FROM transaction t, security s WHERE t.fundinstancekey = $3 AND t.transactiondate = $1 AND t.securitykey = s.securitykey
CONTEXT: SQL statement in PL/PgSQL function "bmtrades" near line 42

********** Error **********

ERROR: syntax error at or near "$1"
SQL status:42601
Context:SQL statement in PL/PgSQL function "bmtrades" near line 42

That's how I found the bug, the column-name between bm_pbaccount and bm_secid is replaced with a parameter. And that couldn't be right.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Error on INSERT INTO


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway