Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 插入到sql server中的现有临时表中_Sql Server_Sql Server 2005 - Fatal编程技术网

Sql server 插入到sql server中的现有临时表中

Sql server 插入到sql server中的现有临时表中,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我在sql server中创建了一个临时表,并向其中插入了值。 如果某个特定列的值为null,我需要使用另一个表中的一些值进行填充。如何查询 下面给出了示例数据 select 'name' as name,3 as age,'email' as email into #tmp1 from table1 现在,如果列年龄为空,我需要在tmp1中所有现有记录的列年龄中插入一个值。 INSERT INTO #tmp1 (age)SELECT age AS [age] FROM table2 WHER

我在sql server中创建了一个临时表,并向其中插入了值。 如果某个特定列的值为null,我需要使用另一个表中的一些值进行填充。如何查询

下面给出了示例数据

select 'name' as name,3 as age,'email' as email into #tmp1 from table1
现在,如果列年龄为空,我需要在tmp1中所有现有记录的列年龄中插入一个值。

INSERT INTO #tmp1 (age)SELECT age AS [age] FROM table2 WHERE name=@name 
但它插入了一个新的记录


请帮助。

您需要更新,而不是插入

大概是这样的:

UPDATE  #tmp1 
SET age = Table2.Age
FROM table2
WHERE #tmp1.Age IS NULL
AND   #tmp1.Name = Table2.Name

您需要执行更新而不是插入:

Update a
a.age = b.age
from
(select id,age from table where age is null) a 
inner join
table b 
ON a.id = b.id