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 ?'

  • 相关阅读:
    淘宝技术分享
    15个富有创意的单页设计
    jQuery全能图片滚动插件
    jquery性能优化
    Algs4-1.4.40随机输入3-sum问题
    Algs4-1.4.39 改进倍率测试的精度
    Algs4-1.4.38 3-sum的初级算法与ThreeSum性能比较
    Algs4-1.4.37自动装箱的性能代价
    Algs4-1.4.35下压栈的时间成本
    Algs4-1.4.36下压栈的空间成本
  • 原文地址:https://www.cnblogs.com/HDK2016/p/9237971.html
Copyright © 2011-2022 走看看