1、用SP_EXECUTESQL试试
declare @sort Nvarchar(50)
declare @intID int
set @sort = N'user_id'
declare @sql Nvarchar(250)
set @sql = 'select @intID=res_id from res where parent_dir = 1 order by '
set @sql = @sql + @sort
execute sp_executesql @sql,N'@intID int output',@intId output2、select (select count(*) from table1 where res_id<=a.res_id) as 序号,res_id,res_name from table1 a
declare @sort Nvarchar(50)
declare @intID int
set @sort = N'user_id'
declare @sql Nvarchar(250)
set @sql = 'select @intID=res_id from res where parent_dir = 1 order by '
set @sql = @sql + @sort
execute sp_executesql @sql,N'@intID int output',@intId output2、select (select count(*) from table1 where res_id<=a.res_id) as 序号,res_id,res_name from table1 a
2,结果放入一临时表#temp里,用indentity(1,1)字段
select * into #temp from result where 1=2
alter #temp add column(id,identity(1,1)
insert into #temp
select * from result
select id from #temp
1. 所返回的 res_id 是随机的,怎么使返回的 res_id 是指定的记录的 res_id?2.执行成功,结果正确,就是在速度上比较慢。OpenVMS(半知半解) 的方法也不错。
则给@intID赋了最后一条记录的值!
我在前面使用了一个定位,set rowcount 10 那么应该是第10条记录的ID,如:
set rowcount 10
select @intID=res_id from res where parent_dir =1 order by user_id
这样就能正确找出来了。可是用了 sp_executesql 就是不行了。
alter #temp add column(id,identity(int,1,1))
能改成一句。
select identity(int,1,1) as id,* into #temp from result where 1=2
第二个问题用foolishchao(亚超) 的方法很不错!
第二个问题用foolishchao(亚超) 的方法很不错!