[SQL] error on line 1 trying to execute a script using psql
I am attempting to generate a temporary function to execute some dml with the following script: create or replace function setuplicense() returns integer as $$ declare hwcustid integer := 0; retvalinteger := 0; begin insert into license.customer ( customer_id ) values ( hwcustid ); commit; return retval; end; $$ LANGUAGE plpgsql; select setuplicense(); When running this with psql I get the following: Error: syntax error at or near "create" Does anyone have a suggestion as to what is causing this? Thanks
[SQL] running scripts like oracle sqlplus
I have been trying to figure out how I can run a pgsql script like I can run a plsql script with oracle's sqlplus. Here is a sample script file for what I want to run: declare sysuserid integer := 0; hwcustid integer := 0; begin select nextval( 'user_seq' ) into sysuserid; select nextval( 'customer_seq' ) into hwcustid; insert into user ( user_id, customer_id, create_user, update_user ) values ( sysuserid, hwcustid, sysuserid, sysuserid ); insert into customer ( customer_id, create_user, update_user ) values ( hwcustid, sysuserid, sysuserid ); commit; end; I try to run the script in psql and thru pgadmin and cannot seem to make them work. I do not want to turn it into a function. I just want it to execute the block in a fashion similar to Oracle'sqlplus running @scriptfile.sql. Thanks
Re: [SQL] running scripts like oracle sqlplus
On Fri, Jun 18, 2010 at 04:24:18PM -0600, Steven Dahlin wrote: > I have been trying to figure out how I can run a pgsql script like I can run > a plsql script with oracle's sqlplus. Here is a sample script file for what > I want to run: > > declare > sysuserid integer := 0; > hwcustid integer := 0; > begin > > select nextval( 'user_seq' ) into sysuserid; > select nextval( 'customer_seq' ) into hwcustid; > > insert into user > ( > user_id, > customer_id, > create_user, > update_user > ) > values > ( > sysuserid, > hwcustid, > sysuserid, > sysuserid > ); > > insert into customer > ( > customer_id, > create_user, > update_user > ) > values > ( > hwcustid, > sysuserid, > sysuserid > ); > > commit; > > end; > > I try to run the script in psql and thru pgadmin and cannot seem to make > them work. I do not want to turn it into a function. I just want it to > execute the block in a fashion similar to Oracle'sqlplus running > @scriptfile.sql. > > Thanks I think you will need to wait for version 9.0 and anonymous functions using DO. Cheers, Ken -- Sent via pgsql-sql mailing list ([email protected]) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql
Re: [SQL] running scripts like oracle sqlplus
Have you tried nextval & currval? http://www.postgresql.org/docs/8.4/interactive/functions-sequence.html Something like this: begin insert into user ( user_id, customer_id, create_user, update_user ) values ( nextval(user_seq), nextval(customer_seq), currval(user_seq), currval(user_seq) ); insert into customer ( customer_id, create_user, update_user ) values ( currval(customer_seq), currval(user_seq), currval(user_seq) ); commit; end; On Fri, Jun 18, 2010 at 6:24 PM, Steven Dahlin wrote: > I have been trying to figure out how I can run a pgsql script like I can > run a plsql script with oracle's sqlplus. Here is a sample script file for > what I want to run: > > declare > sysuserid integer := 0; > hwcustid integer := 0; > begin > > select nextval( 'user_seq' ) into sysuserid; > select nextval( 'customer_seq' ) into hwcustid; > > insert into user > ( > user_id, > customer_id, > create_user, > update_user > ) > values > ( > sysuserid, > hwcustid, > sysuserid, > sysuserid > ); > > insert into customer > ( > customer_id, > create_user, > update_user > ) > values > ( > hwcustid, > sysuserid, > sysuserid > ); > > commit; > > end; > > I try to run the script in psql and thru pgadmin and cannot seem to make > them work. I do not want to turn it into a function. I just want it to > execute the block in a fashion similar to Oracle'sqlplus running > @scriptfile.sql. > > Thanks >
Re: [SQL] error on line 1 trying to execute a script using psql
Steven Dahlin wrote:
> I am attempting to generate a temporary function to execute some dml with
> the following script:
> create or replace function setuplicense() returns integer as $$
> declare
> hwcustid integer := 0;
> retvalinteger := 0;
> begin
> insert into license.customer
> ( customer_id ) values ( hwcustid );
> commit;
> return retval;
> end;
> $$
> LANGUAGE plpgsql;
> select setuplicense();
> When running this with psql I get the following:
> Error: syntax error at or near "create"
> Does anyone have a suggestion as to what is causing this?
Your editor prepends the file with a byte-order mark ("BOM")
that PostgreSQL chokes on (bug #5398). This will be fixed in
9.0 (cf.
http://developer.postgresql.org/pgdocs/postgres/release-9-0.html#AEN99331>);
until then you either have to configure your editor not to
save the BOM or chop off the first three bytes yourself
(with tail, sed, Perl & Co.).
Tim
--
Sent via pgsql-sql mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
Re: [SQL] error on line 1 trying to execute a script using psql
In response to Tim Landscheidt :
> Steven Dahlin wrote:
>
> > I am attempting to generate a temporary function to execute some dml with
> > the following script:
>
> > create or replace function setuplicense() returns integer as $$
> > declare
> > hwcustid integer := 0;
> > retvalinteger := 0;
> > begin
> > insert into license.customer
> > ( customer_id ) values ( hwcustid );
> > commit;
> > return retval;
> > end;
> > $$
> > LANGUAGE plpgsql;
>
> > select setuplicense();
>
> > When running this with psql I get the following:
>
> > Error: syntax error at or near "create"
>
> > Does anyone have a suggestion as to what is causing this?
>
> Your editor prepends the file with a byte-order mark ("BOM")
> that PostgreSQL chokes on (bug #5398). This will be fixed in
> 9.0 (cf.
> http://developer.postgresql.org/pgdocs/postgres/release-9-0.html#AEN99331>);
> until then you either have to configure your editor not to
> save the BOM or chop off the first three bytes yourself
> (with tail, sed, Perl & Co.).
Additional error: you can't do a COMMIT inside a function.
Regards, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99
--
Sent via pgsql-sql mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
