几条SQL语句

1.查询一个数据库中所有表的行数:

SELECT   a.name, b.rows
FROM      sysobjects AS a INNER JOIN
sysindexes AS b ON a.id = b.id
WHERE (a.type = 'u') AND (b.indid IN (0, 1))
ORDER BY b.rows DESC

2.将查询出的A表数据再插入A表,重复执行的话,A表中的数据会指数增长,哈哈,弄他几百万条数据玩玩:

INSERT INTO dbo.UserInfo SELECT [UserName]
,[PassWord]
,[ErrorDate]
,[ErrorTimes] FROM dbo.UserInfo

3.使用表变量保存中间结果来简化我们的查询(via:http://www.2cto.com/database/201310/251805.html)

  在开发中,我们经常会使用到表变量,通常会先将查询的某些中间结果保存在表变量中,为后续的操作做准备。

  数据表(Student):

 

 数据表(Class):

  需求:查询出教师“yl”的所有学生信息。

  常规SQL查询方法: 

select a.id,a.name,a.age,a.classid 
from tb_Student a,tb_Class b 
where a.classid=b.classid and b.teacher='yl'

 使用表变量的查询方法:

declare @classIdTable table(  
classid int  
);  
insert into @classIdTable select classid from tb_Class where teacher='yl' 
select * from tb_Student where classid in (select classid from @classIdTable)

 这种方式将满足条件的classid先保存在表变量@classIdTable,再利用表变量中的classid去查询表tb_Student。

 结果:

 

 4.SQL Server统计查询语句消耗时间

 方法1:

set statistics time on
go
 SQL语句
go
set statistics time off

 方法2:

DECLARE @begin dateTime
DECLARE @end dateTime
SET @begin=getdate();
BEGIN
 SQL语句
end
set @end=getdate();
SELECT datediff(ms,@begin,@end) as 'Elapsed Time'

方法3:在SQL Server Management Studio中的工具>选项中设置。需要重启SQL Server Management Studio才会有效。

 

转载于:https://www.cnblogs.com/BigerXie/p/4410047.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值