I currently have a database with two tables in it one hold lets say job details and the other company details. The "company_name" exists in the "Jobs" table which will have a matching entry in the "Companies" table under the field of "name". I want to basically set the field in the "Companies" table of "comp_id" to be the value of the field "id" in the "Jobs" table, WHERE the "name" in the "Companies" table is equals to the "company_name" in the "Jobs" table.
I have create the query below which i believed should work however it returns no rows affected?? can anyone please help me with this?
UPDATE `jobs`, `companies`
SET `comp_id` = 'companies.id'
WHERE ('companies.name' = 'jobs.company_name')
Thanks
This condition:
WHERE ('companies.name' = 'jobs.company_name')
is one problem (unless this is a copy & paste error during posting)
You are comparing two string literals there (and of course as they are not the same, you will never update anything).
The reason is you are using single quotes which denote a string literal. To quote column names you either need to use double quotes or the backticks you had used before (assuming you are on MySQL).
Reading your question you are only updating one table not two?
I want to basically set the field in
the "Companies" table of "comp_id" to
be the value of the field "id" in the
"Jobs" table, WHERE the "name" in the
"Companies" table is equals to the
"company_name" in the "Jobs" table.
All you want to do is set the comp_id column in the companies table to the value of the id column in the jobs table where the name and company_name columns are the same?
To do this using MSSQL you would do something like this:
Update c
Set c.comp_id = j.Id
From dbo.Companies c
Join dbo.Jobs j on c.Name = j.Company_Name
You can update only 1 table in an UPDATE statement in SQL Server. To update multiple tables at once you have the following options:
Use Stored procs
Create a View of the select statement and delete from the view
Use triggers.
Related
I have a main table that I am trying to update from a table that only has fields populated that need to be updated in the main table, using a unique ID to ensure I am updating only matched records. Here is what the SQL string looks like for the query:
UPDATE [tblMain] INNER JOIN tblUpdate ON tblMain.UUID = tblUpdate.UUID
SET tblMain.contractName = tblUpdate.contractName
WHERE ((tblUpdate.contractName) IS NOT NULL);
The idea is to only pull the fields from the update table that actually have data, and ignore the fields that are either NULL, blank, or empty. To be clear, I want to include ALL records in the tbleUpdate, but only update with the fields that contain data. How can I do this?
If you want to UPDATE one table from another in MS Access, you should do this
UPDATE tblMain, tblUpdate
SET tblMain.contractName = tblUpdate.contractName
WHERE tblMain.UUID = tblUpdate.UUID
AND tblUpdate.contractName IS NOT NULL
I am trying to update the column "efficiency" in the table "SUS_WK" with the data from the column "custom_number_8" from the table "SC_PROD". But I only want it to update if certain requirements are met, such as the "ID" from table "SUS_WK" matches the "ID" from the table "SC_PROD".
How can I do this?
I have tried to do this:
UPDATE SUS_WK
SET efficiency = SC_PROD.custom_number_8
FROM SUS_WK t
JOIN SC_PROD p
ON t.id = p.id
When I tried the code above, I get the following error:
The multi-part identifier "SC_PROD_PLAN_PLND.custom_number_8" could not be bound.
But I expect the result of that code to update the column "efficiency" in the "SUS_WK" with the data from column "custom_number_8" in the table "SC_PROD".
You are on the right track. Just use the table alias rather than the table name:
UPDATE t
SET efficiency = p.custom_number_8
FROM SUS_WK t JOIN
SC_PROD p
ON t.id = p.id;
I strongly recommend using the table alias for the UPDATE as well. SQL Server will resolve the table name to be the same as the t -- but depending on that makes the query rather hard to decipher (because references to the same table have difference aliases).
I have a sql table that I am trying to add a column from another table to. Only when I execute the alter table query it does not pull the values out of the table to match the column where I am trying to make the connection.
For example I have column A from table 1 and column A from table 2, they are supposed to coincide. ColumnATable1 being an identification number and ColumnATable2 being the description.
I tried this but got an error...
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200)
where v_venkey = v_vendorno
It tells me that I have incorrect syntax... Anyone know how to accomplish this?
alter table dbo.CommittedTbl
add V_VendorName nvarchar(200);
go
update c
set c.V_VendorName = a.V_VendorName
from CommittedTbl c
join TableA a
on c.v_venkey = a.v_vendorno;
go
I'm just guessing at your structure here.
alter table 2 add column A <some_type>;
update table2 set column A = (select column_A from table2 where v_venkey = v_vendorno);
Your names for tables and columns are a bit confusing but I think that should do it.
There is no WHERE clause for an ALTER TABLE statement. You will need to add the column (your first two lines), and then insert rows based upon a relationship you define between the two tables.
ALTER TABLE syntax:
http://msdn.microsoft.com/en-us/library/ms190273%28v=sql.90%29.aspx
There are several languages within SQL:
DDL: Data Definition Language - this defines the schema (the structure of tables, columns, data types) - adding a column to a table affects the table definitions and all rows will have that new column (not just some rows according to a criteria)
DML: Data Manipulation Language - this affects data within a table, and inserting, updating or other changes fall into this and you can update some data according to criteria (and this is where a WHERE clause would come in)
ALTER is a DDL statement, while INSERT and UPDATE are DML statements.
The two cannot really be mixed as you are doing.
You should ALTER your table to add the column, then INSERT or UPDATE the column to include appropriate data.
Is it possible that you want a JOIN query instead? If you want to join two tables or parts of two tables you should use JOIN.
have a look at this for a start if you need to know more LINK
hope that helps!
So I have a table which has a bunch of information and a bunch of records. But there will be one field in particular I care about, in this case #BegAttField# where only a subset of records have it populated. Many of them have the same value as one another as well.
What I need to do is get a count (minus 1) of all duplicates, then populate the first record in the bunch with that count value in a new field. I have another field I call BegProd that will match #BegAttField# for each "first" record.
I'm just stuck as to how to make this happen. I may have been on the right path, but who knows. The SELECT statement gets me two fields and as many records as their are unique #BegAttField#'s. But once I have them, I haven't been able to work with them.
Here's my whole set of code, trying to use a temporary table and SELECT INTO to try and populate it. (Note: the fields with # around the names are variables for this 3rd party app)
CREATE TABLE #temp (AttCount int, BegProd varchar(255))
SELECT COUNT(d.[#BegAttField#])-1 AS AttCount, d.[#BegAttField#] AS BegProd
INTO [#temp] FROM [Document] d
WHERE d.[#BegAttField#] IS NOT NULL GROUP BY [#BegAttField#]
UPDATE [Document] d SET d.[#NumAttach#] =
SELECT t.[AttCount] FROM [#temp] t INNER JOIN [Document] d1
WHERE t.[BegProd] = d1.[#BegAttField#]
DROP TABLE #temp
Unfortunately I'm running this script through a 3rd party database application that uses SQL as its back-end. So the errors I get are simply: "There is already an object named '#temp' in the database. Incorrect syntax near the keyword 'WHERE'. "
Comment out the CREATE TABLE statement. The SELECT INTO creates that #temp table.
I have to write a statement which fills a table (customers) with synthetically generated values. There is an addtional constraint that I should only fill those attributes (columns) with a special property (i.e. formally do a projection on them and then operate on them exclusively). These properties are stored in a second table, attributes.
My first draft consists of the following two statements:
-- Get the attributes (columns) we are interested in only
SELECT attributeID from attributes
WHERE tableID = 'customers'
-- Iterate over each row of customers, filling only those attributes (columns)
-- obtained by the above SELECT statement
UPDATE customers
SET (use the records from above select statement...)
Now my problem is how to put them together. I know there is the possibility of appending a WHERE clause to the SET clause, but that would select rows, not columns, as I need. I also read about PIVOT, but so far only inside one single table, not two, as is the case here. I would be very thankful for any hint, since I have no idea how to do this.
is not it you're looking for?
SQL Update Multiple Fields FROM via a SELECT Statement
UPDATE
Table
SET
Table.col1 = other_table.col1,
Table.col2 = other_table.col2
FROM
Table
INNER JOIN
other_table
ON
Table.id = other_table.id
Standard SQL-92 requires a scalar subquery:
UPDATE customers
SET attributeID = (
SELECT A1.attributeID
FROM attributes AS A1
WHERE A1.tableID = 'customers'
);
However, UPDATE customers...WHERE A1.tableID = 'customers' "smells" like you may be mixing data with metadata.