How to use UPDATE IIF - sql

I am using SQL in MS-Access.
I created some queries that analyze if a code is present in two different excel spreadsheets and generate a new table, informing "yes" or "no" for each comparison.
However, sometimes, a certain code appears in just one table, but after a while (when someone update the excel spreadsheets), that code appears.
I already have a query that makes this comparison, but it doesn't work for those codes that are introduced in the table AFTER the comparison has already been made.
So, I would like to create a query that uses an UPDATE, checking if the given code is now in both tables, and if it is, it would update the comparison column of my table.
This is the query I created for this, but it is not working:
UPDATE
comparationTable
SET
col_comp = IIf(spreadSheet1.code = spreadSheet2.code),"Yes","******No******")
WHERE
code1 = code2;
Note: code1 and code2 are columns that only show the code coming from excel spreadsheet1 and excel spreadsheet2, respectively.
Edit: Here's the images of the two spreadsheets (sp1, sp2) and the comparation table:

Too many parenthesis - must always be in pairs. Field names are not correct. The WHERE clause is not appropriate - records without value in Code2 will not update with any value.
UPDATE comparationTable SET col_comp2 = IIf(code1 = code2, "Yes", "******No******")

Related

sql query expression

The table retrieves data from the table "Group" with 2 columns First key "GroupNumber" and GroupName"I am trying to create a sql query in Microsoft Access from a table with two columns that will works as below:
Based on the user's selection from the first column the query has to return the value of the second column, and based on the user's selection from the second column the query has to return the value of the first.
Any idea how is possible to express the user's selection in sql? Ty
Some more data would be useful. AT this moment, it is hard to understand what it is exactly you are trying to do.
However, you mention two different selecting actions that need to be done by the user, so two SELECT statements would work.
To return the value of the second column based on user selection of the first: SELECT second_column_value FROM table WHERE first_column_value = value_selected_by_user
To return the value of the first column based on user selection of the second:SELECT first_column_value FROM table WHERE second_column_value = value_selected_by_user
or you can use drop-down lists, if the situation allows
Try providing more data to get more useful answers.

How can I use record content from one table to update another, without a join option?

I need to update values in a column in a specific table that exists in all our databases, but do not know the name of the column as it is user-generated.
I have two tables: one of them with user-generated columns tab_Case. In this table there is a column attachment that I need to update if the following condition applies: WHERE attachment = '0' (if true then the value needs to be changed to NULL).
In its simplest form the update query would look something like this:
UPDATE tab_Case
SET attachment = 'NULL'
WHERE attachment = '0'
This table is used in all our databases, so I need to write a query general enough to be usable across all of them.
The problem is that as the table uses user-generated columns, I have no way of knowing what the exact name is of concerned column-type, and exactly how many of those columns exist in the table.
I can, however, find out the type of the column by looking it up in another table tbl_itemPart inner joined with tbl_ValueType, like this:
SELECT ip.DbReference, ip.DbTableName, vt.ValueDescription
FROM tbl_itemPart ip
INNER JOIN tbl_ValueTypes vt ON ip.ValueTypeId = vt.ValuetypeId
WHERE vt.ValueDescription = 'file'
AND ip.DbTableName = 'tab_Case'
The columns I need are always of type 'file' and as the tab_Case table is referenced in tbl_ItemPart it is easy to find out 1) if any columns of type 'file' exist in this table, and 2) when true, what their respective names are.
So great, now I know the names of the columns that I need to potentially update. But, this is where I get lost: how do I use that information in my update query?
How do I write a script that first checks the tbl_itemPart for existence of any columns in tab_Case of type ' file', then retrieves the actual values (= names of those columns) from the DbReference column in tbl_itemPart and then finally uses those values in the update query for tab_Case?
Remember that this scripts needs to automatically do this for each of our databases, so I do not want to look up column names manually per database and then adjust my script accordingly for each of the databases.
I am very new to programming, and may be missing something very obvious, but so far I haven't been able to find a solution, or any relevant information to help me on my way.

how can I link rows of a SQL table for some columns but not others?

I have a table of values in excel that I want to put into sql as a lookup table. the table looks like this:
the sql table looks like this:
having this in SQL, I now want to never use the excel file ever again.
I also need the ability to change the parameters, but some of them in the excel file were linked by merging the cells and thereby shared the same value, if it changed for one it changed for all.
for example: when I change Parameter B for Product 1, I need it to change it for Products 2, 3, 4, and 5 because they share the same cell in the excel table. And if I change parameter A for Product 2, It only changes for product 2 and 3. I am looking for a SQL Query solution. I have the ability to change the table structure as well.
Here goes my example query:
Update [Table] Set [Parameter_A] = '{new_parameter_tag}'
Where [product] = '{selected_product_tag}'
except I want to have the Where include all the rows that share the same cell from the excel table.
I want to be able to update the SQL table for multiple products at a time based on if they share the same cell for that parameter in the excel file.
here is my initial guess at an answer:
Select [{Parameter}],[Product],[Extra_column]
From [Table]
Where [Product] = '{selected_product}'
this returns one row and [Extra_column] that contains a grouping number shared by others in the same cell grouping. this then gets stored as {Extra_column}. then:
Update [Table] Set [{Parameter}] = '{new_parameter_value}'
Where [product] = '{selected_product}' Or [Extra_column] = '{Extra_Column}'
this requires two queries and also means that i need twice as many columns as i had before. I am looking for something a little more elegant.
This is SQL Server 2012 and the {} indicate a value that I am passing in form a script.
I ended up doing something similar to what I had above, the user enters the group they want to edit (it's pretty easy to pick out which one you want when viewing the table) as:
Update [table]
Set [{Parameter}]={NewValue}
Where [Extra_Column] = '{Extra_Column}'
I had to add three columns for the grouping indexes but over 43 parameters that doesn't add much to my table size. I did not take into account the fact that if I change a single parameter for a single product that would remove it from the "group" essentially for just that parameter, and later I would overwrite the changed value if I do a group change for that parameter. I could add in a check to only change values that match within that group but either way the user will have to be smart about what they do. luckily, they can see the table before they change it.

Update existing database values from spreadsheet

I have an existing MSSQL database where the values in some columns need updating according to a spreadsheet which contains the mappings of old data and new data.
The spreadsheet is like this:
| OLD DATA | NEW DATA |
RECORD | A | B | C | D | A | B | C | D |
1 |OLD|OLD|OLD|OLD|NEW|NEW|NEW|NEW|
2 |OLD|OLD|OLD|OLD|NEW|NEW|NEW|NEW|
Where ABCD are the column names, which relate to the database, and OLD / NEW relates to the data.
Thus for each line (approx 2500 rows)
The database values that match OLD in each column, need to be changed to NEW
My current thoughts are to do it in a similar way to this:
SQL Statement that Updates an Oracle Database Table from an Excel Spreadsheet
Essentially getting Excel to formulate a list of replace statements, though this feels like a horribly convoluted way to deal with the problem!
Is there a way to have SQL cycle though each row of the spreadsheet, check all records for a=old, b=old2, c=old3, d=old4 and then replace those values with the appropriate a=new, b=new2, c=new3, d=new4?
You shouldn't need to loop through each row in the spreadsheet. You can use the OPENROWSET command, like in the answer you linked to, to load the spreadsheet data into a sort of temporary table. You can then run a regular UPDATE statement against that table.
It would look something like this
UPDATE YourTable
SET YourTable.A = ExcelTable.NewDataA,
YourTable.B = ExcelTable.NewDataB,
YourTable.C = ExcelTable.NewDataC,
YourTable.D = ExcelTable.NewDataD
FROM YourTable
INNER JOIN OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\foldername\spreadsheetname.xls;',
'SELECT column1name, column2name, column3name, column4name
FROM [worksheetname$]') AS ExcelTable
ON YourTable.ID = ExcelTable.ID
WHERE (YourTable.A = ExcelTable.OldDataA
AND YourTable.B = ExcelTable.OldDataB
AND YourTable.C = ExcelTable.OldDataC
AND YourTable.D = ExcelTable.OldDataD)
Looks like Jeff got you the answer you needed, but for anyone looking to update a database from a Google Sheet, here's an alternative, using the SeekWell desktop app. For a version of this answer with screen shots, see this article.
Get the right rows and columns into the spreadsheet (looks like #sbozzie already had this)
Write a SQL statement that SELECT 's all the columns you want to be able update in your sheet. You can add filters as normal in the WHERE clause.
Select 'Sheets' the top of the app and open a Sheet. Then click the destination icon in the code cell and select "Sync with DB"
Add your table and primary key
In the destination inputs, add your table name and the primary key for the table.
Running the code cell will add a new Sheet with the selected data and your table name as the Sheet name. Please note that you must start your table in cell A1. It's a good idea to include an ORDER BY.
Add an action column
Add a "seekwell_action" column to your Sheet with the action you'd like performed for each row. Possible actions are:
Update - updates all columns in the row (unique primary key required)
Insert - adds the row to your database (you need to include all columns required for your database)
Sync - An Update action will be taken every time the query runs, on a schedule (see "5. Set Schedule" below)
Complete - status after the schedule has run (see below) and the actions have been taken. The new data should now be in your database. Note that 'Sync' actions will never show complete, as they run every time the schedule runs. To stop a 'Sync' action, change it manually.
Set Schedule
To execute the actions, select the clock icon at the top of the application, indicate the frequency and exact time, and click 'Save.' You can manage your schedule from the inserted 'RunSheet' (do not delete this sheet or you will need to reset your schedule) or from seekwell.io/profile. If you need to run the actions immediately, you can do so from /profile.
Gotchas
You need to start your table in cell A1.
Snowflake column names are case sensitive. Be sure to respect this when specifying the primary key, etc.
If your server is behind a firewall, you will need to whitelist SeekWell's static IP address to use scheduling. See more about whitelisting here.

How to filter by 2 fields when loading data into an access database table from an excel spreadsheet

OK, here is my problem, without being too specific for reasons of being fired for putting company practices on the internet.
There are spreadsheets made. These are uploaded to the database. I need to filter out duplicates from uploading. The only way to do this is by making sure that for each and every entry that two fields aren't the same as an entry already in the database. As just becasue one field is the same does not mean its a duplicate. There are two specific fields lets call them FLDA and FLDB that both must match up to an entry in the database already. I can filter by one field already. I'm thinking this has to be a subquery but I'm not sure how to apply it.
This is hard to decribe. Just ask if your not sure what I mean.
I had a similar problem. My solution was to:
import into a staging-table.
delete the duplicates
copy what's left over into the live table
It's a little BFI, but it just plain works.
Would a query suit? For example:
INSERT INTO ToUpdate ( Field1, Field2 )
SELECT e.H1, e.H2
FROM (SELECT * FROM [Sheet1$] IN '' [Excel 8.0;HDR=YES;IMEX=1;database=C:\Docs\LTD.xls]) As e
LEFT JOIN ToUpdate ON (e.H2 = ToUpdate.Field2) AND (e.H1 = ToUpdate.Field1)
WHERE ToUpdate.Field1 Is Null AND ToUpdate.Field2 Is Null
How are you loading them into the database? Is this with your own code to read the Excel files? You can read the Excel files using ADO/ADO.NET with the right connection string. Then you could read the data using a query that would weed out the dupes.
Building on CodeSlave's answer, Access provides a Find Duplicates Query wizard that can help you easily build the query to weed out the duplicates.
Another approach would be to set up an identity on FLDA and FLDB. This will prevent duplicate entries from even getting written to the table. Of course you will also need to catch the exception that is thrown when the insert operation fails.
Is there a field FLDC that would be different for identifying duplicates? I presume there must be, as otherwise having (FLDA,FLDB) as a unique or primary key would solve your problem immediately.
Assuming there is such a field, you could try something like this:
SELECT T1.FLDA, T1.FLDB, T1.FLDC
FROM Table1 T1, Table1 T2
WHERE T1.FLDA = T2.FLDA
AND T1.FLDB = T2.FLDB
AND T1.FLDC <> T2.FLDC
The downside here is that both the original and the duplicate will be returned by something like this. If you only want to see the duplicates, you will probably have to figure out a way to identify an 'original' row and add another WHERE clause or two for this.
If you can get a query that gives you just the duplicate rows and not the originals, it should be pretty easy to change it to a DELETE query.
To avoid duplicates on imports:
1 - If there's not already a primary key on the table, put one on FLDA and FLDB (together). If there's already a primary key that's not FLDA and FLDB (together), place an index on the table on these two fields, unique yes, ignore nulls no.
2 - You can import from the spreadsheet to the table with the wizard or with a query. If you do it with the import spreadsheet wizard, you'll see this message before the import starts:
"DB name was unable to append all the data to the table.
"The contents of fields in 0 records were deleted and (xx) records were lost due to key violations. (These lost records were duplicates, so no real loss there.) ... Do you want to proceed anyway?"
Click yes to import the rows from the spreadsheet. No duplicates will be imported.
Or, to use a query for the import, paste this into a new query in sql view (menu: Insert > Query > Design View, Close button; menu: View > SQL View.)
INSERT INTO tblInput
SELECT XLS.*
FROM tblInput AS T RIGHT JOIN
[Excel 8.0;IMEX=1;HDR=Yes;DATABASE=c:\data.xls;].[Sheet1$] AS XLS
ON T.FLDA = XLS.FLDA AND T.FLDB = XLS.FLDB
WHERE ISNULL(T.FLDA) AND ISNULL(T.FLDB);
Change the path, c:\data.xls, to your path, Sheet1$ to your sheetname, tblInput to your table name, and FLDA and FLDB to your column names. If the spreadsheet doesn't have headers (column names) change HDR=Yes to HDR=No
I did this by using a delete query and then using Select from Table 1 Group by X Having Y, Z, A. And then I put run query button for front end users.
Cheers for all your help.