is possible to pass variable in a query SQL statement? - ms-access

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.

Related

How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

I am using a SSIS Data Flow Task to transfer data from one table to another. Column A in Table A contains a number, the last 3 digits of which I want to store in Column B of Table B.
First I'm trying to grab all of the data in Column A and store in a variable via a simple SELECT statement SELECT COLUMN_A FROM TABLE_A. However, the variable stores the statement as a string when I want the result set of the query. I have set the EvaluateAsExpression property to False but to no avail.
Secondly I want to be able to use the result of this query in the Derived Column of my Data Flow to extract the last 3 digits and store the values in Column_B in the destination. The expression I have is:
(DT_STR,3,1252)RIGHT(#User::[VariableName],3)
I want to store this as a string hence the (DT_STR,3,1252) data type.
All I'm getting so far in Column_B of Table_B is is the last 3 characters of the SELECT statement "E_A". There is a lot of useful information on the web including YouTube videos for things like setting file paths and server names as parameters or variables but I can't see many relevant to the specifics of my query.
I have used an Execute SQL Task to insert row counts from flat files but, in this example, I want to use the Derived Column tool instead.
What am i doing wrong? Any help is gratefully appreciated.
I prefer to do all the work in SQL if you aren't doing anything else with that number.
select right(cast(ColA as varchar(20)),3) from tableA
-- you can add another cast if you want it to be an int
use that in an execute sql to result set = single row.
Map that to a variable.
In a derived column in data flow you can set that variable to the new column.
Thanks KeithL thats one solution I will use in future but I found another.
I dropped the variable and in the Expression box of the Transformation Editor did:
(DT_STR,3,1252)RIGHT((DT_STR,3,1252)Column_A,3).
In my question, I failed to cast Column_A from Table_A as a string. The first use of (DT_STR,3,1252) simply sets the destination column as a string so as not to use the same data type as the source which in my case was int.
Its the 2nd use of (DT_STR,3,1252) that actually casts Column_A from int to a string.

Handling Multiple Values in SS Reports

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.

SQL Query on transformed table in SSIS

I have joined 5 tables and done transformation on these tables. Now I got a single table at the end. Now I want to perform sql query on this single table to filter records. But I don't know how to perform simple sql query on this table. I have attached a snap shot which shows the resulting table. How I get this resulting data set as the source? I want to populate my destination after filter out this data.
I am using SSIS 2008.
Click here to see the Table on which I want to perform a simple sql query
SELECT * FROM `first_table`
where `some_column` =
(
SELECT `*`
FROM second_table
WHERE
`some_column2`='something'
LIMIT 1
)
Try this code This will help. You can even use this to connect all those four tables with each other.
From the image you posted, it looks like you have a set of data in the dataflow you're trying to query against. You need to do one of two things at this point. Either you insert the data into a table in the database and use another data flow to query it, or you use use a conditional split (or multicast and conditional splits) to filter the rows down further from there.
Without more detail about what you're actually trying to accomplish, these are the recommendations I can determine.
You could send the rows into a record set destination, but you aren't able to query it like a regular table and you'd need some C#/VB skills to access it to do more than a FOR EACH loop.
Assuming your sql query that you want to run against the resulting table is simple, you can use a script component task. By simple, I mean, if it is of this nature:
SELECT * FROM T WHERE a = 'zz' and b = 'XX' etc.
However, if your query has self joins, then you would be better of dumping the outcome of joining those 5 tables in to a physical table, and go from there.
It appears that query is going to be real straight-forward; in that case using a script component would be helpful.
A separate question: It's advisable to do the sorting at the database level. You are using 5 sort tasks in your solution. Can you please elucidate the reason?

MySQL determining column types basing on a result set

Let's say I have a stored procedure or just a complex SELECT statement. I would like to know what are the types of returned columns. Is it possible in MySQL, or I have to use some programmatic access and read the result values from there?
If you want to know the type of a column you can use the SHOW COLUMNS statement.
Have a look: http://dev.mysql.com/doc/refman/5.6/en/show-columns.html

MySQL wrong output with IN clause and parameter

I'm having a hard time with a query in MySQL.
I'm working with Delphi XE and I'm sending a query with some parameter to MySQL. Here's an example for the query:
SELECT * FROM users u WHERE u.id IN (:idUsers);
The ':idUsers' is the variable that will receive the parameter I send with Delphi, which is a string containing that is formatted like this, ex.: 1,2,3
The problem is that with this string, I receive only the first user (id = 1). From what I can see, its just like MySQL is adding some quote ('') at the beginning and at the end of the string I send, like if it was '1,2,3' instead of 1,2,3. I've tried this select :
SELECT * FROM users u WHERE u.id IN ('1,2,3');
and it does indeed return only the first user..
I had a function in MSSQL that was splitting the string I sended and returning a temporary table, but we recently switched to MySQL, and from what I read, MySQL doesn't allow returning a table.
Anyone has a clue on how to solve this problem? I've been scrapping the Web for an answer and haven't found one for this specific problem.
Thanks for the help.
Parameters don't work like that; they have no idea that what you're trying to provide is a comma-separated list of values. If you set ParamByName('WhatEver').AsString or Value, it thinks you mean a quoted string that contains everything that belongs in that parameter. So it's doing exactly what it appears to - it's passing IN ('1, 2, 3'), instead of the intended IN (1, 2, 3).
You'll need to either go back to parsing it out yourself and using a temp table, or build the WHERE clause dynamically and concatenating it before executing the query. (Actually, you could build the query dynamically, creating a new parameter for each of the items in the IN clause, and then loop through and assign values to each of those dynamically created parameters, but that gets very ugly very quickly.)
If you have a variable number of parameters in the IN clause, you could build a temporary table in MYSQL (which only contains a column called ID), load the parameter values into the temporary table and then do something like this:
SELECT * FROM users u WHERE u.id IN (SELECT ID FROM TEMPTABLE);
As long the TEMPTABLE only contains the values you want to query, the table space scan is acceptable.
You can then have a variable number of values. You could also make the table permanent, and store the parameters. Add a column for the User, and each user can store their individual parameters.