I have a field in a table(1) that can be filled with two different fields from another table(2). Both of the tables are linked through their Primary Key. How do I create a Update Query that uses a parameter to define which field will be used at the update?
In this example, tblSource has 2 date fields, date1 and date2. The parameter WhichDate is used with the Switch() function to determine which of those 2 fields is used to update date_assigned in tblTarget.
PARAMETERS WhichDate Long;
UPDATE tblTarget AS t INNER JOIN tblSource AS s ON t.id = s.id
SET t.date_assigned = Switch(WhichDate=1,date1,WhichDate=2,date2);
I don't think you get to do this directly in MS Access. You could mimic this behavior if you are using VBA code, but my best (possibly incomplete) understanding is that you won't be able to use a Parameter to reference a Database object.
With VBA (or any other client code) you could set up a method which accepts a method parameter that establishes which column to update, and then assemble the appropriate SQL statement from there.
Related
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.
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.
I am trying to Update a column in my table Inputcounts called concatenate off of a query called InputConcatenates that has a column also called concatenate. I am running an update query with the field name as concatenate the table name as InputCounts and the update to field as [InputConcatenates].[Concatenate]. But every time I run the query it pulls back that 0 records will be updated. Is my syntax wrong possibly?
Update Query SQL:
UPDATE InputCounts INNER JOIN InputConcatenate
ON InputCounts.CONCATENATE = InputConcatenate.CONCATENATE
SET InputCounts.CONCATENATE = [InputConcatenate].[CONCATENATE];
InputConcatenate Query SQL:
SELECT InputCounts.FLEET, InputCounts.AMMs, [FLEET] & [AMMs] AS CONCATENATE
FROM InputCounts;
You reported this query accomplishes what you want ...
UPDATE InputCounts
SET CONCATENATE = [FLEET] & [AMMs]
WHERE CONCATENATE Is Null;
That may be fine. However CONCATENATE is not updated until you execute the UPDATE, and does not get updated (after having previously received a value) in response to changes in FLEET or AMMs
Decide whether CONCATENATE really needs to exist as a field in your table. You could use a query to derive it whenever you need it:
SELECT *, FLEET] & [AMMs] AS CONCATENATE
FROM InputCounts;
With the query, CONCATENATE will always be up to date.
If your database is ACCDB format and your Access version is >= 2010, another possibility is to make CONCATENATE a "calculated field" type in the table's design:
If you prefer CONCATENATE be Null whenever FLEET or AMMs is Null, change the field's Expression property to [FLEET] + [AMMs]
The advantage of a calculated field is that Access automagically updates its value without further effort (like executing an UPDATE) from you.
A disadvantage is that you can't index a calculated field. That means it's not suited for joins, WHERE criteria, ORDER BY, etc. You'll have to decide whether it's a reasonable fit for your application. :-)
H, I have a parameter 'Client' as drop down in SSRS report. My requirement is to select all the records for all the clients if i don't select any parameter value and if i select particular value in the drop down,i need to display records for the that particular client.
I am getting the list of clients as a input from query.How can i add option select all by default.
Thanks in advance
You can do this a few ways...
Check the "Allow Multiple values" on the general tab of the Parameter Properties, go into the Available values and select the dataset you are using to get the values, make sure the the column that contains the actual data to search on is what you select for the VALUE field...(with this one - make sure your query eliminates the NULLs for the Value field) then you will have a drop down with all the values and it will add Select All...
Then in the Default Values tab, you can hook to the same query and select the VALUE field again... (as long as there aren't any NULLS) You may need to do tweaking depending on your query and values...
or
You can check the "Allow Null Value" on the General tab in the Parameter Properties, then in the Default Values - select "Specify Values", then Add, and (null) pops in there automatically...
Then call a stored procedure where the parameter defaults to NULL and if you pass NULL or don't pass in a string of values, your query will return all (not sure how you'd implement this if your query is embedded in the report... I try to do all of mine in Stored Procedures..)
Add "All" to the results of the query that returns a list of clients for the drop-down and make it the default selection. Then handle it in your main stored procedure that if "All" was selected, you don't filter by clientId, and just get all clients.
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.