How to update a field in a table basing on a field from another table - sql

So I have two tables, InTheMiddle and Table2.
The 1st one contains Name column, the 2nd one contains ID and Name columns.
(they also have other columns, but irrelevant to this question)
I want to update/change the names in the 1st table with the IDs from the second one, but of course only when the names match and there's only one name (so there aren't any semicolons ";").
Here's a query that I tried to use, but Access tells me "Operation must use an updateable query"
UPDATE InTheMiddle
SET [Name] =
(SELECT [Table2].ID
FROM [Table2]
WHERE InTheMiddle.[Name] = [Table2].[Name]
AND InTheMiddle.[Name] NOT LIKE "*;*"
);

Possible duplicate to Operation must use an updatable query. (Error 3073) Microsoft Access, in which the accepted answer suggests that the only way to avoid that error message is to use temporary tables.
Besides the accepted answer, this answer suggests a workaround to code looking like yours

Related

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.

Selecting all Columns which have not already been mentioned

First off: I am reverse engineering SQL queries and as a result I have a bunch of candidate queries, which all contain a designated 'entity' column in their SELECT clause.
The problem is: I want the entity column to be the first column of my result and let all the other columns follow, so basically I want something like this
SELECT A.entity, *
FROM A
...
...but without the duplicate entity column resulting from the *-operator.
EDIT: I also do not know all of the column names, generally only the entity column name is known.
Thanks for helping me

T-SQL - Using Column Name in where condition without any references when having multiple tables that are engaged using multiple joins

I am a newbie for T-Sql, I came across a SP where multiple tables are engaged using multiple joins but the where clause contain a column field without any table reference and assigned for an incoming variable,like
where 'UserId = #UserId'
instead - no table reference like
'a.UserId = #Userid'`
Can any please do refer to me any material that clears my mind regarding such issue.
If the query works it means that there is only one Column with the name UserId, if there are multiple columns with the same name you have to reference the table too.
If you don't specify the table reference you will get
Ambiguous column name 'UserId'. error
Which means there are more then 2 tables with a column name UserId.
Anyway, always try and use the reference table.

Can alias in SQL be used within the same table?

I have difficulty understanding alias. Can alias in SQL be used within the same table?
In a query, you can use multiple aliases for a single table:
SELECT alias1.Name, alias2.Name
FROM table as alias1
INNER JOIN table as alias2
ON alias1.ChildId = alias2.Id
In the code above I am aliasing table as alias1 and alias2. It is the same table, with 2 different aliases.
Not sure I understand your question completely...
A good read on aliases # http://www.w3schools.com/sql/sql_alias.asp
http://www.sqltutorial.org/sqlalias.aspx
There are 2 kinds of aliases, one for tables and one for columns. Aliaes are used as a way to make your sql code more readable. It can give meaningful names to column and table names that might be long and/or confusing.
check the w3schools breif description and examples for SQL Alias
You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.
Which alias as you referring to: 'table alias' or 'column alias'?
In the SQL-92 Standard, the vernacular 'table alias' is referred to as a correlation name. A correlation name much be unique within its scope. The actual wording is as follows:
An identifier that is a correlation
name is associated with a table
within a particular scope. The scope
of a correlation name is either a
select statement: single row,
subquery, or query specification.
Scopes may be nested. In different
scopes, the same correlation name
may be associated with different
tables or with the same table.
In the SQL-92 Standard, the vernacular 'column alias' is referred to (rather wordily) as an as clause that contains a column name. There is no general condition that the same column name shall not be specified more than once in column lists (but there are context-specific restrictions e.g. a view column list). In fact, SQL's allowance of duplicate column names is often cited as a fatal flaw as regards being turly relational.

SQL Server join and wildcards

I want to get the results of a left join between two tables, with both having a column of the same name, the column on which I join. The following query is seen as valid by the import/export wizard in SQL Server, but it always gives an error. I have some more conditions, so the size wouldn't be too much. We're using SQL Server 2000 iirc and since we're using an externally developed program to interact with the database (except for some information we can't retrieve that way), we can not simply change the column name.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2 ON table1.samename = table2.samename
At least, I think the column name is the problem, or am I doing something else wrong?
Do more columns than just your join key have the same name? If only your join key has the same name then simply select one of them since the values will be equivalent except for the non-matching rows (which will be NULL). You will have to enumerate all your other columns from one of the tables though.
SELECT table2.samename,table1.othercolumns,table2.*
FROM table1
LEFT JOIN table2 ON table1.samename = table2.samename
You may need to explicitly list the columns from one of the tables (the one with less fields), and leave out the 2nd instance of what would be the duplicate field..
select Table1.*, {skip the field Table2.sameName} Table2.fld2, Table2.Fld3, Table2.Fld4... from
Since its a common column, it APPEARS its trying to create twice in the result set, thus choking your process.
Since you should never use select *, simply replace it with the column names of the columns you want. THe join column has the same value (or null) in both sides of the join, so only select one of themm the one from table1 which will always have the value.
If you want to select all the columns from both tables just use Select * instead of including the tables separately. That will however leave you with duplicate column names in the result set, so even reading them out by name will not work and reading them by index will give inconsistent results, as changing the columns in the database will change the resultset, breaking any code depending on the ordinals of the columns.
Unfortunately the best solution is to specify exactly the columns you need and create aliases for the duplicates so they are unique.
I quickly get the column headings by setting the query to text mode and copying the top row ...