根据表名和索引获取需要的列名的存储过程
前端之家收集整理的这篇文章主要介绍了
根据表名和索引获取需要的列名的存储过程,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<div class="codetitle"><a style="CURSOR: pointer" data="36063" class="copybut" id="copybut36063" onclick="doCopy('code36063')"> 代码如下:
<div class="codebody" id="code36063">
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