stored-functions

MySQL String separation by comma operator

烈酒焚心 提交于 2019-12-03 08:02:11
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, description text, primary key(ID)); WHILE _FoundIndex > 1 DO INSERT INTO _RtnValue (description) SELECT

Constructing a string out of several records with 2 columns

半世苍凉 提交于 2019-12-02 04:27:14
问题 I have prepared a simple SQL Fiddle for my question - In a word game written in Pl/pgSQL for PostgreSQL 10.2 player moves are stored in the table: CREATE TABLE words_scores ( mid bigint NOT NULL REFERENCES words_moves ON DELETE CASCADE, gid integer NOT NULL REFERENCES words_games ON DELETE CASCADE, uid integer NOT NULL REFERENCES words_users ON DELETE CASCADE, word text NOT NULL CHECK(word ~ '^[A-Z]{2,}$'), score integer NOT NULL CHECK(score >= 0) ); Here it is filled with some test data:

mysql stored function parameter

眉间皱痕 提交于 2019-12-01 19:16:36
问题 I have just started to create a stored function this is my first time so I am having a few problems. Currently I call the function using SELECT test(); (test is the function name for now). I want to send a number to the function (username ID) and have the username returned. I have this working by using SELECT test(1); 1 is the ID of a user in the table. This seems to work as the username is returned, but if I type in any number the same username is returned also. BEGIN DECLARE new_username

mysql stored function parameter

心不动则不痛 提交于 2019-12-01 18:49:30
I have just started to create a stored function this is my first time so I am having a few problems. Currently I call the function using SELECT test(); (test is the function name for now). I want to send a number to the function (username ID) and have the username returned. I have this working by using SELECT test(1); 1 is the ID of a user in the table. This seems to work as the username is returned, but if I type in any number the same username is returned also. BEGIN DECLARE new_username VARCHAR(90); SELECT `username` INTO new_username FROM `users` WHERE `ID` = ID; return new_username; END I

Create database using a stored function

为君一笑 提交于 2019-12-01 18:00:47
问题 I am new to PostgreSQL and want to create a database using a stored function. For ex: CREATE OR REPLACE FUNCTION mt_test(dbname character varying) RETURNS integer AS $BODY$ Create Database $1; Select 1; $BODY$ LANGUAGE sql; When I am trying to execute this function I get a syntax error. Does Postgres support the CREATE DATABASE statement in stored functions? 回答1: This question is old, but for the sake of completeness ... As has been pointed out in other answers, that's not simply possible

Required single query to fetch data from tables

一曲冷凌霜 提交于 2019-12-01 08:12:23
I have the following tables //all users deails smsusers(id,fname , lname ,primary key(id)); //message details of users //one smsusers can have N messages user_messages(messageid,message,adddate ,sentby,visibility, userid,primary key(messageid),foreign key(userid) references smsusers(id), foreign key(sentby) references smsusers(id)); //One message(user_message) can have N comments comments(comment_id,comment_on ,commented_by,comment_date, comment,foreign key(commented_by) references smsusers(id), primary key(comment_id)); //one message(user_message) can have N post_images post_images(image_id

Stored Procedure to Open and Read a text file

橙三吉。 提交于 2019-12-01 04:43:39
I am looking for a stored procedure code that will open a text file, read in several thousand lines, and add the code to a table in the database. Is there a simple way to implement this in T-SQL? If the file is ready to load "as-is" (no data transformations or complex mappings required), you can use the Bulk Insert command: CREATE PROC dbo.uspImportTextFile AS BULK INSERT Tablename FROM 'C:\ImportFile.txt' WITH ( FIELDTERMINATOR ='|', FIRSTROW = 2 ) http://msdn.microsoft.com/en-us/library/ms188365.aspx I would recommend looking at using SSIS. It's designed to do this sort of thing (especially

Error Code: 1422. Explicit or implicit commit is not allowed in stored function or trigger

隐身守侯 提交于 2019-12-01 04:21:54
Everywhere I look it seems MySQL stored procedures can do transactions. Yet when I declare my stored function create function test( a int ) returns int MODIFIES SQL DATA BEGIN START TRANSACTION ; update t set col='some value' where id=a ; COMMIT ; return 0 ; END // I get Error Code: 1422. Explicit or implicit commit is not allowed in stored function or trigger. Actually you are not allowed transactions inside stored functions . You are allowed transactions inside stored procedures only. create procedure test( a int ) MODIFIES SQL DATA BEGIN START TRANSACTION ; update t set col='some value'

Error Code: 1422. Explicit or implicit commit is not allowed in stored function or trigger

不问归期 提交于 2019-12-01 01:39:57
问题 Everywhere I look it seems MySQL stored procedures can do transactions. Yet when I declare my stored function create function test( a int ) returns int MODIFIES SQL DATA BEGIN START TRANSACTION ; update t set col='some value' where id=a ; COMMIT ; return 0 ; END // I get Error Code: 1422. Explicit or implicit commit is not allowed in stored function or trigger. 回答1: Actually you are not allowed transactions inside stored functions . You are allowed transactions inside stored procedures only.

How to properly loop in a stored function on MySQL?

丶灬走出姿态 提交于 2019-11-30 19:43:33
I am having some difficulty getting a pretty simple stored procedure right. Consider the following article table snippet: id replaced_by baseID 1 2 0 2 3 0 3 0 0 A simple hierarchical table, using copy-on-write. When an article is edited, the replaced_by field of the current article is set to the id of it's new copy. I've added a baseID field, which in the future should store the baseID of an article. In my example above, there is one article (eg id 3). It's baseID would be 1. To get the baseID, I have created the following stored procedure: DELIMITER $$ CREATE FUNCTION getBaseID(articleID INT