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

  • 相关阅读:
    支付宝-单笔转账接口
    # Creating Server TCP listening socket *:6379: bind: No such file or directory
    Window 下安装 Redis,配置redis环境变量
    Ajax的跨域(一)
    web人脸识别(二)
    web人脸识别(一)
    给GridView添加列头复选框
    计算两个时间相差多少年月日的sql算法
    MUI下拉加载安卓手机无效的解决方法
    博主回来了!
  • 原文地址:https://www.cnblogs.com/HDK2016/p/9237971.html
Copyright © 2011-2022 走看看