Postgresql - json操作数上无法解释的语法错误

时间:2014-11-15 14:52:07

标签: json postgresql

希望一双新鲜的眼睛可以发现我做错了什么,或者指出我可以进一步调查我的问题。我正在创建一个包含作为json字符串传递的参数的函数。

使用json运算符->访问json中的数据元素会报告一个让我感到困惑的语法错误。这是我的功能(简而言之):

CREATE OR REPLACE FUNCTION createUser(  
  email_f character varying(50),  
  mobile_f integer,  
  salt_f character(16),   
  hash_f character(16),  
  encryption_f smallint,  
  iterations_f smallint,  
  fnam_f character varying(16),  
  lnam_f character varying(16),  
  nickname_f character varying(16),   
  fb_f json  
) RETURNS json AS $$  
/* Example /me response from FB  
{   
  id: "101...", email: "test@test.com", verified: true,  
  name: "test testlast", first_name: "test", last_name: "testlast", gender: "male",  
  link: "https://www.facebook.com/app_scoped_user_id/101.../",  
  locale: "en_US", timezone: -5, updated_time: "2014-07-01T14:48:27+0000"  
}  
*/
BEGIN  
  IF fnam_f IS NULL THEN  
    SET fnam_f = fb_f->'first_name';  
    SET lnam_f = fb_f->'last_name';  
  END IF;  
  INSERT INTO users (email,mobile,salt,hash,encryption,iteration,username,fbid)  
    SELECT email_f,mobile_f,salt_f,hash_f,encryption_f,iteration_f,nickname_f,null  
    RETURNING row_to_json(row(*));  
END;   
$$ LANGUAGE 'plpgsql';  

运行此查询会返回语法错误:

ERROR:  syntax error at or near "->"
LINE 24:  SET fnam_f = fb_f->'first_name';

我发现这令人困惑,因为我似乎根据文档正确使用了json操作数:http://www.postgresql.org/docs/9.3/static/functions-json.html

由于

1 个答案:

答案 0 :(得分:0)

对于变量分配,请改用:=

fnam_f := fb_f->'first_name';
lnam_f := fb_f->'last_name'; 

SET语句用于更改运行时参数。