Copy SQL column from one to another maintaining ID - sql

I am looking to copy SQL long binary data from the "photo" column to the "id_photo_c" column. Both columns are in separate tables. A got a query to show exactly what I need, but unfortunately you cannot copy & paste outputs from the "Results Pane" of mssql.
I cannot copy the entire table from one to another, the new database has more rows (including some duplicates).
http://i.imgur.com/tmRpMh2.png
Here is the code:
SELECT
[GroupTables].[dbo].[VisitorsAdvanced].[RecordNumber], [
GroupTables].[dbo].[VisitorsAdvanced].[photo],
[SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c],
[SugarCRM].[dbo].[contacts_cstm].[id_photo_c]
FROM
[GroupTables].[dbo].[VisitorsAdvanced], SugarCRM].[dbo].[contacts_cstm]
WHERE
[GroupTables].[dbo].[VisitorsAdvanced].[RecordNumber] = [SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c];
It seems like such a simple task (would take two clicks in Excel) - but I can't seem to get it to work.
This isn't a duplicate question. I've seen similar questions on here, none of which describe how to simply copy data from one column to another.
Thank you.

Unless I'm missing something here, it seems like a simple update statement is all you need:
UPDATE [SugarCRM].[dbo].[contacts_cstm]
SET [id_photo_c] = [SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c]
FROM
[GroupTables].[dbo].[VisitorsAdvanced]
INNER JOIN [SugarCRM].[dbo].[contacts_cstm]
ON
[GroupTables].[dbo].[VisitorsAdvanced].[RecordNumber] = [SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c];

Isn't this as straight-forward as updating the data in the contacts_cstm table?
UPDATE contacts_cstm
SET id_photo_c = photo
FROM VisitorsAdvanced
WHERE RecordNumber = xxx_id_number_c

Related

T-SQL selecting all col to update in merge

Maybe that stupid question, but I don't know how to describe my problem to uncle google.
I have two simple tables with 3 rows: ID, Name, SomeVal;
Now I want update them with merge, and that's simple:
MERGE Locations T
USING Locations_2 S ON T.ID=S.ID
WHEN MATCHED THEN
UPDATE
SET
T.Name=S.Name,
T.SomeVal=S.SomeVal;
Ok, that works, but I wrote every column name by hand. So, when I want to update for example table with 30 columns, writing everything by hand will be painful. So, is there any option to update every column in the table no matter how many columns it has?
I tried "*". It is UPDATE SET T.*=S.*, and that didn't work.
What I normally do when I want to be time efficient (or lazy, depending on your view) is this:
Run this command:
sp_help TableName;
Copy/paste the first column, then hold down Alt whilst dragging the mouse cursor in front of the column names. This way I can then type one comma in front of all the fields. Then I do the same after the field name except with an = sign.
That's the only shortcut I can think to help you, and has saved me hours of typing over the years.
No. Merge statements are very verbose. You can hack a shortcut by scripting your table as CREATE, copy the column names using SHIFT + ALT to select a large swath of them, then paste them into the MERGE query, hit space, add the = sign, then paste again.
The statement is one direction, (i.e., you can't update S with T; only T can be updated) so you don't strictly need to provide the source/destination alias for each column.

Table Query Selecting one field from multiple tables

I am trying to write a query that will select the records from multiple tables whose field pass or fail = fail. Normally I wouldn't have a problem writing this its pretty straight forward. My problem is that I normalized the data of the fields. The field I am trying to select is called PassOrFail, however since it occurs in multiple tables I normalized the field and changed it to "Tablename"PassOrFail i.e OvenPassOrFail. My question is how do I select this field for all the tables that have this field? How would I type the WHERE and FROM expressions. Would I look something like this?
SELECT*
FROM Oven, FirstPunch
WHERE Oven.OvenPassOrFail,FirstPunch.FirstPPassOrFail = "Fail";
Or would I have to undo the normalization of the fields to get it to work? Any tips or suggestions to point me in the right direction would be appreciated.
I think I figured it out, All I had to do was add an AND statement and it worked.
SELECT *
FROM Oven, FirstPunch
WHERE OvenPassOrFail = "Fail" AND FirstPPassOrFail = "Fail";

Are there alternatives to nested LEFT JOIN's in VBA for Microsoft Access?

I am attempting to load a bajillion excel spreadsheets into a single, normalized database. My goal is to duplicate the structure of each original excel spreadsheet but convert the cell entries (the majority of which repeat frequently) into foreign keys before appending to a master table. I've found a process that ALMOST works but I'm running into some problems with an SQL query. I'm fairly savvy in VBA but quite new to databases and SQL, so while I'm confident I can dynamically generate the string necessary for a DoCmd.RunSQL, I'm having trouble with the query structure.
Here's what I'm trying to do:
After importing an excel spreadsheet into an Access table ("tbl_b"), I convert all entries in "tbl_b" to a corresponding foreign key before appending to another, preexisting table ("tbl_A").
For illustrative purposes, say the database contains the following tables:
tbl_a
field1_foreignKeys | field2_foreignKeys
tbl_b
field1_import | field2_import
tbl_field1_primaryKeys
field1_primaryKey | field1
tbl_field2_primaryKeys
field2_primaryKey | field2
I can successfully use the following query to achieve my goal:
INSERT INTO tbl_a (field1_foreignKeys, field2_foreignKeys)
SELECT tbl_field1_primaryKeys.field1_primaryKey, tbl_field2_primaryKeys.field2_primaryKey
FROM (tbl_B LEFT JOIN tbl_field1_primaryKeys ON tbl_b.field1_import = tbl_field1_primaryKeys.field1) LEFT JOIN tbl_field2_primaryKeys ON tbl_B.field2_import = tbl_field2_primaryKeys.field2
This works perfectly for an excel spreadsheet with two columns, but unfortunately, the spreadsheets I'm working with contain 26 columns, each of which must be converted to foreign keys before appending. Access is consistently crashing when I nest more than eight LEFT JOIN's, so I need a different solution. Hopefully this can be done and I'm missing something obvious :p
Any takers?
........................................................................
UPDATE
Regardless of whether or not I'm using the best data structure for my purposes (this database dilettante is optimistic), Don George's suggestion put me on the path to a working solution:
It occurred to me it was unnecessary to preserve "tbl_b" in its original condition, so I looped through each field with a dynamically generated version of the update query below:
UPDATE tbl_b LEFT JOIN tbl_field1_primaryKeys
ON tbl_b.field1_import = tbl_field1_primaryKeys.field1
SET tbl_B.field1_import = tbl_field1_primaryKeys.field1_primaryKey
After updating tbl_b I appended the whole thing to tbl_a all in one go.
Perhaps inelegant, but I thought I'd post my solution in case any other poor soul finds his/herself in an identical situation.

Retrieve results from a batch of SQL queries in Pentaho or Postgres?

I'm still relatively new to SQL and Pentaho.
I've pulled a table with two different IDs and need to run a query for each specific instance.
For example,
SELECT *
FROM Table
WHERE RecordA = 'value in column A'
AND RecordB = 'value in column B'
I need the results back, either appended to new columns in the original table or part of their own text file output.
I was initially looking at using a formula for this inside of Pentaho, but couldn't quite figure it out. Since I have the query written I threw it into Excel and got the concatenated results (so a string of 350 or so queries that I need to run). I'm just not sure how to accomplish this - I tried the Execute SQL Script inside of Pentaho but it doesn't seem to do output?
Any direction would be useful. I've searched a little but have come up short so far, possibly because I am still pretty new to this platform.
You can accomplish this behavior in a lot of ways, with a "Database Lookup" step for example, but I usually do that in a quite easy way and here is a example for your tests, I hope it helps.
The idea here is to have two Table input steps, the first one will fetch the IDs we want to look at. For example you may use a SQL query similar to note on the left. The result will be a 1 column stream of rows.
Next we have a Table Input that reads the rows received and executes it's query for each row. I'll add a screenshot with the options that I selected.
What it does is replace a placeholder '?' with the data that is received. If you need two columns use two '?' but remember that it will replace the first one with the first column and the second one with the second column
And you are good to go. Test it a couple of times and good luck.
And the config for the second table input.

Query to select items from a list and update table

I've got a query that I ran against a table of stock items to change a field from false to true so that a label would be printed out as per the below.
UPDATE [StockDB].[dbo].[StockItem]
SET [UseDescriptionOnDocs] = 0
Where [UseDescriptionOnDocs] = 1
I have since been told that all of the items do not need this and to only do it for the ones in a list of 1150. From that I need to modify the query, something which I am really struggling with!, so it can only do it per item which are in the format of R368/WASHER/M4/ZINC/PL etc. I thought I may be able to do it using LIKE but it will only do one line? If I could get it to query a list from Excel or CSV that would be great.
Any pointers would be good as I'm an absolute novice with this once you get past the simple stuff like the above!
Thank you.
With MySQL (and others) you can load data from a file to a table. See http://dev.mysql.com/doc/refman/5.1/en/load-data.html to understand and for exemple.
Once you have your items loaded into a SQL table, you just have to do an update + join query. See http://dev.mysql.com/doc/refman/5.1/en/update.html to understand and How can I do an UPDATE statement with JOIN in SQL? for exemples.