I am learning Npgsql and PostgreSQL. I am unable to define the output parameter correctly. What am I doing wrong?

Here is the function:

CREATE OR REPLACE FUNCTION Insert_Customer_WithOutputParameter(
IN _FirstName character varying DEFAULT NULL::character varying,
IN _LastName character varying DEFAULT NULL::character varying,
OUT _CustomerID integer)
RETURNS integer as
$BODY$
BEGIN
INSERT INTO Customers (FirstName, LastName) VALUES (_FirstName, _LastName); SELECT _CustomerID = lastval(); END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

Here is the code:

[Test]
public void ExecuteNonQuerySproc()
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost; Database=postgres; User ID=postgres; Password=password");
conn.Open();
IDbCommand command = conn.CreateCommand();
command.CommandText = "Insert_Customer_WithOutputParameter";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new NpgsqlParameter("@FirstName", "John"));
command.Parameters.Add(new NpgsqlParameter("@LastName", "Smith"));
NpgsqlParameter outParm = new NpgsqlParameter("@CustomerID", NpgsqlDbType.Integer)
{
Direction = ParameterDirection.Output
};
command.Parameters.Add(outParm); command.ExecuteNonQuery();
conn.Close();
Console.WriteLine(outParm.Value);
}

Here is the error message I am getting: Npgsql.NpgsqlException : ERROR: 42601: query has no destination for result data

The following doesn't work:

SELECT _CustomerID = lastval();

Replace it with a simple:

_CustomerID = lastval();

Note that Npgsql currently binds parameters by position only, and not by name. This means that the names you give in the NpgsqlParameter instances mean nothing - their order of addition must correspond to the function's declaration. Npgsql 3.1 will support named binding of function arguments (see this issue).

修改前:

-- Function: sp_updatesecurity(integer, character, character, character, character, integer, integer, character, integer)

-- DROP FUNCTION sp_updatesecurity(integer, character, character, character, character, integer, integer, character, integer);

CREATE OR REPLACE FUNCTION sp_updatesecurity(
OUT sqlcode_out integer,
IN parastaffid integer,
IN parafunctioncode character,
IN paraviewflag character,
IN paramodifyflag character,
IN paraadvanceflag character,
IN paralocalversion integer,
IN original_staffid integer,
IN original_functioncode character,
IN original_localversion integer)
RETURNS integer AS
$BODY$
DECLARE SQLCODE_OUT_TMP INT DEFAULT 0;
begin
IF (paraVIEWFLAG IS NULL) OR ((paraVIEWFLAG <> '') AND (paraVIEWFLAG <> '')) THEN
set SQLCODE_OUT_TMP = -1;
END IF; IF (paraMODIFYFLAG IS NULL) OR ((paraMODIFYFLAG <> '') AND (paraMODIFYFLAG <> '')) THEN
SET SQLCODE_OUT_TMP = -2;
END IF; IF (paraADVANCEFLAG IS NULL) OR ((paraADVANCEFLAG <> '') AND (paraADVANCEFLAG <> '')) THEN
SET SQLCODE_OUT_TMP = -3;
END IF; IF NOT EXISTS (SELECT *
FROM DMS.CM_STAFF
WHERE STAFFPKID = paraSTAFFID AND STATUS = '' AND (DELETED IS NULL OR DELETED = '')) THEN
SET SQLCODE_OUT_TMP = -4;
END IF; IF NOT EXISTS (SELECT *
FROM DMS.MM_FUNCTION
WHERE FUNCTIONCODE = paraFUNCTIONCODE ) THEN
SET SQLCODE_OUT_TMP = -5;
END IF; UPDATE DMS.MM_SECURITY
SET STAFFID = paraSTAFFID
, FUNCTIONCODE = paraFUNCTIONCODE
, VIEWFLAG = paraVIEWFLAG
, MODIFYFLAG = paraMODIFYFLAG
, ADVANCEFLAG = paraADVANCEFLAG
, INPUTTIME = CURRENT_TIMESTAMP
, LOCALVERSION = LOCALVERSION + 1
WHERE (STAFFID = original_STAFFID) AND (FUNCTIONCODE = original_FUNCTIONCODE) AND (LOCALVERSION = original_LOCALVERSION);
SELECT LOCALVERSION INTO paraLOCALVERSION FROM MM_SECURITY
WHERE (STAFFID = paraSTAFFID) AND (FUNCTIONCODE = paraFUNCTIONCODE);
SELECT SQLCODE_OUT_TMP; end
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sp_updatesecurity(integer, character, character, character, character, integer, integer, character, integer)
OWNER TO postgres;

修改后:

-- Function: sp_updatesecurity(integer, character, character, character, character, integer, integer, character, integer)

-- DROP FUNCTION sp_updatesecurity(integer, character, character, character, character, integer, integer, character, integer);

CREATE OR REPLACE FUNCTION sp_updatesecurity(
OUT sqlcode_out integer,
IN parastaffid integer,
IN parafunctioncode character,
IN paraviewflag character,
IN paramodifyflag character,
IN paraadvanceflag character,
IN paralocalversion integer,
IN original_staffid integer,
IN original_functioncode character,
IN original_localversion integer)
RETURNS integer AS
$BODY$
DECLARE SQLCODE_OUT_TMP INT DEFAULT 0;
begin
IF (paraVIEWFLAG IS NULL) OR ((paraVIEWFLAG <> '') AND (paraVIEWFLAG <> '')) THEN
set SQLCODE_OUT_TMP = -1;
END IF; IF (paraMODIFYFLAG IS NULL) OR ((paraMODIFYFLAG <> '') AND (paraMODIFYFLAG <> '')) THEN
SET SQLCODE_OUT_TMP = -2;
END IF; IF (paraADVANCEFLAG IS NULL) OR ((paraADVANCEFLAG <> '') AND (paraADVANCEFLAG <> '')) THEN
SET SQLCODE_OUT_TMP = -3;
END IF; IF NOT EXISTS (SELECT *
FROM DMS.CM_STAFF
WHERE STAFFPKID = paraSTAFFID AND STATUS = '' AND (DELETED IS NULL OR DELETED = '')) THEN
SET SQLCODE_OUT_TMP = -4;
END IF; IF NOT EXISTS (SELECT *
FROM DMS.MM_FUNCTION
WHERE FUNCTIONCODE = paraFUNCTIONCODE ) THEN
SET SQLCODE_OUT_TMP = -5;
END IF; UPDATE DMS.MM_SECURITY
SET STAFFID = paraSTAFFID
, FUNCTIONCODE = paraFUNCTIONCODE
, VIEWFLAG = paraVIEWFLAG
, MODIFYFLAG = paraMODIFYFLAG
, ADVANCEFLAG = paraADVANCEFLAG
, INPUTTIME = CURRENT_TIMESTAMP
, LOCALVERSION = LOCALVERSION + 1
WHERE (STAFFID = original_STAFFID) AND (FUNCTIONCODE = original_FUNCTIONCODE) AND (LOCALVERSION = original_LOCALVERSION);
SELECT LOCALVERSION INTO paraLOCALVERSION FROM MM_SECURITY
WHERE (STAFFID = paraSTAFFID) AND (FUNCTIONCODE = paraFUNCTIONCODE);
sqlcode_out = SQLCODE_OUT_TMP; end
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sp_updatesecurity(integer, character, character, character, character, integer, integer, character, integer)
OWNER TO postgres;

解决postgresql -- ERROR: 42601: query has no destination for result data的更多相关文章

  1. POSTGRESQL中ERROR: recursive query "t" column 2 has type character varying(150) in non-recursive term but type character varying overall

    最近在做项目的时候有个需求是需要查到当前登录的用户下辖所有区域的数据,并将查询出来的部门信息以如下格式展示 最高人民法院>江苏省高级人民法院>南通市中级人民法院最高人民法院>江苏省高 ...

  2. ERROR: No query specified

    mysql中执行show命令的时候,提示以下信息: mysql> show variables like 'datadir'\G; *************************** 1. ...

  3. 工作日志,error parsing query: unable to find time zone

    工作日志,error parsing query: unable to find time zone 坑 Windows 系统使用influxdb数据库,在执行查询语句时提示 ERR: error p ...

  4. influxdb ERR: error parsing query: found -, expected

    ERR: error parsing query: found -, expected 使用时遇到这个问题,执行语句: select * FROM test10-cc-core01 本来以为和sql语 ...

  5. MySQL查询报错 ERROR: No query specified

    今天1网友,查询报错ERROR: No query specified,随后它发来截图. root case:查询语法错误 \G后面不能再加分号;,由于\G在功能上等同于;,假设加了分号,那么就是;; ...

  6. 解决Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!

    解决Unknown error: to the dx.jar the SDK folder!最近渐渐迁移到Android Studio来了,更新过Android SDK Manager里的东西后,打开 ...

  7. presto——java.sql.SQLException: Error executing query与javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?异常问题

    使用presto的时候以mysql为presto的数据源 安装的presto是0.95版本:使用的presto-jdbc是0.202的,这里使用jdbc去访问时候,connection可以链接成功,但 ...

  8. ubuntu 16.04 + eigen3 安装(解决 fatal error: Eigen/Core: No such file or directory)

    1.安装 sudo apt-get install libeigen3-dev 2. 解决 fatal error: Eigen/Core: No such file or directory 当调用 ...

  9. ArcGIS Server 地图服务,查询出错 Error performing query operation

    Error: Error performing query operation Error Message Querying a layer from a Map Service that retur ...

随机推荐

  1. Git-it字典翻译

    Git-it字典翻译 下面的内容翻译自git-it/dictionary 水平有限,翻译欠佳. Git准备工作 创建一个新的文件夹(目录) $ mkdir <目录名称> 切换到这个目录 ( ...

  2. 为MongoDB创建一个Windows服务

    一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...

  3. Twisted安装

    Debian sudo apt-get install gcc python-dev && sudo pip install twisted CentOS sudo yum insta ...

  4. pip 安装命令

    pip官网文档 https://pip.pypa.io/en/latest/reference/pip.html 若没有将c:\Python27\Scripts加入到path环境变量,可以在c:\Py ...

  5. 用普通用户通过sudo进行启动tomcat时报如下异常

    用普通用户通过sudo进行启动tomcat时报如下异常 tomcat user 不在 sudoers 文件中.此事将被报告. 这是由于sudo命令使用root用户执行命令.而处于安全性的考虑,一般不允 ...

  6. Java for LeetCode 223 Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  7. Windows 下的 MarkdownPad 2 工具使用

    MarkdownPad 2 工具(windows) 一. 软件下载和安装 下载登陆官网: http://markdownpad.com/ 点击Download,会自动下载.或者直接点击http://m ...

  8. shiro的简单使用

    <?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http:// ...

  9. IOS - delegate为什么不使用retain

    循环引用所有的引用计数系统,都存在循环应用的问题.例如下面的引用关系: 对象a创建并引用了对象b.对象b创建并引用了对象c.对象c创建并引用了对象b. 这时候b和c的引用计数分别是2和1.当a不再使用 ...

  10. 9.22 window对象、document对象

    一.window对象: 属性(值或者子对象): opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器打开的,则opener是null,可以利用这个属性来关闭源窗口 dialogArgume ...