MySQL stored procedure parameters - mysql

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?

Related

is possible to pass variable in a query SQL statement?

I'm wanting to know if it's possible to pass a variable into an SQL statement for a query, rather than making 12 different queries which would also have me making 12 different forms (1 for each query). The table name to update depends on the column "what_table" in a table named "weekinfo".
currently the SQL statement is:
SELECT wk1_info.ID, wk1_info.Player_Fname, wk1_info.Player_Lname, wk1_info.email, wk1_info.postion
FROM wk1_info
WHERE (((wk1_info.postion) Like 0));
is it possible to declare the table this way, or would I have to make the extra queries and forms? If it is possible could I pointed in the right direction?
No. Table and field names cannot be passed as parameters.
What you can do is to write the full SQL from a template string replacing tokens for table and/or fields names with those you need. Then run/execute the finished SQL string.

Without mysql stored procedure or udf

I am trying to implement a mysql query for some report and can't figure it out.
Let's say we have a 'companies' table, a 'users' and a 'company_users'.
The requested result set is:
'company_name', 'active_users', 'date'(lets say 30 days back).
So I have to calculate user counts up to date based on users.created_at.
I could use some help to achieve it without stored procedure because I'll need to join the result set with another table.
Thank you.

SELECT WITH CALL PROCEDURE MySQL

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.

How to count number of rows in SQL table retrieved by a procedure [duplicate]

Is there a way to get the number of rows 'returned' from a stored procedure?
I know the result set is not really returned so I can't select from it or count on it.
I tried to use an out parameter but with no success..
Basically I have some logic in the stored procedure that finds some table lines. I use it in my C# app. in another place I need the exact same logic but only the count so I will be able to use in an SQL statement.
I could bring it to the C# and count there but I prefer not.
I could also create a stored function that duplicate the logic but returns COUNT but I prefer not to duplicate so I don't maintain it twice..
Try This
found_rows function
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
SELECT COUNT(id) AS example name FROM tablename

How can I get the number of rows 'returned' from a result set of a stored procedure

Is there a way to get the number of rows 'returned' from a stored procedure?
I know the result set is not really returned so I can't select from it or count on it.
I tried to use an out parameter but with no success..
Basically I have some logic in the stored procedure that finds some table lines. I use it in my C# app. in another place I need the exact same logic but only the count so I will be able to use in an SQL statement.
I could bring it to the C# and count there but I prefer not.
I could also create a stored function that duplicate the logic but returns COUNT but I prefer not to duplicate so I don't maintain it twice..
Try This
found_rows function
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
SELECT COUNT(id) AS example name FROM tablename