Minor systax error but not able to resolve it...

Started by dipti shahabout 16 years ago6 messagesgeneral
Jump to latest
#1dipti shah
shahdipti1980@gmail.com

Hi, could anyone please help me to sort out below error. I have spent lot of
time but couldn't resolved it.

mydb=> CREATE OR REPLACE FUNCTION test_create()
RETURNS void AS
$BODY$
$cmd = "CREATE TABLE test-table(col varchar not null);";
spi_exec_query("CREATE OR REPLACE FUNCTION my_tmp_func() RETURNS void AS
$cmd LANGUAGE sql;");
$BODY$
LANGUAGE 'plperl';
CREATE FUNCTION
mydb=> SELECT test_create();
ERROR: error from Perl function "test_create": syntax error at or near
"CREATE" at line 3.
mydb=>

I want to create function from my stored procedure in any language.

Thanks,
Dipti

#2Richard Huxton
dev@archonet.com
In reply to: dipti shah (#1)
Re: Minor systax error but not able to resolve it...

On 23/02/10 11:25, dipti shah wrote:

Hi, could anyone please help me to sort out below error. I have spent lot of
time but couldn't resolved it.

ERROR: error from Perl function "test_create": syntax error at or near
"CREATE" at line 3.

spi_exec_query("CREATE OR REPLACE FUNCTION my_tmp_func() RETURNS void AS
$cmd LANGUAGE sql;");

You're interpolating $cmd here but not quoting it, so you end up with:
... RETURNS void AS CREATE TABLE test-table...
whereas you want:
... RETURNS void AS 'CREATE TABLE test-table...'...

Probably best to use dollar-quoting: $TMP$ or similar, but don't forget
to escapt the $ or perl will think it's a variable.

--
Richard Huxton
Archonet Ltd

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Richard Huxton (#2)
Re: Minor systax error but not able to resolve it...

Richard Huxton <dev@archonet.com> writes:

You're interpolating $cmd here but not quoting it, so you end up with:
... RETURNS void AS CREATE TABLE test-table...
whereas you want:
... RETURNS void AS 'CREATE TABLE test-table...'...

Probably best to use dollar-quoting: $TMP$ or similar, but don't forget
to escapt the $ or perl will think it's a variable.

Dollar-quoting is really dangerous when you're trying to quote
nonconstant text. I'd suggest passing the string through
quote_literal() instead.

regards, tom lane

#4dipti shah
shahdipti1980@gmail.com
In reply to: Tom Lane (#3)
Re: Minor systax error but not able to resolve it...

Thanks. Putting $cmd in single quote resolve the error but it generated
other error. :(

mydb=> CREATE OR REPLACE FUNCTION test_create()
RETURNS void AS
$BODY$
$cmd = "CREATE TABLE testtable(col varchar not null);";
spi_exec_query("CREATE OR REPLACE FUNCTION myfunc() RETURNS void AS
'$cmd'LANGUAGE plperl;");
spi_exec_query("SELECT myfunc();");
$BODY$
LANGUAGE 'plperl';
CREATE FUNCTION
mydb=> SELECT test_create();
ERROR: error from Perl function "test_create": error from Perl function
"myfunc": Can't locate object method "col" via package "varchar" (perhaps
you forgot to load "varchar"?) at line 1. at line 3.

this is also related to quoting?

On Tue, Feb 23, 2010 at 9:23 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Show quoted text

Richard Huxton <dev@archonet.com> writes:

You're interpolating $cmd here but not quoting it, so you end up with:
... RETURNS void AS CREATE TABLE test-table...
whereas you want:
... RETURNS void AS 'CREATE TABLE test-table...'...

Probably best to use dollar-quoting: $TMP$ or similar, but don't forget
to escapt the $ or perl will think it's a variable.

Dollar-quoting is really dangerous when you're trying to quote
nonconstant text. I'd suggest passing the string through
quote_literal() instead.

regards, tom lane

#5Richard Huxton
dev@archonet.com
In reply to: dipti shah (#4)
Re: Minor systax error but not able to resolve it...

On 23/02/10 17:15, dipti shah wrote:

Thanks. Putting $cmd in single quote resolve the error but it generated
other error. :(

mydb=> CREATE OR REPLACE FUNCTION test_create()
RETURNS void AS
$BODY$
$cmd = "CREATE TABLE testtable(col varchar not null);";
spi_exec_query("CREATE OR REPLACE FUNCTION myfunc() RETURNS void AS
'$cmd'LANGUAGE plperl;");
spi_exec_query("SELECT myfunc();");
$BODY$
LANGUAGE 'plperl';
CREATE FUNCTION
mydb=> SELECT test_create();
ERROR: error from Perl function "test_create": error from Perl function
"myfunc": Can't locate object method "col" via package "varchar" (perhaps
you forgot to load "varchar"?) at line 1. at line 3.

The function "myfunc" you are trying to create as plperl isn't perl.
Either give it some perl or try "LANGUAGE SQL".

--
Richard Huxton
Archonet Ltd

#6dipti shah
shahdipti1980@gmail.com
In reply to: Richard Huxton (#5)
Re: Minor systax error but not able to resolve it...

Wonderful! Thanks.

On Wed, Feb 24, 2010 at 2:03 AM, Richard Huxton <dev@archonet.com> wrote:

Show quoted text

On 23/02/10 17:15, dipti shah wrote:

Thanks. Putting $cmd in single quote resolve the error but it generated
other error. :(

mydb=> CREATE OR REPLACE FUNCTION test_create()
RETURNS void AS
$BODY$
$cmd = "CREATE TABLE testtable(col varchar not null);";
spi_exec_query("CREATE OR REPLACE FUNCTION myfunc() RETURNS void AS
'$cmd'LANGUAGE plperl;");
spi_exec_query("SELECT myfunc();");
$BODY$
LANGUAGE 'plperl';
CREATE FUNCTION
mydb=> SELECT test_create();
ERROR: error from Perl function "test_create": error from Perl function
"myfunc": Can't locate object method "col" via package "varchar" (perhaps
you forgot to load "varchar"?) at line 1. at line 3.

The function "myfunc" you are trying to create as plperl isn't perl. Either
give it some perl or try "LANGUAGE SQL".

--
Richard Huxton
Archonet Ltd