SQL Server 復制表結構以及數據,去除表中重復字段


--復制另一個數據庫中的某張表的結構及數據
--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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM