1
我已經寫了一個函數,但是它什麼都沒返回。但是,當我單獨執行該查詢時,它可以工作。你能幫助理解爲什麼會發生嗎?函數什麼也沒有返回Postgresql
SQL查詢如下:
CREATE TABLE IF NOT EXISTS char_block (
id SERIAL PRIMARY KEY,
char_block_name CHAR(64)
);
CREATE TABLE IF NOT EXISTS string_table (
id SERIAL PRIMARY KEY,
char_name CHAR(64),
char_block_id INTEGER NOT NULL,
char_description CHAR(256),
boolean_value boolean NOT NULL
);
INSERT INTO char_block VALUES(1, 'qqqqqqq:');
INSERT INTO char_block VALUES(2, 'wwwwwwwwwwww:');
INSERT INTO char_block VALUES(3, 'eeeeeeeeeeeee:');
INSERT INTO string_table VALUES(1, 'some string', 1, 'It means that blablabla...', FALSE);
INSERT INTO string_table VALUES(2, 'hm', 1, 'Too long text...', FALSE);
INSERT INTO string_table VALUES(3, 'no', 2, 'It means that blablabla...', FALSE);
INSERT INTO string_table VALUES(4, 'pls', 2, 'Too long text...', FALSE);
INSERT INTO string_table VALUES(5, 'bla', 2, 'Too long text...', FALSE);
INSERT INTO string_table VALUES(6, 'bla1', 2, 'Too long text...', FALSE);
INSERT INTO string_table VALUES(7, 'one more thing', 3, 'How to make...', TRUE);
INSERT INTO string_table VALUES(8, 'another thing', 3, 'It means...', TRUE);
CREATE OR REPLACE FUNCTION get_all_string_table()
RETURNS table(id INTEGER,
char_name CHAR(64),
char_description CHAR(256),
boolean_value boolean,
char_block_name CHAR(64)) AS
$func$
BEGIN
EXECUTE format('
SELECT
string_table.id,
string_table.char_name,
string_table.char_description,
string_table.boolean_value,
char_block.char_block_name
FROM
string_table,
char_block
WHERE
string_table.char_block_id = char_block.id
ORDER BY string_table.id;');
END
$func$ LANGUAGE plpgsql;
所以,如果我跑
SELECT
string_table.id,
string_table.char_name,
string_table.char_description,
string_table.boolean_value,
char_block.char_block_name
FROM
string_table,
char_block
WHERE
string_table.char_block_id = char_block.id
ORDER BY string_table.id;
...它會工作,但如果我跑
SELECT * FROM get_all_string_table();
將返回空表。
你能解釋一下爲什麼會發生?
我正在使用postgresql 9.3
謝謝,我將來需要'format',我會將char_block_name傳遞給函數。 – user565447
@ user565447爲了支持一個參數,你不需要格式或動態SQL –