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才会有效。