I have 2 SQL servers .
dstest\mssql2008 <--I'm currently at this instance ( server)
dstrn <-- another server
Both servers has the same table called :
EServices_Pages_Content
Goal : I need to update data on dstest from dstrn
On the current server (dstest) I have :
I can(!) access dstrn from dstest :
SELECT * FROM dstrn.weberp.dbo.EServices_Pages_Content WHERE pageid=80
OKay.
So where is the problem ?
As I told , I need to update data on dstest(current db) from a far server (dstrn) so I do :
UPDATE EServices_Pages_Content
SET [Content] = a.Content
FROM [dstrn].weberp.dbo.EServices_Pages_Content a
WHERE PageID = a.pageID
AND MasterEntityID = a.masterEntityid
AND LanguageID = a.LanguageID
AND PageID = 80
But I get an error :
Msg 209, Level 16, State 1, Line 4
Ambiguous column name 'PageID'.
Msg 209, Level 16, State 1, Line 5
Ambiguous column name 'MasterEntityID'.
Msg 209, Level 16, State 1, Line 6
Ambiguous column name 'LanguageID'.
Msg 209, Level 16, State 1, Line 7
Ambiguous column name 'PageID'.
I don't understand , I did use aliases , why does it tell me Ambiguous columns? How can I fix it ?
Edit : I found a way to make it work( see my answer) - but still can't understand why I need full table prefix in the where clause. - and can't use aliases to prevent Ambiguity
Try this:
UPDATE Dest
SET [Content] = a.Content
FROM EServices_Pages_Content Dest
INNER JOIN [dstrn].weberp.dbo.EServices_Pages_Content a
ON Dest.PageID = a.pageID
AND Dest.MasterEntityID = a.masterEntityid
AND Dest.LanguageID = a.LanguageID
WHERE a.PageID = 80
Explanation:
Using an alias on just one of the tables is not enough.
Since both of the tables have the same names and the same column names,
you need to specify an alias to the destination table as well, and use a multi-part name for it's columns.
Also, you are using an implicit join. Since explicit joins more readable, I would recommend to never use implicit joins.
I have created 2 test tables ( on the same server) :
Table testA
Table testB
Now , for simplicity - let's say I want to update tableA from tableB but only only for pageId=2 :
So first I tried :
UPDATE testA a
SET a.[Content] = b.Content
FROM testB b
WHERE a.PageID = b.pageID
AND a.PageID = 2
But I got an error :
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
So then I tried :
UPDATE testA
SET [Content] = b.Content
FROM testB b
WHERE testA.PageID = b.pageID -- notice prefix
AND testA.PageID = 2 -- notice prefix
And Then :
(1 row(s) affected)
And now you can see it's ok :
You need some join here:
UPDATE d
SET [Content] = a.Content
FROM EServices_Pages_Content d
JOIN [dstrn].weberp.dbo.EServices_Pages_Content s ON d.PageID = s.pageID
AND d.MasterEntityID = s.masterEntityid
AND d.LanguageID = s.LanguageID
AND d.PageID = 80
Related
I'm trying to update a table in sql server using the below command but I get the following error:
Msg 156, Level 15, State 1, Line 716
Incorrect syntax near the keyword 'LEFT'
Is LEFT not allowed when updating? What can be used instead? Thanks
UPDATE DI.DBO.MHS
SET (LEFT(BATCH_DATE_2, 1) + '0' + RIGHT(BATCH_DATE_2, 6))
WHERE LEFT(BATCH_DATE_2, 1) = 2
You must specify the column that you want to update:
UPDATE DI.DBO.MHS
SET BATCH_DATE_2 = LEFT(BATCH_DATE_2,1) + '0' + RIGHT(BATCH_DATE_2,6)
WHERE LEFT(BATCH_DATE_2,1) = 2
If it is not BATCH_DATE_2 the column that you want to update then use that column after SET.
I'm trying to run simple update , not cursor at all :) in Microsoft SQL Server
and I'm getting a strange error message .
Here is the update statement:
update [MaintenancePortal].[dbo].maz_batia
set mok_sap_num = '00001009999' ,
mok_sap_work_order = '00000123123',
mok_sap_status = 'אאאאא',
mok_sap_status_work_order = 'בבבבב',
-- mok_equ_bz_description = NULL,
mok_location_description = 'גגג>ב8>גגג',
mok_work_order_cost_center = 'רררר',
mok_work_order_date = '2020-03-11',
mok_work_order_description = 'אבגאבגאבג' ,
mok_sap_tibco_status = 'SAP statuses updated',
mok_user_open = 'מממממ'
mok_department_desc = 'גגג>ב8>גגג'
where mok_sap_num = '00001009999'
This is the full error msg:
Msg 16924, Level 16, State 1, Procedure SendMail, Line 63 [Batch Start Line 76]
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
What can be the problem ? I'm going crazy :(
Thanks
What is wrong with my code?
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near 'UP'.
SQL server says missing something
UPDATE
(
SELECT
T.Br, U.Br AS Br1 ,
T.Dis, U.Dis AS Disc1 ,
T.DeletedDate , U.DeletedDate AS DeletedDate1 ,
T.INSERT_TS, U.INSERT_TS AS INSERT_TS1
FROM dbo.Matrix T
JOIN tlt_svs.Matrix_Update U
ON T.Key= U.Key)UP
SET
UP.Br = UP.Br1 ,
UP.Dis = UP.Dis1 ,
UP.DeletedDate = UP.DeletedDate1 ,
UP.INSERT_TS = UP.INSERT_TS1
Maybe you want an UPDATE with a join, i.e. update the values of matrix with the corresponding values of matrix_update?
UPDATE m
SET m.br = mu.br,
m.dis = mu.dis,
m.deleteddate = mu.deleteddate,
m.insert_ts = mu.insert_ts
FROM dbo.matrix m
INNER JOIN tlt_svs.matrix_update mu
ON mu.key = m.key;
Consider the relationship:
I want to remove a row in my many-to-many (userlabo) table but SQL Server shows the following error:
Msg 3609, Level 16, State 1, Line 1
Cannot delete last userlabo because usuario exists.
The transaction ended in the trigger. The batch has been aborted.
when running the statement:
delete from userlabo
where iduser = 3 and idlabo = 3
Anyone has faced this problem before?
SQL Server only allows me to remove a row if a row in the userlabo table is not the last row with identity to be removed. This error only occurs for the highlighted line below:
idlabo iduser
=================
1 1
2 1
3 1
1 1
2 1
3 1
3 3 <-- this line cannot be removed, it gives the "Cannot delete last userlabo because usuario exists." error.
=== UPDATE ===
After reading #IngoB and #Jorge Campos comments, I realized that there is a trigger:
.
.
.
/* erwin Builtin Trigger */
/* usuario userlabo on child delete no action */
/* ERWIN_RELATION:CHECKSUM="00026f13", PARENT_OWNER="", PARENT_TABLE="usuario"
CHILD_OWNER="", CHILD_TABLE="userlabo"
P2C_VERB_PHRASE="", C2P_VERB_PHRASE="",
FK_CONSTRAINT="R_23", FK_COLUMNS="iduser" */
IF EXISTS (SELECT * FROM deleted,usuario
WHERE
/* %JoinFKPK(deleted,usuario," = "," AND") */
deleted.iduser = usuario.iduser AND
NOT EXISTS (
SELECT * FROM userlabo
WHERE
/* %JoinFKPK(userlabo,usuario," = "," AND") */
userlabo.iduser = usuario.iduser
)
)
BEGIN
SELECT #errno = 30010,
#errmsg = 'Cannot delete last userlabo because usuario exists.'
GOTO error
END
.
.
.
My tables were generated using erwin forward engineering and it probably embedded triggers to my tables without my knowledge. I think I have to take a careful look at the erwin physical model to figure out why it's embedding those triggers.
Bottom line: deleting the table's triggers solved the problem.
I am trying to enter the following to an UPDATE trigger of QuoteItem:
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM dbo.fn_GetQuoteItemListPrice(QuoteItem.ItemId, QuoteItem.RoomId)
AS StyleItem
CROSS JOIN (QuoteItem JOIN INSERTED ON
QuoteItem.QuoteItemId = INSERTED.QuoteItemId)
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)
I get the follwoing error:
Msg 4104, Level 16, State 1, Procedure QuoteItem_UPDATE, Line 6
The multi-part identifier "QuoteItem.ItemId" could not be bound.
Msg 4104, Level 16, State 1, Procedure QuoteItem_UPDATE, Line 6
The multi-part identifier "QuoteItem.RoomId" could not be bound.
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM dbo.fn_GetQuoteItemListPrice(xx.ItemId, xx.RoomId)
AS StyleItem
CROSS JOIN (QuoteItem JOIN INSERTED ON
QuoteItem.QuoteItemId = INSERTED.QuoteItemId) xx
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)
Try the change I made above, assigning a table alias xx and referencing that instead..
Answer found here by Naomi Nosonovsky:
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM QuoteItem JOIN INSERTED AS INSERTED ON QuoteItem.QuoteItemId =
INSERTED.QuoteItemId
CROSS APPLY dbo.fn_GetQuoteItemListPrice(QuoteItem.ItemId, QuoteItem.RoomId)
AS StyleItem
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)