• sql插入记录到带标识列的表


     

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Product_temp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[Product_temp]
    GO

    --创建临时表
    CREATE TABLE [dbo].[Product_temp] (
     [PAutoID] [int] NOT NULL,
     [PName] [varchar](20) NOT NULL,
     [PHasContent] [bit] NULL
    ) ON [PRIMARY]
    GO
    --**************************************************开始: 常数内容 **********************************************************

    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(1,'在线电视',1)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(2,'在线影视',1)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(3,'视频分享',1)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(4,'数字收音机',1)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(5,'流媒体综合首页',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(6,'智能乐园',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(7,'2010世界杯',1)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(8,'VIP专区',1)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(9,'鬼节专区',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(10,'VIP选美专区',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(11,'游戏专区',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(12,'高清电影专区',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(13,'男性健康VIP专区',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(14,'李小龙VIP专区',0)
    --------------------------------------------------------------------------------
    INSERT INTO Product_temp(PAutoID,PName,PHasContent) VALUES(15,'优酷VIP专区',0)
    --------------------------------------------------------------------------------

    --**************************************************结束: 常数内容 **********************************************************


    --**************************************************开始: 更新 **********************************************************

    UPDATE orig SET
    PName=temp.PName,
    PHasContent=temp.PHasContent
    from Product orig, Product_temp temp
    where orig.PAutoID = temp.PAutoID

    GO

    SET IDENTITY_INSERT Product on

    INSERT INTO Product(PAutoID,PName,PHasContent)
    select PAutoID,PName,PHasContent
    from Product_temp patch
    where not exists (select * from Product where PAutoID = patch.PAutoID)

    SET IDENTITY_INSERT Product OFF
    GO
    --**************************************************结束: 更新 **********************************************************

    --删除临时表
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Product_temp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[Product_temp]
    GO

    转载请注明出处:http://blog.csdn.net/dasihg/article/details/6782378

  • 相关阅读:
    BZOJ 3506 机械排序臂 splay
    BZOJ 2843 LCT
    BZOJ 3669 魔法森林
    BZOJ 2049 LCT
    BZOJ 3223 文艺平衡树 splay
    BZOJ 1433 假期的宿舍 二分图匹配
    BZOJ 1051 受欢迎的牛 强连通块
    BZOJ 1503 郁闷的出纳员 treap
    BZOJ 1096 ZJOI2007 仓库设计 斜率优化dp
    BZOJ 1396: 识别子串( 后缀数组 + 线段树 )
  • 原文地址:https://www.cnblogs.com/dashi/p/4034778.html
Copyright © 2020-2023  润新知