在线等,靠,这么简单的语句写不来,郁闷中

Toti 2007-01-06 01:05:43
declare @pagesize varchar(10)-----显示每页数据的大小
declare @pageindex varchar(20)-----当前索引页
declare @testtime datetime -------建立测试时间
select @pagesize=10,@pageindex=9000,@testtime=getdate()

declare @sql nvarchar(1000)
set @sql='select top '+@pagesize+' *,datediff(ms,'+cast(@testtime as varchar(30))+',getdate()) from #testtable where tid not in (select top '+cast(@pagesize * (@pageindex-1) as varchar(30))+' a.tid from #testtable a order by a.tid desc) order by tid desc'
exec (@sql)
---------------------------------------------------------------
我去掉datediff(ms,'+cast(@testtime as varchar(30))+',getdate())这句就没事,我想把查询用时统计出来,怎么写啊??注意查询用时可能会非常小!!
...全文
138 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
akuzou 2007-01-06
  • 打赏
  • 举报
回复
select top 10 *,datediff(ms,01 6 2007 1:14PM,getdate()) from #testtable where tid not in (select top 89990 a.tid from #testtable a order by a.tid desc) order by tid desc

取出你的SQL分析一下,datediff(ms,01 6 2007 1:14PM,getdate()),中间少了两个单引号,所以要加上单引号,而在动态语句中,两个单引号代替一个单引号
fish_yht 2007-01-06
  • 打赏
  • 举报
回复
要用引号引起来
因为你传进来的是个时间字符串,是字符串一定要加单引号撒
akuzou 2007-01-06
  • 打赏
  • 举报
回复
试试:
set @sql='select top '+@pagesize+' *,datediff(ms,'''+cast(@testtime as varchar(30))+''',getdate()) from #testtable where tid not in (select top '+cast(@pagesize * (@pageindex-1) as varchar(30))+' a.tid from #testtable a order by a.tid desc) order by tid desc'
Toti 2007-01-06
  • 打赏
  • 举报
回复
还真的可以呢,不明白??为什么要加上''?????
Toti 2007-01-06
  • 打赏
  • 举报
回复
zheninchangjiang 2007-01-06
  • 打赏
  • 举报
回复
datediff(ms,'''+cast(@testtime as varchar(30))+''',getdate())

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧