T-SQL - 拼脚本语句索引监控

use profiler to capture a server status for 24 hrs, the result stored into table Conn_AdminIII_2009_02_10
0. create index
Create index idx_starttime on Conn_AdminIII_2009_02_10(starttime)
1.SQL query:
select * from Conn_AdminIII_2009_02_10 where starttime between '2009-02-20 00:00:05.680' and '2009-02-20 09:10:05.680' order by duration desc
2.exec sp_executesql @sql:
declare @starttime datetime
declare @endtime datetime
set @starttime = '2009-02-20 00:00:05.680'
set @endtime = '2009-02-20 09:10:05.680'
declare @sql nvarchar(max)
set @sql= N'
select * from Conn_AdminIII_2009_02_10 where starttime
between '''+ convert(nvarchar(200), @starttime, 120) +'''
and '''+ convert(nvarchar(200), @endtime, 120) +'''
order by duration desc'
exec sp_executesql @sql
ref: Covert datetime to nvarchar:

select convert(nvarchar, getdate(), 120)

declare @starttime datetime
set @starttime = '2009-02-20 08:00:05.680'
select convert(nvarchar, @starttime, 120)

你可能感兴趣的:(sql,sql,server,脚本)