I have created a lookup transformation to search 2 table col for matching data and then insert another column data from matching data into the other table. My problem is how can i insert the matching data into that table i have shown in the shot.
I managed to insert the lookup matched cols by inserting OLEDB Command and Writing update query
Update dbo.city set fdocode = ? where id = ?
and then Map the col i want to insert to first parameter and id to second parameter.see shot for more clarity.
Related
The following is an example to better explain my scenario. My database table has following columns
Column -1: Operating_ID (which is the primary key)
Column -2: Name
Column -3: Phone
Column -4: Address
Column -5: Start Date
Column -6: End Date
The values for the columns 1,2,3,4 come from an extract and this extract is pushed to the database daily using SSIS data flow task
The values for the columns 5 and 6 are user inputted from a web applicxation and saved to the database.
Now in SSIS process instead of throwing violation of primary key error, i need to update Columns 2,3,4 if the primary key i.e column 1 already exists.
First i considered replace but that deletes the user inputted data columns 4,5.
I would like to keep the data in columns 4,5 and update columns 2,3,4 when column 1 already exists.
Do a LOOKUP for Operating_ID. Change that lookup from "FAIL ON NOT FOUND" to "REDIRECT ROWS TO NO MATCH"
If match not found, go to INSERT
If match found, go to UPDATE. You can run OLAP commands to update, but if it is a large set of data, you are better off putting into a table, and doing an UPDATE with a JOIN
This is what I would do. I would put all the data in a staging table. Then I woudl use a data flow to insert the new records and the source of that dataflow would be the staging table with a not exists clause referencing the prod table.
Then I would use an Execute SQL task in the control flow to update the data for existing rows.
I have a mysql table with several columns. on some conditions I run a script that duplicates the row.
This is done via these steps:
select all columns from the row to be duplicated
on the resulting array change two values out of 30 (new info are built on the fly using php scripts and are unique for the row). these values can be created any time during the update process
insert the new row with the usual insert into... that make me list again all the 30 fields and values
My question is: is there a way to change this script into:
create the new values
run a single query that will duplicate the row and at the same time update the values while duplicating?
So that i don't need to manipulate the array in php and I run just one query instead of two?
You can do it with a single query, but you will need to list all the fields:
INSERT INTO your_table
SELECT
NULL, #in place of auto-increment column (if any)
'some value for the field you want to change',
'some value for another field you want to change',
not_changed_field1,
not_changed_field2,
...
FROM your_table
WHERE <row has to be duplicated>
I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.
I am trying to create a calculation logic using MySQL tables.
Data from two table is processed using a stored procedure and a set of values is generated.
These values are part of a column of output table.
I have to run different procedure to generate output for each column in output table
Now if I create insert query for each row it will have large number of inserts for each column. Can I insert a set of values into a table column in one go? assuming other columns can be NULL.
INSERT INTO tableName(columnName)
VALUES ('baz'),('foo'),('bar'),('baz'),('baz'),('baz'),('baz');
etc as u like..
See this: Bulk insert into table with one single query
The insert can be done for one column rest can be NULL if remaining columns are nullable.
But next time for the remaining columns the Insert will not work for the existing rows. If You want to update the existing rows then you need to fire the update query.
Assuming col1 and col2 are nullable
If you want to insert in col1 keeping col2 null insert will work
If you want to insert in col2 keeping col1 null insert will work
what if I wanted to update the records in the table by altering values in one of the columns?
I have records in the table that have one column empty(null values). I want to change these values and insert values from another table into those records.
Basically I have a table with one column empty. I do not want to append to the end of the table but start inserting from record 1.
For the existing records, you would have to use UPDATE to update that one column, WHERE thatColumn IS NULL.
Shouldn't the values in that column have some relation to the rest of the record? I could understand initializing the existing records to a non-null value, or using an UPDATE query to populate data from another table in that column, but all related to the original row...
UPDATE old SET old.badColumn = new.newData
FROM oldTable old
JOIN newTable new on old.someID = new.someID
This would find the related data in newTable matching oldTable, and update the badColumn to some data from newTable... let me know if you need more help.
See the "Using the UPDATE statement with information from another table" section from this page of SQL Server Books Online.