SELECT WITH CALL PROCEDURE MySQL - 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.

Related

MS Access how can i pass my search criteria from top query to subquery?

I have a query in a base-data table that (given that my search criteria are correct) gives back approx. 950 records.
Except of the 3 criteria fields, i want to have about 10 more fields (the Project is still at the beginning) , every single one based on sub-queries, some of them normal select queries, some are aggregated queries.
As far as i know every sub-query must give 1 and only one value back.
This value school be individual for every Record of the top query.
My Problem now is, that i don't know how to pass the search criteria from the top query (simple select query) to the sub-query in the in 10 fields i mentioned before.
Is this possible at all, or is my Approach to complicated. Is there possibly an easier way?
I have a Windows 7 System with Office 2010 installed.
Your help is much appreciated.
Thanks a lot.
PS
The sub-queries are based on the same table as the top query. Sorry, I forgot to mention.
You can pass arguments between things with a function call to set a public variable. This vba must be in a Module, not behind a Form Module. I don't use this approach very often, because the global value is in volatile memory, I prefer to save the variable in a special data Table.
Public strGlobal As String
Function Func_ReadGlobal() As String
Func_ReadGlobal = strGlobal
End Function
Function Func_WriteGlobal() As String
strGlobal = Func_WriteGlobal
End Function
In all subqueries create parameter(s) and use it as search criteria. Parameter name should be the same for same column. Now, if you use those subqueries in your main query, Access will ask only once per each parameter name, you don't need to pass them explicitly to subqueries.
Thank you guys.
I did'nt think of the most obvious solution with the Globals. I will try it out as soon as my Boss gives me the time to continue with the Project.
#Sergey
I can't use the Parameter(s) way, because the whole query, incl. Subqueries shall run completely alone in VBA, without human input at all.

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

Using the result of an SP in an IN clause

I ran into a problem or maybe even a MySQL limitation.
The situation is as follows:
I have an SP X, selecting related records based on 1 or 2 arguments. The SP returns a list of 0, 1 or more id's. I wanna use that list in an IN clause like so:
SELECT
*
FROM
table
WHERE
id IN (spX(y));
This gives me an error:
Syntax error or access violation: 1305 FUNCTION z.spX does not exist
The error is a bit vague: I'm confident it's a syntax error rather than access violation or non-existence of the SP itself. If I CALL the SP directly I get my expected results.
This kinda feels like a dead end. My expectation was that MySQL would throw an error if the SP returned more than 1 column, not if it returned more than 1 row (in which case I could've used a FUNCTION instead and this would've worked straight away).
So, the question is: is there any way of using the SP's result in an IN clause?
On a side note: I'm aware I could achieve the same result by simply joining the table and then add a new where clause to the existing query, instead of using the SP. However, the real problem here is that new functionality has to be added to the application, and not having to join in tons of queries, but using a SP instead is the way of least resistance.
SELECT * FROM table WHERE id IN(SELECT spX FROM table WHERE y = ???);
you need a select statement in the parenthesis otherwise it is very unclear what you are doing.
If sPx is the name of your table and y is the variable you want to pull out of it, then you you need to say that with the select statement in parenthesis. Def just a syntax error
I implemented the solution as proposed in the chat:
https://chat.stackoverflow.com/rooms/12910/discussion-between-robin-v-g-and-hituptony
What I do step by step:
I put a placeholder in every query involved.
On a higher level in my application I call the stored procedure. To clarify: there're 2 things I know for sure when it comes to the SP:
It will always take 1 id as parameter.
It will always give 0 or more id's back.
I generate the IN() clause depending on the SP's outcome.
I replace the placeholder with the generated IN() clause and run my query.
This way I can always add or alter the conditions in the SP itself, without changing the application logic/queries.

Is there a way to call a function by its oid in postgresql?

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.

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