兔八哥极品软件园    运行: 2821天 | 文章:589 篇 | 评论:15 条 | 碎语:1条

sqlserver复制表

作者:admin 发布于:2014-6-30 15:56 Monday 分类:MSSQL


1、新表不存在(即复制数据的同时创建与旧表相同结构的新表):

SELECT * INTO dbo.test_backup FROM dbo.TP_TeacherSalaryPreviewHistory WHERE SemesterID=137 AND TrainingCenterID=13 AND (GradeCode BETWEEN 14 AND 16) AND SegmentID=2 AND SalaryDate ='2012-8'


2、新表已存在,从旧表从插入选择的数据(注意:表的[主键]不要复制,因为具有唯一性,否则:Sql Server提示“当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'xxxxxx' 中的标识列插入显式值”)

INSERT INTO dbo.test_backup(        [SemesterID]       ,[SemesterName]       ,[SegmentID]       ,[SegmentName]       ,[SalaryDate]       ) SELECT        [SemesterID]       ,[SemesterName]       ,[SegmentID]       ,[SegmentName]       ,[SalaryDate] FROM [dbo].[TP_TeacherSalaryPreviewHistory] WHERE SemesterID=137 AND TrainingCenterID=13 AND (GradeCode between 14 and 16) AND SegmentID=2 AND SalaryDate ='2012-8' AND (ClassCategoryID IN (167,192,91,191,192))


以上方法经过测试,全部通过!!

标签: sqlserver


Powered by 兔八哥极品软件 苏ICP备12049267号 sitemap