• SQL 常用语句


    1、触发器
    --关闭所有触发器   
      alter   table   TableName  disable   trigger   all   
    --开启所有触发器   
      alter   table   TableName  enable   trigger   all
    如果是启停指定触发器,则将all改为触发器名
     
    2、表结构
    --获取 表 列名
    Select Name From SysColumns Where ID=OBJECT_ID('TableName') Order By ColID

    增加字段
    alter table table_NAME add column_NAME char(200) default 默认值
    删除字段
    ALTER TABLE table_NAME DROP COLUMN column_NAME
    修改字段类型
    ALTER TABLE table_name  ALTER COLUMN column_name new_data_type
    改名
    sp_rename
    更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。

    语法
    sp_rename [ @objname = ] 'object_name' ,
        [ @newname = ] 'new_name'
        [ , [ @objtype = ] 'object_type' ]

    --假设要处理的表名为: tb

    --判断要添加列的表中是否有主键
    if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')
    begin
     print '表中已经有主键,列只能做为普通列添加'

     --添加int类型的列,默认值为0
     alter table tb add 列名 int default 0  
    end
    else
    begin
     print '表中无主键,添加主键列'

     --添加int类型的列,默认值为0
     alter table tb add 列名 int primary key default 0  
    end

     3、客户端 
    --显示当前连接数据库的客户端 

    select spid,hostname,nt_username,loginame,program_name,db_name(dbid) dbname,login_time
    from master.dbo.sysprocesses
    where spid>50

    (小于50的是系统进程,像资源监视器、日志写入、检查点、任务管理器等进程)

     4、获取 表中 自增列 的列名

    SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns

    WHERE TABLE_NAME='customer' AND COLUMNPROPERTY(OBJECT_ID('customer'),COLUMN_NAME,'IsIdentity')=1

    5、获取 表 字段名 拼串:

    select stuff((select ',' + name  from syscolumns Where ID=OBJECT_ID('customer') for xml path ('')),1,1,'') as ColNameStr

  • 相关阅读:
    Google Map API使用详解(一)——Google Map开发背景知识
    IOS开发中发送Email的两种方法
    iOS简单的画线(UIImageVIew方式)
    NSData + Base64
    (转载)谈Flash的破解与加密(附flash破解工具)
    Google Map API使用详解(二)——Google Map API中文说明
    oracle sysdate 等 时间的相关应用
    vc mail 封装类
    switch if 比较
    http://tech.ddvip.com/200810/122362552676322.html state模式~
  • 原文地址:https://www.cnblogs.com/wx881208/p/4274644.html
Copyright © 2020-2023  润新知