zoukankan      html  css  js  c++  java
  • 【SqlServer】SqlServer的常规操作

    创建一张新表,不负责任何数据(该表不会有原来表的主键、索引等等)

    select * into NewTable from OldTable where 1<>1;

    创建一张新表,并且复制旧表的数据(不会复制原来表的主键,索引等等)

    select * into tablenew from tableold

     也可以指定复制那些字段:

    SELECT vale1, value2 into Table2 from Table1

    INSERT INTO SELECT语句

    Insert into Table2(field1,field2,...) select value1,value2,... from Table1

    这种查询后直接转表的形式,比先用select将数据查询出来后再一条条的插入到新表中效率要高。

    在插入数据之前先判断是否已经有相同的数据,若有则不添加,若无则添加

    if not exists(select * from Table1 where orderId=@orderid) insert into Table1(info,orderId)values(@info,@orderid);

    上面的语句就是先判断Table1中是否已经有@orderid的订单,如果没有则加入该订单的信息,若有则不做任何操作。

    上面的过程是如果是没有订单就加入,下面语句对上面的语句进行了扩充,如果有订单的话,则更新。

    if not exists(select * from Table1 where orderId=@orderid)

    insert into Table1(info,orderId)values(@info,@orderid)

    else

    update Table1 set info=@info  where orderId=@orderid;

    在创建表的时指定主键自增长(identity):

    create  table{

    id int primary key IDENTITY(1,1),

    name varchar(10)

    }

    除了可以在指定自增长的时候,还可以主键为聚集索引(默认为非聚集索引):

    create table TestTable(

    id int primary key clustered IDENTITY(1,1),

    name varchar(10)

    );

    SqlServer撤销所有表

    EXEC sp_MSforeachtable 'DROP TABLE ?'

  • 相关阅读:
    Android_项目文件结构目录分析
    WPF_MVVM 开发的几种模式讨论
    Blend_技巧篇_淡入淡出
    Blend_技巧篇_导入PSD文件制作ToggleButton (Z)
    Blend_界面快速入门(Z)
    Blend_软件系列简介(Z)
    Blend_ControlTemplate(Z)
    803. 区间合并
    P4017 最大食物链计数
    P1113 杂务
  • 原文地址:https://www.cnblogs.com/HDK2016/p/9237971.html
Copyright © 2011-2022 走看看