I'm trying to set "Default Parameter Values" in stored proc.
i know the syntax TYPE 1:
CREATE PROCEDURE proc_name #param nvarchar(30) = NULL, #param1 nvarchar(60) = NULL
My TOAD 7.3 using mysql 5.1.35 is giving a syntax of TYPE 2: when I create new Procedure
`CREATE PROCEDURE name (param_name param_type)
for e.g: create procedure test (IN name varchar(50))
but i can't find the right syntax to set 'IN' values
create procedure test (in name varchar(50) = null)
is throwing syntax error.
please tell me the right syntax to set default parameters in type 2.
I've searched a lot in and out of SO for this, no luck :\
You can't explicitly do that, but you could simulate it, in some cases. I just now had a case of that, where the parameter I wanted a default value to, was being checked in an IF statement. Here's how I did it:
In the body of my condAddNewLineTo, I had if statement. Since my function was to conditionally return things, I simply put my default behavior outside of those if-statements. Like this:
IF varToCheck = <nonDefaultValue> THEN
#some lines of code to execute
RETURN <value>;
END IF;
# default behavior here
RETURN <defaultValue>;
Alternately, if not writing stored function that returns stuff, you could do something like:
IF varToCheck = <nonDefaultValue> THEN
# some lines of code to execute
ELSE
# some default code
END IF;
Hope this helps!
Related
I have a nested MySQL stored procedure that's basically structured like this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_main`(in param1 int, in param2 int)
BEGIN
declare refid int;
# do some stuff here
select refid;
call sp_child(param1, param2, refid);
END
Both sp_main and sp_child are stored procedures, the problem is sp_child itself has a return value (call it refid2) and when I use sqlquery in RODBC I always ended up getting refid2 instead of refid (which is what I need), although when I run it in MySQL Workbench I ended up seeing both refid and refid2 when call sp_main(...). All sp_child param are "in" params and the only "out" value is returned in the end of sp_child call.
Is there any way I could either:
Change the code in sp_main so that the output in sp_child is does not gets shown? I tried to use set #output = call sp_child(...); to suppress the output showing up when calling sp_main but that ended up giving me a syntax error. The sp_child unfortunately can not be modified as it is shared with several other SPs and changing the params would require changing all of the dependent SPs. Also obviously making sp_child to become a function is also out of question.
OR
Somehow use sqlquery in RODBC (or another R package or function) to somehow get both the output from the sp_child call AND sp_main call?
Thanks!
I currently have a stored procedure I'm working on that's supposed to output a message when something fails.
ALTER PROCEDURE---
#in_id INT
#msg NVARCHAR(30) = OUTPUT
The procedure variables are structured like this where one of them is an input parameter and the msg is an output.
The idea is that it uses that input parameter of an ID which works as an identification that changes the message based on the ID so the end of the sproc looks something like:
SET #msg = usf_change_message(#id, #msg)
I get an error stating that the sproc is trying to treat #msg as an input variable when in actuality it's an OUTPUT param? Can someone help me with this issue?
You use your SP as function which is obviously wrong - SP does not return a value.
You must use:
CALL usf_change_message(#id, #msg);
SELECT #msg;
Or you may convert SP to UDF and use your SELECT query (but in this case #msg in UDF parameters is excess and should be removed).
I want write procedure which after execute another procedure change auto increment value in my table. Problem is procedure created ,but not works. When i try to run i have error #1210 - Incorrect arguments to EXECUTE. I find similar problem in link : Set AUTO_INCREMENT value through variable in MySql ,but it's not working for me.
I try change #v_value for v_value (normal variable, not #variable). Also I try use '?' in #v_sql but not working too. I try change 'READS SQL DATA' but no matter. Help what's going wrong.
DELIMITER //
CREATE PROCEDURE check_increment_value()
READS SQL DATA
BEGIN
SET #v_value = (SELECT MAX(id_version)+1 FROM versions);
SET #v_sql = CONCAT('ALTER TABLE `wersje` AUTO_INCREMENT = ',#v_value);
PREPARE st FROM #v_sql;
EXECUTE st USING #v_value;
END
//
Thanks for your help :)
You pass the parameter via string interpolation, not via the prepared statement, so you do not need to specify the using clause:
EXECUTE st;
I'm facing the exact issue as described in this SO link which hasn't been answered. I've tried the suggestions given in the comment section, nothing works.
I've searched online for the answers and found no luck.
My Procedure looks like this.
DROP PROCEDURE IF EXISTS userdb.new_test;
CREATE PROCEDURE userdb.new_test (IN arg varchar(255))
BEGIN
select * FROM message
END;
and from this link I got to know
This exception indicates that a method is called with incorrect input arguments. Then, the only thing you must do is correct the values of the input parameters. In order to achieve that, follow the call stack found in the stack trace and check which method produced the invalid argument.
does anyone know how to fix this?
That's most probably because of the non-default value parameter in your procedure IN arg varchar(255) and if not wrong you are probably calling the procedure without passing any parameter. You need to call like
call userdb.new_test 'some value'
MySQL doesn't support parameter default value likewise in SQL Server and so you can't say IN arg varchar(255) = null
Also, I don't see you are using that parameter in your query and so best is drop it;
DROP PROCEDURE IF EXISTS userdb.new_test;
CREATE PROCEDURE userdb.new_test
BEGIN
select * FROM message
END;
I'm wondering if there is a way, after pragmatically giving a Varchar variable a value, to use that variable in the From Statement? We have archive tables that will cycle out and back in. So, the table can go missing for several days and when they cycle back in they change the table name to end w/ the current year (Ex. 012015 when before it was 012014.) Because of this my query can throw up object errors and I'm the only guy on my team that understands the simple error and how to quickly fix the stupid thing. below is the solution I'm trying to get to work, but I keep getting the error "Must declare the table variable #Jan." I totally understand what a table variable is and how to use it and that is obviously not what I want to do here. Is there any way to use a varchar variable (or other similar variable type) in the From Statement? Code below:
Declare #Jan Varchar(40)
IF OBJECT_ID('ColTelephonyArchive.dbo.ACDSkill201401') IS NOT NULL
Begin
Set #Jan = 'ColTelephonyArchive.dbo.ACDSkill201401'
END
IF OBJECT_ID('ColTelephonyArchive.dbo.ACDSkill201501') IS NOT NULL
Begin
Set #Jan = 'ColTelephonyArchive.dbo.ACDSkill201501'
END
IF #Jan IS NULL
Begin
Set #Jan = 'Does.Not.Exist'
END
Select WorkDte, SwitchNbr, SkillNbr,StaffTimeInSec, AvailableTimeInSec, ACDCallTotCt
,AbandonCallTotCt, AbandonCall1Ct, AnswerTimeInSec, ACDTalkTimeInSec,TotAcwTimeInSec, HoldTimeInSec
FROM #Jan
If I'm getting your problem right, you need to build the dynamic query. So in your case it would be
DECLARE #qry VARCHAR(511)
SET #qry = 'Select WorkDte, SwitchNbr, SkillNbr,StaffTimeInSec, AvailableTimeInSec, ACDCallTotCt
,AbandonCallTotCt, AbandonCall1Ct, AnswerTimeInSec, ACDTalkTimeInSec,TotAcwTimeInSec, HoldTimeInSec
FROM ' + #Jan
EXEC (#qry)