数据库教程:SQL Server 复制表结构以及数据,去除表中重复字段

–复制另一个数据库中的某张表的结构及数据–select * from Test.dbo.TestTable(查询表中所有数据) –into [表名] 插入当前数据库新表,如果没有该表就创建 select * into TestCopy from Test.dbo.TestTable –只复制 …

–复制另一个数据库中的某张表的结构及数据
–select * from test.dbo.testtable(查询表中所有数据)
–into [表名] 插入当前数据库新表,如果没有该表就创建

select * into testcopy from test.dbo.testtable

–只复制表结构(1!=1等价于1<>1,只要where后的为false就可以)
–把查询出的数据插入到新表,如果没有数据就只是复制表结构了
select * into testcopy from test.dbo.testtable where 1!=1

–into #[表名]  #代表临时表
select * into #testcopy from test.dbo.testtable

–复制某个字段到新表(字段名相同)
select testid into testcopy from test.dbo.testtable

–复制某个字段到新表(字段名不同)
–下面的写法不正确,可能插入某一个字段时表必须存在
select testid into testcopy1(testcopy1id) from test.dbo.testtable

–复制某个字段到新表(重新命免费精选名字大全段名)
–在查询时使用别名新表中的字段名是别名而不是本名
select testid as testcopyid into testcopy1 from test.dbo.testtable

–上面是新表不存在的时候,当表存在时
insert into testcopy1 select * from test.dbo.testtable

–插入一个字段的数据到新表(可以插入到别的字段中去)
insert into testcopy1(testname) select testid from test.dbo.testtable

–复制同一张表中数据(没有主键,复制的数据在上面不在下面)
insert into testcopy(testid) select testname from testcopy

 

 

–去除表中重复字段
–row_number() over 统计相同的字段并给每条数据加上一个标号,>1代表重复数据,删除掉>1的数据

delete t from
(select row_number() over (partition by testid,testname,price order by testid) as p1,* from testcopy) as t
where t.p1>1

 

需要了解更多数据库技术:SQL Server 复制表结构以及数据,去除表中重复字段,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/625903.html

(0)
上一篇 2021年5月25日
下一篇 2021年5月25日

精彩推荐