用sql 得到某表下的列名

Declare @TabName varchar(100)
Declare @Cols varchar(1000)
Declare @tmpCol varchar(100)

Set @TabName ='StuSources'
SELECT @Cols = (SELECT COL_NAME(OBJECT_ID(@TabName),1) )
Set @tmpCol = ''

Declare @i int
Set @i=2

WHILE @i > 0
Begin

   SELECT @tmpCol = ISNULL((SELECT COL_NAME(OBJECT_ID(@TabName),@i)),'')

   if (@tmpCol<>'')
      begin
     set @Cols = @Cols +','+ @tmpCol
     set @tmpCol = ''
     set @i = @i + 1
      end
   else
  Set @i = 0 --退出循环
End

print @Cols

posted on 2006-12-31 16:11  freeliver54  阅读(3329)  评论(3编辑  收藏

导航