我还在学习SQL。我有关于更新表的问题。我如何根据其他表更新我的表?请看下面的例子:SQL Server初学者:如何从另一个表更新表数据?
create table batch_info (
batch_key int identity(1, 1) not null primary key,
batch_num int not null,
batch_name varchar(50) null,
batch_start datetime null,
batch_end datetime null,
table_name varchar(50) null,
insert_rows int null
)
go
insert into batch_info (batch_num, batch_name, batch_start)
values ('32', 'Batch_to_insert_rows', '20110414')
go
select * from batch_info
create table ref_table (
bat_num int not null,
bat_end_date datetime null,
bat_table_name varchar(50) null,
bat_ins_rows int null,
)
go
insert into ref_table
values ('32','20110414 02:12:00.000','Table1','10'),
('32','20110414 02:12:00.000','Table2','33'),
('32','20110414 02:12:00.000','Table3','12')
select * from ref_table
--How can I update batch_info table to get this info?
select
bi.batch_key,
bi.batch_num,
bi.batch_name,
bi.batch_start,
rt.bat_end_date,
rt.bat_table_name,
rt.bat_ins_rows
from batch_info as bi
inner join ref_table as rt on bi.batch_num = rt.bat_num
编辑:一批重点是代孕所以它应该是渐进的,而不是(1,1,1)在我的连接查询。
@Jeffrey +1为你回答没有光标(ick)的问题,就像我建议的那样。但正如我们双方都同意的那样,它应该是两张桌子。 – Jim 2011-04-14 21:32:14
谢谢。第一条语句会生成一个错误:消息8120,级别16,状态1,行9 列'ref_table.bat_num'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。 – jrara 2011-04-15 04:16:31
而插入语句似乎没有插入任何东西在这个例子中。 – jrara 2011-04-15 04:20:39