2013-09-27 88 views
0

林已經有這樣的:。從另一個表更新表格與信息的列

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] [起始日期]排?

回答

2

在您的更新:

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] 
+0

SQL Server的告訴我說:「這個查詢返回不止一個結果......」 – Relrin

+0

現在寫,說,「對於日期時間廣東話調用方法」 – Relrin

+0

查詢更新..請給它嘗試.. – iceheaven31

相關問題