码迷,mamicode.com
首页 > 其他好文 > 详细

关联更改,内部需要引用表名

时间:2014-09-05 12:55:51      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   数据   div   sp   log   on   

 

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

关联更改,内部需要引用表名

标签:style   blog   color   ar   数据   div   sp   log   on   

原文地址:http://www.cnblogs.com/zihunqingxin/p/3957806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!