您的位置:首页 > 数据库

sql的一些实用技巧收集!

2008-08-05 10:43 381 查看
 先来一个,打开sql  STATISTICS 的I/O状态,可看到读I/O的状态!

SET STATISTICS IO ON
GO

 

下面一个关于循环取值的问题,别人写的句SQL,很值得借鉴~

 

DECLARE @var NVARCHAR(1000)
SET @var = '11¦22¦33¦44¦55'
set @var=replace(@a,'¦',' union all select ')
exec('select '+@var)

 

--根据组来汇总

select c,sum(id) from b group by c with rollup 

create table #a(姓名 varchar(10),学科 varchar(10),分数 int) 

insert #a select '001',  '语文', 98 

insert #a select '001',  '数学', 95 

insert #a select '001',  '外语', 92 

insert #a select '002',  '语文', 98 

insert #a select '002',  '数学', 95 

insert #a select '002',  '外语', 92 

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

select 姓名,学科,sum(分数) 分数 from #a group by 姓名,学科 with rollup having 姓名 is not null 

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

001 数学 95 

001 外语 92 

001 语文 98 

001 NULL 285 

002 数学 95 

002 外语 92 

002 语文 98 

002 NULL 285

 

--获取存储过程的参数,类型,长度
select  
  a.name '参数',
  b.name '类型',
  a.length '长度'    
from  
  syscolumns a,
  systypes b  
where  
  id=object_id('proc_test')  
and  
  a.xtype=b.xtype

--查看存储过程的定义
exec   sp_helptext 'proc_test'
 

 

 

 

将数据取出存入一实体表,不需要表结构

select * into newtable from table
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息