复制代码 代码如下:
create proc p_sword_getblcolumn
(
@tblName varchar(200),
@fromIndex int,
@toIndex int,
@columnName varchar(3000) output
)
as
begin
declare @tempColumn varchar(3000)
declare @errMsg varchar(200)
declare @i int
set @i=1
set @columnName=''
set @errMsg=''
declare tempColumnCur cursor for
select syscolumns.name from syscolumns join sysobjects on syscolumns.id = sysobjects.id
where sysobjects.name =@tblName order by syscolumns.colorder
open tempColumnCur
fetch next from tempColumnCur into @tempColumn
while @@FETCH_STATUS=0
begin
if(@fromIndex=0 and @toIndex=0)
begin
set @columnName=@columnName+','+@tempColumn
end
if(@fromIndex=0 and @toIndex<>0)
begin
if(@i<=@toIndex)
set @columnName=@columnName+','+@tempColumn
end
else if(@fromIndex <>0 and @toIndex=0)
begin
if(@i>=@fromIndex)
set @columnName=@columnName+','+@tempColumn
end
else if(@i>=@fromIndex and @i<=@toIndex)
begin
set @columnName=@columnName+','+@tempColumn
end
set @i=@i+1
print @i
fetch next from tempColumnCur into @tempColumn
end
close tempColumnCur
deallocate tempColumnCur
set @columnName=SUBSTRING(@columnName,2,len(@columnName))
print @columnName
if(@@ERROR<>0)
begin
set @errMsg='get column error '
goto errorproc
end
else
return 0
end
errorproc:
begin
raiserror(@errMsg,16,1)
return 1
end
go
新闻热点
疑难解答