0

我无法正确执行此命令。表的第一列是一个自动递增的整数,所以我希望在第 2 列开始输入数据。当我执行以下操作时:

PREPARE fooplan (text, smallint, smallint, text, date, timestamp with time zone) as 
INSERT INTO "table" VALUES($2, $3, $4, $5, $6, $7);
EXECUTE fooplan('Add New Record', 2, 2, 'User', '1999-Jan-08', '04:05:06');

我收到此错误:

SQL error:

ERROR:  column "category_id" is of type smallint but expression is of type text
LINE 2: insert into "MOP" values($2, $3, $4, $5, $6, $7);
                                     ^
HINT:  You will need to rewrite or cast the expression.
In statement:
prepare fooplan (text, smallint, smallint, text, date, time without time zone) as 
insert into "MOP" values($2, $3, $4, $5, $6, $7);
execute fooplan('Add New Mop', 2, 2, 'User', '1999-Jan-08', '04:05:06');

谁能帮我理解我做错了什么?

4

2 回答 2

1

后面的数字是$指参数顺序。

INSERT INTO "table" VALUES($1, $2, $3, $4, $5, $6);

除此之外,您还必须命名要插入的列:

INSERT INTO "table" (col2, col3, col4, col5, col6, col7) 
VALUES($1, $2, $3, $4, $5, $6);
于 2013-03-04T17:14:24.263 回答
1

如果您只是尝试插入值,但没有指定第一个字段值,那么您需要说明您要插入哪些列。

我不知道你的列叫什么,所以我只称它们为 column_1、column_2 等。

prepare fooplan (text, smallint, smallint, text, date, timestamp with time zone)
INSERT INTO MOP(column_2, column_3, column_4, column_5, column_6, column_7) 
VALUES ($1, $2, $3, £4, $5, $6);
于 2013-03-04T17:14:45.130 回答