方法1:
CREATE TABLE #tabTmp(id int)
INSERT #tabTmp EXECUTE('SELECT id FROM '+@TableName)
declare curOne cursor for
SELECT id FROM #tabTmp
open curOne
FETCH NEXT from curOne into @ID
方法2
exec('
declare curOne cursor global
for' + @aSql)
open curOne
fetch curOne
方法3:
declare @sql varchar(2000),
@typefield varchar(100),
@tablename varchar(255),
@name varchar(100)
select @typefield = ''name'',@tablename = ''sysobjects''
set @sql = '' declare cuTemp cursor for ''
+'' SELECT DISTINCT ''+@typefield + '' FROM '' + @tablename
exec (@sql)
open cuTemp
fetch cuTemp into @name
while (@@fetch_status=0)
begin
print @name
fetch cuTemp into @name
end
close cuTemp
DEALLOCATE cuTemp
来源:http://www.cnblogs.com/sekihin/archive/2007/05/11/742797.html