我没有任何问题地调用此程序:
SELECT SP_getGlobalVariable('current_user_id')::INT ;
如果我在另一个存储过程中调用它:
-- Get current User Id --
SELECT SP_getGlobalVariable('current_user_id')::INT INTO __currentUserId ;
我有这个错误:
ERROR : Syntax error near "current_user_id"
LINE 25: SELECT SP_getGlobalVariable('current_user_id')::INT INTO _...
这可能是一个愚蠢的语法错误...如果有人可以帮助一个可怜的MySQL用户开始使用PG!
编辑1:
SELECT version();
PostgreSQL 9.3.3 on i686-pc-linux-gnu, compiled by gcc-4.4.real (Debian 4.4.5-8) 4.4.5, 32-bit
这是version()调用的结果。
CREATE OR REPLACE FUNCTION SP_getGlobalVariable (__variableName VARCHAR(64))
RETURNS VARCHAR
AS
'
DECLARE
BEGIN
RETURN (SELECT value FROM tmp_variables
WHERE name = __variableName) ;
END ;
'
LANGUAGE 'plpgsql';
在临时表中获取全局变量的过程。*
编辑2:
CREATE OR REPLACE FUNCTION SP_insertSite (__siteName VARCHAR(70), __siteDescription TEXT, __siteLatitude NUMERIC, __siteLongitude NUMERIC)
RETURNS INT
AS
'
DECLARE
-- Variables
__right_edge_father INT ;
__left_edge_father INT ;
__depth_father INT ;
__nbrSites INT ;
__insertId INT ;
__currentUserId INT ;
BEGIN
-- Check if tree is empty --
SELECT COUNT(*) INTO __nbrSites
FROM site ;
-- Get current User Id --
SELECT SP_getGlobalVariable('current_user_id')::INT INTO __currentUserId ;
IF __nbrSites = 0 THEN
INSERT INTO site (site_name, site_description, site_latitude, site_longitude, site_left_edge, site_right_edge, site_depth, site_create_dt, site_create_user_id)
VALUES(__siteName, __siteDescription, 0.0, 0.0, 1, 2, 0, LOCALTIMESTAMP, __currentUserId)
RETURNING site_id INTO __insertId;
ELSE
-- Get father edges --
SELECT site_left_edge, site_right_edge, site_depth
INTO __left_edge_father, __right_edge_father, __depth_father
FROM site
WHERE site_id = (SELECT site_id FROM site WHERE site_depth = 0 LIMIT 1) ;
-- Updates left edges --
UPDATE site
SET site_left_edge = site_left_edge + 2
WHERE site_left_edge >= __right_edge_father ;
-- Updates right edges --
UPDATE site
SET site_right_edge = site_right_edge + 2
WHERE site_right_edge >= __right_edge_father ;
-- Insert new node --
INSERT INTO site (site_name, site_description, site_latitude, site_longitude, site_left_edge, site_right_edge, site_depth, site_create_dt, site_create_user_id)
VALUES(__siteName, __siteDescription, __siteLatitude, __siteLongitude, __right_edge_father, __right_edge_father+1, __depth_father+1, LOCALTIMESTAMP, __currentUserId)
RETURNING site_id INTO __insertId;
END IF ;
RETURN __insertId ;
END ;
'
LANGUAGE plpgsql;
答案 0 :(得分:0)
问题在于您使用单引号来封装函数体,因此,当您尝试在体内添加单引号时,您需要将其转义,例如:
CREATE OR REPLACE FUNCTION SP_insertSite (...)
RETURNS INT
AS
'
DECLARE
...
-- Get current User Id --
SELECT SP_getGlobalVariable(''current_user_id'')::INT INTO __currentUserId ;
...
RETURN __insertId ;
END ;
'
LANGUAGE plpgsql;
那会很好用。但我建议您使用dollar-quoting,这样您就不必担心函数体内的单引号转义,如下所示:
CREATE OR REPLACE FUNCTION SP_insertSite (...)
RETURNS INT
AS
$$
DECLARE
...
-- Get current User Id --
SELECT SP_getGlobalVariable('current_user_id')::INT INTO __currentUserId ;
...
RETURN __insertId ;
END ;
$$
LANGUAGE plpgsql;
我建议你阅读PostgreSQL docs about the usage of dollar-quoting in PL/pgSQL(整个文件,不仅仅是这个小节也很有趣)。