I am trying to update a table so that all values become identical to another table on a different database. I can do it with an insert command but not an update command.
This works:
INSERT [test1].[dbo].[table1]
SELECT * FROM [source].[dbo].[table1]
This does not:
UPDATE [test2].[dbo].[table1]
SET [source].[dbo].[table1] = [test2].[dbo].[table1]
nor this:
UPDATE [test2].[dbo].[table1]
SET
[test2].[dbo].[table1].[PKcolumn] = [source].[dbo].[table1].[PKcolumn]
,[test2].[dbo].[table1].[column2] = [source].[dbo].[table1].[column2]
,[test2].[dbo].[table1].[column3] = [source].[dbo].[table1].[column3]
WHERE
[source].[dbo].[table1].[PKcolumn] = [test2].[dbo].[table1].[PKcolumn]
The result is always some variation of this error message despite checking for errors countless times:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "source.dbo.table1.PKColumn" could not be bound.
Any Suggestions?
Since update can only affect one table, you don't have to specify it:
UPDATE dest
SET column2 = src.column2
FROM source.dbo.table1 as src
JOIN test2.dbo.table1 as dest
on dest.PKcolumn = src.PKcolumn
P.S. Depending on which database you're using, you might want to check out the MERGE statement.
You are missing the FROM clause (since you are using a multiple tables in an update clause)
Check this: http://scottonwriting.net/sowblog/archive/2010/07/13/howto-update-records-in-a-database-table-with-data-from-another-table-ms-sql-server.aspx
Try this:
UPDATE [test2].[dbo].[table1]
SET
[test2].[dbo].[table1].[PKcolumn] = [source].[dbo].[table1].[PKcolumn]
,[test2].[dbo].[table1].[column2] = [source].[dbo].[table1].[column2]
,[test2].[dbo].[table1].[column3] = [source].[dbo].[table1].[column3]
FROM [source].[dbo].[table1] JOIN [test2].[dbo].[table1]
ON
[source].[dbo].[table1].[PKcolumn] = [test2].[dbo].[table1].[PKcolumn]
Related
In a SQL Server table, I have a BIT column and based its value, I need to update that table's other columns with some values. I tried this
UPDATE tablename SET Completed = GETDATE() WHERE CheckTaskAvailable = TRUE
but I get the error
Msg 207, Level 16, State 1, Server SQLDEV01, Line 1
Invalid column name 'TRUE'.
How to do this in a T-SQL query?
if you want to set as true try
Update table set columnName = 1 where ...
if you want to set as false try
Update table set columnName = 0 where ...
In addition to using the values 0 and 1, the T-SQL documentation for bit says that
The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.
so these work, too:
UPDATE tablename SET bitcolumn = 'TRUE' WHERE ...
UPDATE tablename SET othercolumn = 'something' WHERE bitcolumn = 'TRUE'
I had a need to do something similar where I was looking to update a field based on whether a record existed or not existed in another table so I used above code (thank you RezaRahmati) and added:
Update table set columnName = 1 where ID IN (SELECT ID FROM other table)
or for false
Update table set columnName = 0 where ID NOT IN (SELECT ID FROM other table)
I really enjoy Stack Overflow it has really helped me learn.
I wrote the following trigger:
begin
update NFL.TeamStatistics
set Passing_Yards = (select sum(Quarterbacks.Yards)
from NFL.Quarterbacks
where Quarterbacks.Team = inserted.Team)
from NFL.Quarterbacks
inner join inserted on Quarterbacks.Team = inserted.Team;
Whenever someone updates the passing yards in the table about quarterbacks, it should automatically set NFL.TeamStatistics.Passing_Yards to the sum of each team's passing yards.
I used the following update statement to test it:
update NFL.Quarterbacks
set Quarterbacks.Yards = 4000
where Team = 'PIT';
However, in the table NFL.TeamStatistics it set the passingyards for all teams to 4000 instead of just for PIT. What is the matter?
You need to add a where clause and column in NFL.TeamStatistics that you want to compare to
e.g:
begin
update NFL.TeamStatistics
set Passing_Yards = (select sum(Quarterbacks.Yards)
from NFL.Quarterbacks
where Quarterbacks.Team = inserted.Team)
from NFL.Quarterbacks
inner join inserted on Quarterbacks.Team = inserted.Team
where Team = Quarterbacks.Team;
Assuming you were comparing to Quaterbacks.Team
You should use where clause in your update statement in order to filter data you are going to update.
Can anyone help me fix this script?
I get this error: Msg 4104, Level 16, State 1, Line 1 The multi-part
identifier "#Temp3.EmpID" could not be bound.
the error is coming from: -- Update into Keyshop.EmployeeTable
UPDATE EmployeeTable SET Status = 'False' WHERE #Temp3.EmpID = EmployeeTable.EmpID
The script is pretty self explanatory but let me know if you need me to provide more info.
Thank you
-- Update into Keyshop.EmployeeTable
UPDATE EmployeeTable SET Status = 'False' WHERE #Temp3.EmpID = EmployeeTable.EmpID
This isn't a valid update statement since it references a table not in the UPDATE Clause or the Optional FROM clause
UPDATE EmployeeTable
SET Status = 'False'
WHERE #Temp3.EmpID = EmployeeTable.EmpID
There are several ways to do it using IN is probably the easiest to understand
UPDATE EmployeeTable
SET Status = 'False'
WHERE EmployeeTable.EmpID IN (SELECT EmpID FROM #Temp3)
Switch these guys around. Instead of this:
UPDATE EmployeeTable SET Status = 'False' WHERE #Temp3.EmpID = EmployeeTable.EmpID
Have this
UPDATE EmployeeTable SET Status = 'False' WHERE EmployeeTable.EmpID in (SELECT EmpID FROM #Temp3)
Edit: I missed that #Temp3.EmpID is a table, not a variable.
I want to update some data in EditEmail table from DBUsers database to EditEmail table in DBCurrent datebase.So i got error when i execute following statement:
USE DBCurrent
UPDATE [DBUsers].[dbo].[EditEmail] EN
SET EN.MailSubject = E.MailSubject,
EN.MailMessage = E.MailMessage
FROM
(
SELECT * FROM EditEmail
) AS E
WHERE EN.Type = E.Type
Error message:
ERROR:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'EN'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'AS'.
Is there any syntax mistake in this T-SQL?
You cannot give the table you're specifying in the UPDATE statement a table alias (for whatever reason - don't ask me why this isn't possible, ask the T-SQL design team)...
Try this statement instead:
USE DBCurrent
UPDATE
[DBUsers].[dbo].[EditEmail]
SET
MailSubject = E.MailSubject,
MailMessage = E.MailMessage
FROM
dbo.EditEmail E
WHERE
DBUsers.dbo.EditEmail.Type = E.Type
AND E.Type = 'blahblah' -- or whatever additional conditions you have!
You need to specify the table being updated in full, e.g. with its database, schema, table and column name, in the WHERE clause.
There is also no need for your "artificial" subquery there to reference the EditMail table - just define that in the FROM clause and give it a table alias (here, they're supported).
T-SQL doesn't allow to define an alias on the table updated,
but it allows to update an alias:
This fixes the syntax, I think the logic must still be improved.
Please try (I didn't check):
UPDATE EN
SET EN.MailSubject = E.MailSubject,
EN.MailMessage = E.MailMessage
FROM [DBUsers].[dbo].[EditEmail] EN
join EditEmail E
on EN.Type = E.Type
I've written a stored procedure as following:
CREATE PROC spSoNguoiThan
#SNT int
AS
begin
IF not exists (select column_name from INFORMATION_SCHEMA.columns where
table_name = 'NhanVien' and column_name = 'SoNguoiThan')
ALTER TABLE NhanVien ADD SoNguoiThan int
else
begin
UPDATE NhanVien
SET NhanVien.SoNguoiThan = (SELECT Count(MaNguoiThan)FROM NguoiThan
WHERE MaNV=NhanVien.MaNV
GROUP BY NhanVien.MaNV)
end
SELECT *
FROM NhanVien
WHERE SoNguoiThan>#SNT
end
GO
Then I get the error :
Server: Msg 207, Level 16, State 1, Procedure spSoNguoiThan, Line 12
Invalid column name 'SoNguoiThan'.
Server: Msg 207, Level 16, State 1, Procedure spSoNguoiThan, Line 15
Invalid column name 'SoNguoiThan'.
Who can help me?
Thanks!
When the stored proc is parsed during CREATE the column does not exist so you get an error.
Running the internal code line by line works because they are separate. The 2nd batch (UPDATE) runs because the column exists.
The only way around this would be to use dynamic SQL for the update and select so it's not parsed until EXECUTE time (not CREATE time like now).
However, this is something I really would not do: DDL and DML in the same bit of code
I ran into this same issue and found that in addition to using dynamic sql I could solve it by cross joining to a temp table that had only one row. That caused the script compiler to not try to resolve the renamed column at compile time. Below is an example of what I did to solve the issue without using dynamic SQL
select '1' as SomeText into #dummytable
update q set q.ValueTXT = convert(varchar(255), q.ValueTXTTMP) from [dbo].[SomeImportantTable] q cross join #dummytable p