SQL Server 2008实用语句整理

来源:互联网 发布:2017年最火的网络手游 编辑:程序博客网 时间:2024/06/06 08:45
1、新建和某张表完全一样的表

  方法一:select top 0 * into HJY_Abcd from HJY_Goods (只复制表结构)
  方法二:select  * into HJY_Abcd from HJY_Goods(复制表结构和表数据)
2、拷贝表中数据(源表名:HJY_Goods 目标表名:HJY_Abcd) 
  insert into HJY_Abcd (Goo_Name,Goo_Unit,Goo_Specification) select 
  Goo_Name,Goo_Unit,Goo_Specification  from HJY_Goods;
3、左外连接查询
  select * from HJY_Staffs left outer join HJY_Roles on HJY_Staffs.Rol_ID = HJY_Roles.Rol_ID
4、between的用法,between限制查询数据范围时包括了边界值,not between不包括
  select * from HJY_Mgrns where Mgr_Makedate between '2012-01-01' and '2012-04-07'
  select * from HJY_Mgrns where Mgr_Makedate not between '2012-01-01' and '2012-04-07'
5、随机取出10条数据
  select top 10 * from HJY_Inventories order by newid()
6、列出数据库里面所有的表
  select name from sysobjects where xtype= 'u '

(U代表“表”,V代表“视图”,P代表“存储过程”)
7、查询表的所有字段名
  Select name from syscolumns Where ID=OBJECT_ID('HJY_Roles')
8、查询时改变结果值,在ireport中方便排序(显示时截取后两个字符)
  SELECT Pro_Name, case Lev_Name when '优等' then 'A优等' when '一等' then 'B一等' when '合格' 
  then 'C合格' end as Lev_Name ,Gla_Name,Inv_Number FROM ViewListInv ORDER BY Lev_Name