stored-functions

How to check if INSERT went well in stored function?

冷暖自知 提交于 2019-11-30 15:14:03
问题 I'm creating a stored function which should insert new row to table. In this table is also one unique column. How can I check if everything goes well and row really was inserted? How can I check exactly that it's this unique column found (for example - try to add duplicate value)? 回答1: You can check the LAST_INSERT_ID() function and INSERT IGNORE. If the INSERT IGNORE was successful, you get the primary key returned. Let's create a table with an auto increment primary key and a unique key on

CREATE FUNCTION error “This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA”

夙愿已清 提交于 2019-11-30 11:30:25
Our database has a function to generate an order number. It reads a value from a Settings table, increments it, then returns the new value. For example: CREATE FUNCTION NextOrderNumber() RETURNS INTEGER UNSIGNED NOT DETERMINISTIC BEGIN DECLARE number INTEGER UNSIGNED; UPDATE Settings SET IntegerValue=LAST_INSERT_ID(IntegerValue+1) WHERE KeyName='NextOrderNumber'; SET number=LAST_INSERT_ID(); return number; END Note: Don't critique this function I know it has flaws it's just for illustration. We use this function as follows: INSERT INTO Orders(OrderNumber, ...) SELECT NextOrderNumber(), ...

Using variables as OFFSET in SELECT statments inside mysql's stored functions

被刻印的时光 ゝ 提交于 2019-11-30 09:21:03
问题 I'm quite new to subject of writting stored function for mySQL database, hence i'm not sure if what i'm trying to do here is possible at all. I need a function that will return a column from random row from a table. I don't want to use ORDER BY RAND() method to do that, rather i would do this like this: DECLARE MAX_COUNT INT DEFAULT 120000; DECLARE rand_offset INT; DECLARE str_rnd_word VARCHAR(255); SET rand_offset = FLOOR((RAND() * MAX_COUNT)); SELECT word INTO str_rnd_word FROM all_words

Testing performance of Scalar vs Table-valued functions in sql server

荒凉一梦 提交于 2019-11-30 08:57:00
OK so I have read a whole bunch of articles suggesting table-value functions and cross apply give better performance than a scalar udf. I wanted to write my function in both ways and then test to see which one is better - but I cannot figure out what I'm supposed to use/look for to understand which is the better option. I'm using SQL Server 2005. I've tried running the estimated execution plan, the actual execution plan and analyze query in database engine tuning advisor and I don't know what it is trying to tell me. Using showplan_all on /off it looks like the table based function will use

View stored procedure/function definition in MySQL

坚强是说给别人听的谎言 提交于 2019-11-29 19:56:13
What is the MySQL command to view the definition of a stored procedure or function, similar to sp_helptext in Microsoft SQL Server? I know that SHOW PROCEDURE STATUS will display the list of the procedures available. I need to see a single procedure's definition. afftee SHOW CREATE PROCEDURE <name> Returns the text of a previously defined stored procedure that was created using the CREATE PROCEDURE statement. Swap PROCEDURE for FUNCTION for a stored function. Haim Evgi You can use this: SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = 'yourdb' AND ROUTINE_TYPE

How to pass a parameter from vb.net

蹲街弑〆低调 提交于 2019-11-29 17:05:11
I have a Program that will auto run each night, run a query, and email results. In my program I am calling a function as part of the query... What i'd like to is pass the date the program is run as the parameter. (@startdate and @enddate) @startdate will always be "today's" date at 00:00:00 and enddate will always be "Todays date" at 23:59:59. So for example. If the program was run tonight, it would pass 1/31/13 as the date. Tomorrow, it would pass 2/1/13 as the date, the next date 2/2/13, etc. If I can do this at the query level that is fine as well... Below is my code: SELECT SUM(QTY) AS

CREATE FUNCTION error “This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA”

走远了吗. 提交于 2019-11-29 17:04:41
问题 Our database has a function to generate an order number. It reads a value from a Settings table, increments it, then returns the new value. For example: CREATE FUNCTION NextOrderNumber() RETURNS INTEGER UNSIGNED NOT DETERMINISTIC BEGIN DECLARE number INTEGER UNSIGNED; UPDATE Settings SET IntegerValue=LAST_INSERT_ID(IntegerValue+1) WHERE KeyName='NextOrderNumber'; SET number=LAST_INSERT_ID(); return number; END Note: Don't critique this function I know it has flaws it's just for illustration.

Using variables as OFFSET in SELECT statments inside mysql's stored functions

浪尽此生 提交于 2019-11-29 14:41:50
I'm quite new to subject of writting stored function for mySQL database, hence i'm not sure if what i'm trying to do here is possible at all. I need a function that will return a column from random row from a table. I don't want to use ORDER BY RAND() method to do that, rather i would do this like this: DECLARE MAX_COUNT INT DEFAULT 120000; DECLARE rand_offset INT; DECLARE str_rnd_word VARCHAR(255); SET rand_offset = FLOOR((RAND() * MAX_COUNT)); SELECT word INTO str_rnd_word FROM all_words LIMIT 1 OFFSET rand_offset ; RETURN str_rnd_word; MySQL throws an error upon creating function with body

How to throw an error in MySql procedure?

末鹿安然 提交于 2019-11-29 13:18:24
What is the mechanism to force the MySQL to throw an error within the stored procedure? I have a procedure which call s another function: PREPARE my_cmd FROM @jobcommand; EXECUTE my_cmd; DEALLOCATE PREPARE my_cmd; the job command is: jobq.exec("Select 1;wfdlk# to simulatte an error"); then: CREATE PROCEDURE jobq.`exec`(jobID VARCHAR(128),cmd TEXT) BEGIN DECLARE result INT DEFAULT 0; SELECT sys_exec( CONCAT('echo ',cmd,' | base64 -d > ', '/tmp/jobq.',jobID,'.sh ; bash /tmp/jobq.',jobID,'.sh &> /tmp/jobq.',jobID)) INTO result; IF result>0 THEN # call raise_mysql_error(result); END IF; END; My

Testing performance of Scalar vs Table-valued functions in sql server

眉间皱痕 提交于 2019-11-29 12:47:06
问题 OK so I have read a whole bunch of articles suggesting table-value functions and cross apply give better performance than a scalar udf. I wanted to write my function in both ways and then test to see which one is better - but I cannot figure out what I'm supposed to use/look for to understand which is the better option. I'm using SQL Server 2005. I've tried running the estimated execution plan, the actual execution plan and analyze query in database engine tuning advisor and I don't know what