I want to use arrays in mysql stored procedures.
i.e. array would look like
(0 =>array('name','value','value1'), 1=> array('name1','value2','value3))
I want to loop through each of them and perform some action on different tables. Can i do this ?
Stored Procedures do not accept arrays; you can, however, send as many input values as you want, as long as it matches the number and types of parameters declared in the stored procedure.
Hope this helps.
If you want to pass more then one item, then use additional (temporary) table instead of IN parameters.
If you need to pass items one by one - ('name','value','value1'), then use some IN parameters - (IN name VARCHAR(255), IN value1 INT(11), IN value2 INT(11))
Related
I have made a procedure which returns multiple values and i want to use these values in another procedures without OUT parameter. is there any solution?
Depending on the actual type of result you want to "transfer" from one procedure to the next there are different options:
If your fist procedure produces only a small number of individual values (like a single id or a count or sum of something) you can store them in (a) variable(s) and use them for calling the next procedure. Otherwise - for data in a tabular form - you can create a temporary table which you then access from your second procedure.
Is it possible to change ONE parameter drop down values if ANOTHER parameter drop down values has been changed?
For example, you have two parameters:
drop down YEAR (simply populated int values from stored procedure: 2016,2017,2018)
drop down TYPES (product text value list from stored procedure)
Can we select YEAR 2018 and it will show specific values in TYPES.. then select any other year and show DEFAULT values? Both parameters are calling stored procedures.
IN SHORT:
Idea is to select the year and only show in TYPE parameter related to that YEAR product TYPE.
You can do this but there are a few caveats.
Datepickers don't work well but if you only select a year 9as a number) then this should be fine.
Default selected values may not be set to what you expect if you select a year more than once (i.e. you choose 2018 then change your mind and choose 2017).
Basically you just need to make the seconds dataset, rely on the value of your first parameter. I don't know what you data looks like but let's assume that you have a large dataset and each record has a Year, Type and then some more detailed columns.
Assume you want to choose the year, then choose from s list of types that are present in that year. I won't cover the final dataset as it's not part of your question ..
So let's say the first stored proc does something like
CREATE PROC GetYears AS
SELECT DISTINCT [year] FROM myTable ORDER BY [Year]`
Create a dataset called dsYears and point it to this stored proc.
In SSRS you create a parameter called say #pYear and set the available values to point to dsYears
Now create your second stored proc that accepts a year as a parameter so the proc will look something like
CREATE PROC GetTypes(#pYear as int) AS
SELECT DISTINCT [Type] FROM myTable WHERE [Year] = #pYear
Create a seconds dataset called dsTypes and point it to the seconds stored proc setting the parameters to be you #pYears parameter.
Now create your second parameter called #pTypes, make is multi-valued (I assume you want to select more than one type at a time in your final report). Set the available AND default values to point to dsTypes
That should be it. I've done this from memory so it might not be perfectly correct but hopefully close enough.
I got a Stored Procedure names getStocks that have a parameter. The code below is a example how I want to do with the query. Is it possible with the SELECT QUERY we can call a procedure?
PS I didn't use Function cuz I'm getting loading problem when I apply it in populating DataGridview in my VB.Net
BEGIN
SELECT ItemId, CatalogNumber, call getStocks(ItemId) AS quantity,
Cost, Minimum, Maximum, TypeId, SupplierId FROM items;
END
You cant do this. CALL is its own statement that you cant mix with SELECT in any way. If you cant use a UDF, you have to preform the integration manually.
It makes sense that a CALL cant be used like this when you consider that a CALL can optionally output a resultset. It might return a resultset, it might now. It might have one cell, one row, one column, or many rows/columns. The columns are not known at call time so the optimizer couldn't validate any JOINs to it if you put a call in the FROM and the because it can produce more than one cell (or nothing), you cant reliably put it in the SELECT. Additionally, stored procedures can have output variables, which also doesnt make sence in the context of a SELECT statement.
In other words, because the output of procedures is probably incompatible with any part of a SELECT query, the makers of mysql globally prevent their mixing.
You want to use a User-Defined Function (UDF) : http://dev.mysql.com/doc/refman/5.7/en/create-function-udf.html . Unlike stored procedures, UDF's may be called inline in a query.
I have a table with two enum columns, each enum contains about 10 possible values.
I need to write a function that calls a specific other function for each row of this table, depending on the particular combination of these values in the row. I don't want to create a 10x10 (=100) "WHEN"s and cases, what I want to do is to make a table that contains the combinations and the oids of correspoding functions. I even managed to do it, but now I don't know how to call the functions. Any help would be appreciated.
You can't call a function by it's oid. But you can of course get the functions name from the oid, and then construct a dynamic SQL statement and run it with EXECUTE in pl/pgsql.
I want to grab x amount of items out of the DB, I was wondering if there was a way
of passing some list of numbers to a stored procedure to be used with WHERE IN?
SELECT item_id, item_description
FROM items
WHERE item_id IN ( NEED_LIST_OF_IDS_HERE );
Should I not use a stored procedure for this query and just build up the sql in the application?
Or should I make a seperate DB call for each single item_id?
NOTE: items is not the actual name of the table, so dont bash me for a poor name choice, Im just hiding implementation.
Poor unanswered question from a year ago...
If you weren't using an IN clause, you could certainly generate/prepare the sql inside the stored procedure and execute it there. It's harder to parameterize the IN clause.
How about passing in the list of ids, make a temp table inside your stored procedure, and then join over to the items table?