sql 增加自增涨序号

anlianganl 2013-01-29 11:45:32
declare @str  varchar(2000)
declare @i int
set @i = 1
set @str=''

SELECT @str=@str+cast((@i+1) as CHAR(1))+isnull(tm.sAnswer,'')
FROM tbAnswer t
left outer join dbo.tbPollMyAnswer tm on tm.iAnswerId=t.iAnswerId
WHERE t.iQuestionID=12 and tm.sAnswer is not null
print @str


如上
想加一个 序号,1,2,3,4......
大神们给点指点
...全文
243 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
-Tracy-McGrady- 2013-01-29
  • 打赏
  • 举报
回复
num=rownumber()over(order by getdate())
Mr_Nice 2013-01-29
  • 打赏
  • 举报
回复
declare @str  varchar(2000)
declare @i int
set @i = 1
set @str=''
 
SELECT @str=@str+cast((@i+1) as CHAR(1))+isnull(tm.sAnswer,''),num=rownumber()over(order by getdate())
FROM tbAnswer t
left outer join dbo.tbPollMyAnswer tm on tm.iAnswerId=t.iAnswerId
WHERE t.iQuestionID=12 and tm.sAnswer is not null
print @str
kingtiy 2013-01-29
  • 打赏
  • 举报
回复 1

DECLARE @str VARCHAR(2000)
DECLARE @i INT
SET @i = 1
SET @str = ''
 
SELECT  @str = @str+ ROW_NUMBER()OVER(ORDER BY tm.sAnswer ) AS RID, + ISNULL(tm.sAnswer, '')
FROM    tbAnswer t
        LEFT OUTER JOIN dbo.tbPollMyAnswer tm ON tm.iAnswerId = t.iAnswerId
WHERE   t.iQuestionID = 12
        AND tm.sAnswer IS NOT NULL
PRINT @str

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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