繁体   English   中英

使用另一个表中的信息更新表中的列

[英]Update a column in table with information from another table

我已经有了这个:

USE [AdventureWorks2012];
--- somewhere create table  
IF OBJECT_ID('[dbo].[PersonPhone]','U') IS NOT NULL   
DROP TABLE [dbo].[PersonPhone]  
CREATE TABLE [dbo].[PersonPhone](  
    [BusinessEntityID] [int] NOT NULL,  
    [PhoneNumber] nvarchar(25) NOT NULL,  
    [PhoneNumberTypeID] [int] NOT NULL,  
    [ModifiedDate] [datetime] NOT NULL)  
--- and append new column  
ALTER Table [dbo].[PersonPhone]  
ADD [StartDate] date NULL  
--- after this, i want copy dates in this column (at another table, we increase dates by one)  
UPDATE [dbo].[PersonPhone]
SET [StartDate] = [NewTable].[NewDate]
FROM (
       SELECT DATEADD(day, 1, [HumanResources].[EmployeeDepartmentHistory].[StartDate]) AS [NewDate],
          row_number() over(order by (select 1)) AS [RN]
   FROM [HumanResources].[EmployeeDepartmentHistory]
 ) [NewTable]

如何改进查询以将值从[NewTable]复制到[dbo]。[PersonPhone]。[StartDate]行?

在您的更新:

UPDATE [dbo].[PersonPhone]
SET [StartDate] = DATEADD(day, 1, [HumanResources].[EmployeeDepartmentHistory].[StartDate])
FROM [HumanResources].[EmployeeDepartmentHistory]
GO

SELECT row_number() over(order by (select 1)) AS [RN] FROM [HumanResources].[EmployeeDepartmentHistory]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM