这是一个关于使用半正公式计算地球上两个纬度和经度点之间距离的问题,用于需要"找到最近"函数的项目.
在这篇文章中,我们已经在MySQL中很好地讨论并解决了hasrsine公式.
然后,我问了这个问题,将其转换为存储函数,以便将来可用于未来的项目,而无需查找,记住或重新键入长形式的公式.
都很好.除了我的函数结果(稍微)不同于直接在查询中键入公式,所有其他条件相同.为什么是这样?
所以这是我写的函数:
DELIMITER $$
DROP FUNCTION IF EXISTS haversine $$
CREATE FUNCTION `haversine`
(fromLatitude FLOAT,
fromLongitude FLOAT,
toLatitude FLOAT,
toLongitude FLOAT,
unit VARCHAR(20)
)
RETURNS FLOAT
DETERMINISTIC
COMMENT 'Returns the distance on the Earth between two known points of longitude and latitude'
BEGIN
DECLARE radius FLOAT;
DECLARE distance FLOAT;
IF unit = 'MILES' THEN SET radius = '3959';
ELSEIF (unit = 'NAUTICAL_MILES' OR unit='NM') THEN SET radius = '3440.27694';
ELSEIF (unit = 'YARDS' …Run Code Online (Sandbox Code Playgroud) 我是在 postgresql 和一般情况下编写存储函数的新手。我正在尝试使用输入参数写入 onw 并返回一组存储在临时表中的结果。我在我的函数中执行以下操作。1) 获取所有消费者的列表并将他们的 id 存储在临时表中。2)迭代特定表并从上述列表中检索与每个值对应的值并存储在临时表中。3)返回临时表。
这是我自己尝试编写的函数,
create or replace function getPumps(status varchar) returns setof record as $$ (setof record?)
DECLARE
cons_id integer[];
i integer;
temp table tmp_table;--Point B
BEGIN
select consumer_id into cons_id from db_consumer_pump_details;
FOR i in select * from cons_id LOOP
select objectid,pump_id,pump_serial_id,repdate,pumpmake,db_consumer_pump_details.status,db_consumer.consumer_name,db_consumer.wenexa_id,db_consumer.rr_no into tmp_table from db_consumer_pump_details inner join db_consumer on db_consumer.consumer_id=db_consumer_pump_details.consumer_id
where db_consumer_pump_details.consumer_id=i and db_consumer_pump_details.status=$1--Point A
order by db_consumer_pump_details.consumer_id,pump_id,createddate desc limit 2
END LOOP;
return tmp_table
END;
$$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
但是,我不确定我的方法,以及我是否在上面的代码中标记的 A 点和 B …
我该如何解决这个问题?我错过了我的代码或功能代码?
这是我的C#代码
public int AddDetails(string business, string clerkid, string serverid, string ticket, string tablenumber,
string sourcecard, string recipientcard, DateTime datesaved, string status)
{
CreateConnection();
int dbId = 0;
using(cmd = new MySqlCommand())
{
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "AddDetails";
cmd.Parameters.AddWithValue("m_business", business);
cmd.Parameters.AddWithValue("m_clerkid", clerkid);
cmd.Parameters.AddWithValue("m_serverid", serverid);
cmd.Parameters.AddWithValue("m_ticket", ticket);
cmd.Parameters.AddWithValue("m_tablenumber", tablenumber);
cmd.Parameters.AddWithValue("m_sourcecard", sourcecard);
cmd.Parameters.AddWithValue("m_recipientcard", recipientcard);
cmd.Parameters.AddWithValue("m_datesaved", datesaved);
cmd.Parameters.AddWithValue("m_status", status);
cn.Open();
//dbId = Convert.ToInt32(cmd.LastInsertedId);
dbId = int.Parse(cmd.ExecuteScalar().ToString());
//dbId = Convert.ToInt32(cmd.ExecuteNonQuery());
cn.Close();
}
return dbId;
}
Run Code Online (Sandbox Code Playgroud)
这是我存储的功能
CREATE DEFINER=`root`@`192.168.21.%` FUNCTION `AddDetails`( …Run Code Online (Sandbox Code Playgroud) 我verifierQteDemandee在我的数据库中创建了一个存储函数,它有一个整数参数numBonIn,它返回一个布尔值.
我想在我的java程序中执行这个函数,我用google搜索它,我所能找到的就是执行一个存储过程,但我假设执行存储函数与执行存储过程相同,这就是代码我尝试过:
CallableStatement cStmt = con.prepareCall("{call verifierQteDemandee(?)}");
cStmt.setInt("numBonIn", 42);
boolean hadResults = cStmt.execute();
if (hadResults) {
ResultSet rs = cStmt.getResultSet();
}
Boolean outputValue = cStmt.getBoolean(outputValue);;
Run Code Online (Sandbox Code Playgroud)
该con变量是ANS实例Connection.
你可以在我的代码中注意到我不知道如何从这行中的存储函数中获取返回值:int outputValue = cStmt.getInt("");.
如果有人知道如何获得返回值的程序,我会很感激.
我有这个功能:
CREATE FUNCTION [dbo].[full_ads](@date SMALLDATETIME)
returns TABLE
AS
RETURN
SELECT *,
COALESCE((SELECT TOP 1 ptype
FROM special_ads
WHERE [adid] = a.id
AND @date BETWEEN starts AND ends), 1) AS ptype,
(SELECT TOP 1 name
FROM cities
WHERE id = a.cid) AS city,
(SELECT TOP 1 name
FROM provinces
WHERE id = (SELECT pid
FROM cities
WHERE id = a.cid)) AS province,
(SELECT TOP 1 name
FROM models
WHERE id = a.mid) AS model,
(SELECT TOP 1 name
FROM car_names
WHERE …Run Code Online (Sandbox Code Playgroud) 我定义了一个带有 11 个参数的表值函数X。它们的类型分别是nvarchar(30)、nvarchar(30)、datetime、datetime、nvarchar(15)、nvarchar(4)、xml、nvarchar(8)、nvarchar(80)、bit和bit。这是针对 Microsoft SQL Server 2012 的。当我使用以下命令调用该函数时
select * from
X('A','B','2014-01-01','2014-12-31',null,null,'<C><D>E</D></C>',null,null,1,0)
Run Code Online (Sandbox Code Playgroud)
我遇到这个错误:
Parameters were not supplied for the function X
Run Code Online (Sandbox Code Playgroud)
它与以下两种明显不同:
An insufficient number of arguments were supplied for the procedure or function X
Procedure or function X has too many arguments specified.
Run Code Online (Sandbox Code Playgroud)
这与两个预期参数值有关吗null?我怎样才能克服这个问题并定义/调用一个表值函数,比如这个有 11 个参数的函数,其中一些参数可能带有null?
更新如果我传入任意字符串而不是null. 所以肯定还有另一个(也许是愚蠢的)错误。
我知道以下是可能的。即我可以将引用游标作为 Postgresql 中的返回值。
CREATE FUNCTION employeefunc(int) RETURNS refcursor AS '
DECLARE ref refcursor;
BEGIN
OPEN ref FOR SELECT * FROM employee where id = $1;
RETURN ref;
END;
Run Code Online (Sandbox Code Playgroud)
但是我们可以将引用游标作为 postgresql 函数中的 OUT 参数吗?
供您参考,请遵循我正在寻找的 Oracle 等效内容。
create or replace procedure employeefunc(rc out sys_refcursor) as
begin
open rc for 'select * from employee';
end;
Run Code Online (Sandbox Code Playgroud) postgresql plsql stored-procedures ref-cursor stored-functions
是否可以在函数内部使用过程?例如,我想收集与 id 相关的所有行,但我也想计算行数并在 select 语句中使用它。这是行不通的:
drop procedure if exists relatives;
create procedure relatives(in parent int(11),out counted int(11))
begin
set counted=(select count(*) from category where related=parent);
end;
drop function if exists relatives_count;
create function relatives_count(parent parent(11)) returns int(11)
begin
declare count int(11);
call relatives(parent,counted);
return counted;
end;
Run Code Online (Sandbox Code Playgroud)
这样我就可以使用计数
select relatives_count(id) from category
Run Code Online (Sandbox Code Playgroud)
这只是出于好奇的目的。它可能看起来毫无意义,因为我只能调用单个选择查询并获得相同的结果,但我想知道如何在函数中使用我的过程输出变量。
我试图创建一个功能
CREATE FUNCTION `func`(param1 INT, param2 INT, param3 TEXT) RETURNS int(11)
BEGIN
INSERT INTO `table1` (`column1`, `column2`, `column3` )
VALUES (param1, param2, param3)
ON DUPLICATE KEY
UPDATE `time_stamp` = UNIX_TIMESTAMP();
RETURN last_insert_id();
END
Run Code Online (Sandbox Code Playgroud)
如果它不存在,则会在表中插入一行,否则会更新它.请注意,last_insert_id()如果函数将插入则返回哪个是正确的,否则在更新时将是不可预测的.
我知道解决这个问题的另一种方法是使用单独的方法SELECTS并确定它是否存在; 如果存在则检索id并update使用它id; 否则只是做一个平原INSERT.
现在我的问题是:除了sql我现在正在做的事情之外,还有其他两种做法的替代方案吗?
编辑1
附录:
有一个自动递增的索引.要插入的所有值都是唯一的
我宁愿不改变索引,因为它被引用到另一个表中.
我只是试图从服务器(getStat)调用存储的函数,看起来像这样:
create type stat as (type text, location text, number int);
create function getStat() returns setof stat as 'select distinct table1.type, table1.location, table1.number from table1, table2 where table2.finding=10 order by number desc;' language 'sql';
Run Code Online (Sandbox Code Playgroud)
现在这里是jdbc代码:
CallableStatement callable = null;
String storedProc = "{call getStat(?, ?, ?)}";
try {
callable = connection.prepareCall(storedProc);
callable.registerOutParameter(1, java.sql.Types.VARCHAR);
callable.registerOutParameter(2, java.sql.Types.VARCHAR);
callable.registerOutParameter(3, java.sql.Types.INTEGER);
boolean results = callable.execute();
System.out.println(callable.getString(1));
System.out.println(callable.getString(2));
System.out.println(callable.getInt(3));
while(results){
ResultSet rs = callable.getResultSet();
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getInt(3));
}
//rs.close();
results = callable.getMoreResults();
}
Run Code Online (Sandbox Code Playgroud)
好的,现在问题是:当我调用它时,它只打印出整个批量的第一行,它应该是打印的.是的,这很清楚,因为我执行以下代码: …
stored-functions ×10
mysql ×5
postgresql ×2
sql-server ×2
c# ×1
java ×1
jdbc ×1
plsql ×1
psql ×1
ref-cursor ×1
sql ×1