Sql Sever获取指定表中的所有字段

SQL Server采用存储过程,获取指定表中的所有字段名,并以“,”隔开,具体方法如下:

 1 SET ANSI_NULLS ON
 2 GO
 3 SET QUOTED_IDENTIFIER ON
 4 GO
 5 -- =============================================
 6 -- Author:        <may>
 7 -- Create date: <2012-05-09>
 8 -- Description:    <获取指定表中的字段>
 9 -- =============================================
10 Create PROCEDURE procGetColNames
11 @list varchar(1000) output
12 AS
13 BEGIN
14 declare @sql varchar(1000),@temp varchar(1000)
15 set @list=''
16 
17 --将表中的字段赋给输出变量@list
18 select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='Commodity'
19 
20 --将第一个","去掉,right(character_exp,int_xep):从右边开始起,返回指定长度的子字符串
21 select @list=right(@list,len(@list)-1)
22 end
23 GO

注意设置@list的初始值!

posted @ 2012-05-09 13:45  IT女汉纸  阅读(411)  评论(0编辑  收藏  举报