更新表可以用很多方法,最省空间的方法就是直接update,有时候数据来自于其他表,所以我们可以直接从其他表取数来更新
如现在有2张表,table_student(stu_No,name,age,class_No,class_name),table_class(class_No,stu_qty,class_name)
现在需要将class表的class_name更新到student表上,可以用如下语句:
update table_student set ts.class_name=tc.class_name from table_student ts,table_class tc where ts.class_No=tc.class_No
如果表在同服务器另外一个数据库里面,则可用[db].[table_class]来替代
如果表在不同的服务器,则可用[serverName].[db].[table_class]来替代,注意先链接一下,链接方法如下:
EXEC sp_addlinkedserver
@server='ServerName',--被访问的服务器别名
@srvproduct='',
@provider='SQLOLEDB',
@datasrc="10.1.0.8" --要访问的服务器
EXEC sp_addlinkedsrvlogin
'ServerName', --被访问的服务器别名(如果上面sp_addlinkedserver中使用别名ServerName,则这里也是ServerName)
'false',
NULL,
'sa', --帐号
'12345678' --密码