Handling Multiple Values in SS Reports - sql-server-2008

I have a question, I have an SSRS report with multiple value field, I was just wandering how can I parse it from the SQL stored procedure that I have created eg.
Multiple Values -> Egg,Banana,Apple,Candies,Honey
SQL:
SELECT Qty, Amount FROM tblFoodList WHERE ListOfFood IN (?Multiple Values?)
How am I able to put the multiple values inside the IN CLAUSE, likewise is this the correct way, or is there any other way..
Should I pass it as a string like?
#varTemp = 'Egg','Banana','Apple','Candies','Honey'
SELECT Qty, Amount FROM tblFoodList WHERE ListOfFood IN (#varTemp)
Btw, I'm using SQL 2008 R2, and the string manipulation approach for SQL

Since you're using a stored procedure, SSRS will always pass a CSV list; there is no option for a table-valued parameter or anything like that.
So it will be a string that looks like 'Egg,Banana,Apple,Candies,Honey'.
Next step is to turn this string into a some sort of table variable and use that in the IN clause; there are any numbers of techniques you can use here.
Erland Sommarskog has maintained a great list of techniques.
This SO question has various alternatives, too.
If you embed the query directly in the report, you can reference the variable like IN (#varTemp); it's essentially dynamic SQL so this will build the appropriate query. An alternative to consider if you don't feel like putting too much logic in an SP.

Related

Return multiple rows from SQL prepared statement in stored procedure [duplicate]

I want to pass a comma delemited list of values as a parameter to a query I'm building using the designer in Visual Studio 2008 based on some strongly typed DAL tutorials I was going through. The query is going against a DB2 database. Here's what I want to do:
select * from prices where customer in(?)
It works fine win I pass in 123456 as ?
But fails when I pass in '123456' (it is a char field so I don't know why this doesn't work; it must be adding these behind the scenes) or 123456, 123457 or '123456', '123457'
I'm adding this page to a portal where all the data access is being done based on the DAL designer model with a BLL that calls it so I wanted to do it this way for consistency. Is this possible or is this a situation where the tool just isn't flexible enough to accomplish what I need it to do? Thanks.
This is a very common mistake people make with parameterized queries. You have to remember that a single parameter placeholder "?" is a substitute for a single value.
See the question link below for a clever solution for this problem from Joel Spolsky.
"Parameterizing a SQL IN clause?"
Also a bunch of other people answered the same question, reiterating that the standard solution is to construct the SQL query dynamically, appending a parameter placeholder for each value you need to pass.

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.

Alternative approach to parameters in Invantive Control to control query outcome

I would like to use parameters in Invantive control.
For example, I would like to retrieve only the hours, of Exact Online Project management which, are in the given data parameters.
There are three often used approaches:
Use parameters in the model editor.
Use Excel values.
Use data from the databases involved.
Model Parameters
To use parameters in the model editor, you define them in the model editor:
Select a unique code.
Possibly provide a default value (always string, use to_... or casting to change it in the queries).
Define list-of-values providing a quick pick.
The unique code is in general all in uppercase and in the format P_..., but as long it is a legal identifier anything should work.
To use them in one or more block queries or triggers:
Use the building blocks button.
Or type $P{CODE} in the SQL or trigger for Excel.
The values can be entered using the parameters button (the green funnel in the Control ribbon):
Please note that parameters are always bound as parameters, and not lexicographically substituted so you can NOT say: select * from $P{TABLE_NAME}.
Please note that parameters can be dependent on each other, so in the query for the parameter you can use another parameter. Such as first choosing the country of a project and then showing a list of projects in that country. But be wise, avoid recursion and other overly complex scenarios; the user will not easily understand it.
Excel values
To use Excel values, you can define them as follows:
Enter a value somewhere in Excel.
Optionally define a named range to make it easier to change the location.
You can of course assign lists as normal in Excel as a pop-up or other validations. Also cell locking works fine.
To use the actual value in a query or trigger of Invantive Control you can use the building blocks in the query editor or use something like select * from table where code = $X{projectcode} or select * from table where code = $X{B2&C2&D2}. The last one shows that you can also use other type of Excel expressions.
Note that Excel parameters are also bound as parameters to the query, but that they are also typed, so the following query can be different depending on the general format of the Excel cell:
select *
from table
where code like $X{CELL}
When cell is a text, the database or Exact Online in this case will retrieve:
select *
from table
where code like :ex0
With ex0 being a text such as '8%'. But when cell is a percentage, the contents might still display in Excel as '8%', but the actual query will be with identical to the outcome of:
select *
from table
where code like 0.08
Caused me some headaches, but typing is in general a useful feature, especially with dates.
Parameter using database data
Option 3 is practically not feasible with Exact Online, since they are little possibilities to create your own tables and/or fields.
On other platforms such as Oracle you might want to enter new rows in Invantive Control in Excel and them upload them on sync to provide parameters. Especially handy in case of complex risk models.

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.

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.