select top @STATES id from [table1]

haya8721 2004-07-23 02:34:02
这句应该如何改下呢,或用其它方法实现呢
...全文
122 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
青团子 2004-07-23
  • 打赏
  • 举报
回复
create table #tmp1(ID int)
exec('insert into #tmp1 select top '+@STATES+' id from [table1] ')
select * from #tmp1

那你这样试试看
haya8721 2004-07-23
  • 打赏
  • 举报
回复
to : futulove(福途£爱)
set @STATES=你要的数字
set rowcount=@STATES ---???????
select * into #temp1 from table1
好像没有这种语法吧,能不能说说详细点

to: Cityfire(青团子)
select top 2 * from [table1] where [id] in (select top @STATES id from [table1] order by dt) order by dt desc
我的这个子查询,主要功能是选出经过排序后特定位置的两条记录,变量@states标明的是位置,用刚才那个exec('select top '+@STATES+' id from [table1] ')确实运行是正确的,w但是我这边用不上,所以我想到先把exec('select top '+@STATES+' id from [table1] ')的结果存进一张临时表,因此想用exec('select top '+@STATES+' id into #temp1 from [table1] ')来实现,但是好像无法创建对象
我感觉我这个方法笨了点,有没有其它的方法呢,能不能指点一二
青团子 2004-07-23
  • 打赏
  • 举报
回复
嗯?
偶这边运行是成功的呀……
futulove 2004-07-23
  • 打赏
  • 举报
回复
exec 不能这样top 的,要这样
declare @STATES int
set @STATES=你要的数字
set rowcount=@STATES
select * into #temp1 from table1
haya8721 2004-07-23
  • 打赏
  • 举报
回复
可是如果
exec('select top '+@STATES+' id into #temp1 from [table1] ')
的话,无法创建#temp1
青团子 2004-07-23
  • 打赏
  • 举报
回复
CREATE PROCEDURE selnum_getID
@STATES int
AS
declare @SQL VarChar(8000)
Set @SQL='select top '+CAST(@top As VarChar(20))+' id from [table1]'
Exec (@SQL)
GO
青团子 2004-07-23
  • 打赏
  • 举报
回复
exec('select top '+@STATES+' id from [table1] ')

34,590

社区成员

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

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