stored-functions

How to create a MySQL stored aggregate function?

爷,独闯天下 提交于 2019-12-06 05:08:44
I need to write a stored function that does an aggregate operation on a column. Let's say a median (but actually, there are many different summary functions I want to implement). Ideally, I would like to have something like medBy( col1 col2 col3 ) which then for each row returns a median of the col1 values of all the rows that have the same col2 and col3 value as this one. That way, I wouldn't have to do GROUP BY on the whole query, just have a repeating value in that one column. This question ( How to write the quantile aggregate function? ) asked something similar, but it got answered with a

INSERT ON DUPLICATE KEY UPDATE with last_insert_id()

时光总嘲笑我的痴心妄想 提交于 2019-12-06 04:47:36
im trying to create a function 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 this would insert into a table a row if it doesn't exist but otherwise update it. Notice that i returned last_insert_id() which would be correct if the function would insert otherwise would be unpredictable if it updates. I know the alternative to solving this is using separate SELECTS and identify if it exists;

How does Oracle process stored function calls in SQL?

淺唱寂寞╮ 提交于 2019-12-05 21:49:38
guys. Say, I have a query: select t.value, my_stored_function(t.value) from my_table t where my_stored_function(t.value) = n_Some_Required_Value I have rewritten it in the following way: select value, func_value from (select t.value, my_stored_function(t.value) func_value from my_table t) subquery where subquery.func_value = n_Some_Required_Value Let's think of my_stored_function as of resource-consuming one. I assume, in the second query it is called twice less, but I didn't experience any significant performance increase after this change. So, I guess, my assumption was wrong. How does

Postgresql Create Trigger Before Deleting A Row

旧城冷巷雨未停 提交于 2019-12-04 19:04:55
so i have these two tables: Table user columns: id,name,surname, password,token,earnedmoney Table addlisting columns: id, user_fk,price,date_added Here is my problem: I would like to create a trigger so that when I delete a listing from the table addlisting, the price of the listing gets added to the column "earnedmoney" which is in the table user. Could somebody help me? Thank you! CREATE OR REPLACE FUNCTION add_money() RETURNS trigger AS $$BEGIN UPDATE "user" SET earnedmoney = earnedmoney + OLD.price WHERE id = OLD.user_fk; RETURN OLD; END;$$ LANGUAGE plpgsql; CREATE TRIGGER add_money BEFORE

postgresql with jdbc and stored procedures (functions): ResultSet

别说谁变了你拦得住时间么 提交于 2019-12-04 14:38:38
问题 I just tried to call a stored function from the server (getStat), that is looking like this: 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'; Now here is the jdbc code: CallableStatement callable = null; String storedProc = "{call getStat(?, ?, ?)}"; try { callable = connection.prepareCall

MySQL String separation by comma operator

我们两清 提交于 2019-12-04 12:27:32
问题 I have String asdasdwdfef,rgrgtggt,weef and i want output like in table format as shown below id decription 1 asdasdwdfef 2 rgrgtggt 3 weef For this i created a procedure here is my procedure DELIMITER ;; CREATE Procedure Split(_RowData text, _Delimeter text) BEGIN DECLARE _Iterator INT default 1; DECLARE _FoundIndex INT; DECLARE _Data varchar(255); SET _FoundIndex = LOCATE(_Delimeter,_RowData); DROP TABLE IF EXISTS _RtnValue; CREATE temporary TABLE _RtnValue(ID INT AUTO_INCREMENT NOT NULL,

MongoDB: Error executing stored JavaScript function

可紊 提交于 2019-12-04 07:32:30
When I run below stored JavaScript function I get errors: > db.system.js.save({_id:"last_n_users", value: function(n){return db.users.find().sort({created_at:-1}).limit(n)}}) > db.eval("last_n_users(10)") Here is the errors: { "value" : "DBQuery: store.users -> undefined" } Why? Please help me? The find() function returns a cursor, which can't be returned from JavaScript. The suggested workaround is to use toArray() to get an array return value. Example ... before : > use admin switched to db admin > db.system.js.save( { _id : "foo", value: function(n){return db.system.indexes.find().limit(n)}

Generate a random number of non duplicated random number in [0, 1001] through a loop

痴心易碎 提交于 2019-12-04 03:39:21
I need to generate a random number of non duplicated random number in plpgsql. The non duplicated number shall fall in the range of [1,1001]. However, the code generates number exceeding 1001. directed2number := trunc(Random()*7+1); counter := directed2number while counter > 0 loop to_point := trunc((random() * 1/directed2number - counter/directed2number + 1) * 1001 +1); ... ... counter := counter - 1; end loop; If I understand right You need a random number ( 1 to 8 ) of random numbers. The random numbers span 1 to 1001 . The random numbers need to be unique . None shall appear more than once

Optimizing a stored function call in SELECT and WHERE clauses

扶醉桌前 提交于 2019-12-04 03:35:47
I have an SQL query with the following structure: SELECT *, storedfunc(param, table.field) as f FROM table WHERE storedfunc(param, table.field) < value ORDER BY storedfunc(param, table.field); Is there a way to optimize this eliminating several function calls? Or does MySQL perform such optimization behind the scene? In fact the function is declared as deterministic. I need also to mention that the function params are partially from selected table's columns. I changed the example slightly to reflect this. Rewrite and test which one performs faster: SELECT *, storedfunc(param, table.column) AS

postgresql with jdbc and stored procedures (functions): ResultSet

送分小仙女□ 提交于 2019-12-03 09:00:41
I just tried to call a stored function from the server (getStat), that is looking like this: 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'; Now here is the jdbc code: CallableStatement callable = null; String storedProc = "{call getStat(?, ?, ?)}"; try { callable = connection.prepareCall(storedProc); callable.registerOutParameter(1, java.sql.Types.VARCHAR); callable.registerOutParameter(2,