Re: Quick Pg/PLSQL question

Started by Martijn van Oosterhoutover 23 years ago2 messagesgeneral
Jump to latest
#1Martijn van Oosterhout
kleptog@svana.org

On Fri, Sep 27, 2002 at 10:28:57PM -0500, Matt Wagner wrote:

INSERT INTO transaction_summary VALUES (61, 1, "now", 0, 0, 0, 0);

Very simple function, but I continue to receive errors, because of the "now"
part. However, in the Programmer's Manual, it specifically uses "now" with
double quotes in a couple examples. When executing the function, I continue
receiving the following errors:

Simple error. The manual uses two quote's, not double quotes (ie, '' vs ")
which probably look the same in many fonts.

Two quotes counts as an escaped quote whereas a double quote make postgresql
think it's an identifier, which doesn't work.

Hope tihs helps,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

There are 10 kinds of people in the world, those that can do binary
arithmetic and those that can't.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Martijn van Oosterhout (#1)

"Matt Wagner" <mwagner@envex.net> writes:

CREATE OR REPLACE FUNCTION test_func() RETURNS INTEGER AS '
DECLARE
BEGIN
INSERT INTO transaction_summary VALUES (61, 1, "now", 0, 0, 0, 0);
RETURN 1;
END;
' LANGUAGE 'plpgsql';

NOTICE: Error occurred while executing PL/pgSQL function test_func
NOTICE: line 4 at SQL statement
ERROR: Attribute 'now' not found

You would get the same error if you did that INSERT by hand, because
double-quoted "now" is completely different from single-quoted 'now'
in SQL --- one is an identifier equivalent to no-quotes-at-all now,
the other is a literal constant. What you want here is a literal
constant that can be fed to the timestamp input routine.

You probably tried single-quoted 'now' already and got syntax errors
that you didn't understand. The trick here is that the function body
is itself a single-quoted string literal. To get single quotes into
the body of the function, you must either double 'em or backslash 'em.
So either of these should work:

INSERT INTO transaction_summary VALUES (61, 1, ''now'', 0, 0, 0, 0);
INSERT INTO transaction_summary VALUES (61, 1, \'now\', 0, 0, 0, 0);

This is covered in the manual, but perhaps it's not obvious till you
get bit by it...

regards, tom lane