SQL update 관련 변경, 내부 참조 표 이름 필요

6531 단어 update
select A.Id,A.Email,(

select C.Email from [nopCommerce].[dbo].[Customer] C 

left join [nopCommerce].[dbo].CustomerAddresses CA 

on C.Id=CA.Customer_Id where A.Id=CA.Address_Id) as 'Email2' from [nopCommerce].[dbo].[Address] A

이렇게 효과 가 있다.
 
그러나.
update [nopCommerce].[dbo].[Address] set Email=(

select C.Email from [nopCommerce].[dbo].[Customer] C

left join [nopCommerce].[dbo].CustomerAddresses CA 

on C.Id=CA.Customer_Id where Id=CA.Address_Id) 

이렇게 무효 한 데 이 터 는 바 뀌 었 지만, 전혀 원 하 는 결과 가 아니다.
update [nopCommerce].[dbo].[Address] set Email=(

select C.Email from [nopCommerce].[dbo].[Customer] C

left join [nopCommerce].[dbo].CustomerAddresses CA 

on C.Id=CA.Customer_Id where  [nopCommerce].[dbo].[Address] .Id=CA.Address_Id) 

내부 연결 에 만
  [nopCommerce].[dbo].[Address] .Id    

DECLARE @a TABLE (Id INT, NewEmail NVARCHAR(200))--INSERT INTO @a( Id, NewEmail)VALUES(1,'abc')
 
INSERT INTO @a( Id, NewEmail) select A.Id,( select C.Email from [nopCommerce].[dbo].[Customer] C left join [nopCommerce].[dbo].CustomerAddresses CA on C.Id=CA.Customer_Id where A.Id=CA.Address_Id) as 'Email2' from [nopCommerce].[dbo].[Address] A
update [nopCommerce].[dbo].[Address] set Email=( select T.NewEmail from @a T where [nopCommerce].[dbo].[Address].Id=T.Id)
 
update [nopCommerce].[dbo].[Address] set Email=( select C.Email from [nopCommerce].[dbo].[Customer] C left join [nopCommerce].[dbo].CustomerAddresses CA on C.Id=CA.Customer_Id where Id=CA.Address_Id) select A.Id,A.Email,( select C.Email from [nopCommerce].[dbo].[Customer] C left join [nopCommerce].[dbo].CustomerAddresses CA on C.Id=CA.Customer_Id where A.Id=CA.Address_Id) as 'Email2' from [nopCommerce].[dbo].[Address] A

좋은 웹페이지 즐겨찾기