I have a table I want to update this with a view in SQL Server 2008
When I write update sql code like this :
UPDATE [dorsadbfitupdetail].[dbo].[tbl_wl_Joint]
SET
[JntLineNoInternalUse] = dbo.IpmiLineInternal.LnNo
WHERE (dbo.tbl_wl_Joint.JntLineNoInternalUse IS NULL)
GO
SQL Server throws an error:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "dbo.IpmiLineInternal.LnNo" could not be bound.
What can I do to resolve it?
Try this one -
UPDATE j
SET JntLineNoInternalUse = i.LnNo
FROM dbo.tbl_wl_Joint j
JOIN dbo.IpmiLineInternal i ON j.ID = i.ID /* simple change your id columns */
WHERE j.JntLineNoInternalUse IS NULL
Related
Trying to run
SELECT *
FROM sys.dm_fts_index_keywords( DB_ID('database_name'), OBJECT_ID('table_name') )
against the table with the index and I get a syntax error
If I run in on master I get
Msg 30007, Level 16, State 1, Line 1
Parameters of dm_fts_index_keywords and dm_fts_index_keywords_by_document cannot be null.
I had to upgrade the compatibility level for the Database in order to be able to create the stoplist. Then all worked fine. Edetails here http://blogs.msdn.com/b/sqlserverfaq/archive/2011/08/31/creating-stoplist-on-database-of-compatibility-80-90-in-sql-server-2008.aspx.
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]
I have a very basic UPDATE SQL -
UPDATE HOLD_TABLE Q SET Q.TITLE = 'TEST' WHERE Q.ID = 101;
This query runs fine in Oracle, Derby, MySQL - but it fails in SQL server 2008
with following error:
"Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Q'."
If I remove all occurrences of the alias, "Q" from SQL then it works.
But I need to use the alias.
The syntax for using an alias in an update statement on SQL Server is as follows:
UPDATE Q
SET Q.TITLE = 'TEST'
FROM HOLD_TABLE Q
WHERE Q.ID = 101;
The alias should not be necessary here though.
You can always take the CTE, (Common Tabular Expression), approach.
;WITH updateCTE AS
(
SELECT ID, TITLE
FROM HOLD_TABLE
WHERE ID = 101
)
UPDATE updateCTE
SET TITLE = 'TEST';
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