[SQL server]常用SQL(一)

1.在Select语句中使用判断查询:
功能:计算条件列表并返回多个可能结果表达式之一。
示例:以判断user_pass字段值是否为空,为空时值为yes反之为no查询数据,条件为user_name不为空
select
 case
  when user_pass is null then 'yes' else 'no'
 end as 'user_pass'
 ,user_name as 'admin'
from
 admin
where
 user_name is not null

------------------------------------

2.datepart函数的使用
功能:返回代表指定日期的指定日期部分的整数。
示例:查询2004年与2005年之间的数据
select * from admin
where datepart( yyyy,date_time )
 between 2004 and 2005

------------------------------------

3.datediff函数使用
功能:返回跨两个指定日期的日期和时间边界数。
示例:打印日期差
declare @date_time datetime
set @date_time = convert( datetime,'2005-05-06' )
print datediff( dd,@date_time,getdate() )


------------------------------------

4.exists关键字使用
功能:指定一个子查询,检测行的存在。
示例1:判断用户'admin'是否存在,如存在就返回所有行。
select *
from
 admin
where
 exists( select user_pass from admin where user_name='admin' )

示例2:判断用户'admin'是否存在,如不存在就返回所有行。
select *
from
 admin
where
 not exists( select user_pass from admin where user_name='admin' )


------------------------------------

5.@@IDENTITY关键字
功能:返回最后插入的标识值。
示例:插入一新行,打印插入的新行的标识ID值。
insert admin( user_name,user_pass,date_time,team_group )
values
 ( 'test','test',getdate(),3 )
print @@identity


------------------------------------

6.@@rowcount关键字
功能:返回受上一语句影响的行数。
示例1:选择数据,返回所选择的数据的行数
select * from admin
print @@rowcount
示例2:更新数据,返回被更新数据所影响的行数
update admin set user_name='test' where user_name='zxb'
print @@rowcount


------------------------------------

7.Group by应用
功能:
示例1:
 SQL查询:select type,price from titles where royalty = 10
 结果:
  type  price
  -----------------------
  business     19.9900
  business     11.9500
  business     19.9900
  popular_comp 20.0000
  psychology   21.5900
  psychology   7.0000
  psychology   19.9900
  psychology   7.9900
  trad_cook    20.9500
  trad_cook    14.9900
 Group by 分组查询:select type,sum(price) as price from titles where royalty=10 group by type
 结果:
  type  price
  -----------------------
  business     51.9300
  popular_comp 20.0000
  psychology   56.5700
  trad_cook    35.9400
 Group by all 分组查询:select type,sum(price) as price from titles where royalty=10 group by all type
 结果:
  type  price
  -----------------------
  business     51.9300
  mod_cook     NULL
  popular_comp 20.0000
  psychology   56.5700
  trad_cook    35.9400
  UNDECIDED    NULL

CREATE PROCEDURE testPro
AS
    
/* ------- 事务开始 ---------- */
    
BEGIN TRANSACTION tran_test

    
/* -------- 保存事务 ----------*/
    
SAVE TRANSACTION tran_test

        
/* -------- 数据操作 ---------*/
        
INSERT [table1] ( [content] ) VALUES ( '43332' )

    
/*---------- 提交事务 ------------*/
    
COMMIT TRANSACTION tran_test

    
/*--------- 判断是否有错误 ----------*/
    
IF ( @@ERROR <> 0 )
    
BEGIN
        
/*---------- 自定义错误输出 ----------*/
        
RAISERROR'Insert data error!',16,1 )
        
/*-------- 事务回滚 --------*/
        
ROLLBACK TRANSACTION tran_test
    
END
    
    
/*------- 判断事务数是否大于0 -----------*/
    
IF ( @@TRANCOUNT > 0 )
    
BEGIN
        
/*-------- 事务回滚 --------*/
        
ROLLBACK TRANSACTION tran_test
    
END
GO
查询用户表结构:
SELECT 

表名
=case when a.colorder is not null then d.name else '' end,
字段序号
=a.colorder,
字段名
=a.name,
标识
=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '' else '' end,
主键
=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (  SELECT name FROM sysindexes WHERE indid in(   SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid  ))) then '' else '' end,  
类型
=b.name,
占用字节数
=a.length,
长度
=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数
=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空
=case when a.isnullable=1 then ''else '' end,
默认值
=isnull(e.text,''),
字段说明
=isnull(g.[value],'')

FROM syscolumns a left join systypes b on a.xtype=b.xusertype
     
inner join sysobjects d on a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
     
left join syscomments e on a.cdefault=e.id
     
left join sysproperties g on a.id=g.id and a.colid=g.smallid

WHERE d.name='table1'    -- 数据表名
ORDER BY a.colorder    -- 排序

查询用户表:
select 表名=name from sysobjects where xtype='U' and name<>'dtproperties'
原文地址:https://www.cnblogs.com/Leo_wl/p/1933704.html