Sql 使用另一个表中的信息更新表中的列
我已经有了这个:Sql 使用另一个表中的信息更新表中的列,sql,sql-server,sql-server-2012,sql-server-express,Sql,Sql Server,Sql Server 2012,Sql Server Express,我已经有了这个: 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] nva
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]
SQL Server告诉我“此查询返回的结果不止一个…”现在写下,“不能调用datetime的方法”已经复制,但所有行都有一个值。如何逐行复制?如果是这样,如果我是你,我会将update查询放入循环语句中。