sqlserver 数据库获取talbe的列名

与客户或者厂商做接口对接的时候,经常需要具体到表名与列名,对于已经有数据结构的成熟体系来讲,可以通过数据库里存储过程来解决,下面分享个人程序,方便以后自己查看!

 

 

Create procedure SP_OutPutColumn
@name nvarchar(50)='spt_monitor' ---TableName
as
--select  * from spt_monitor   Modify by Peter 2019/7/16
--declare @Object_id nvarchar(18)
--select @Object_id=object_id from sys.all_objects where type='U' and  name=@name

declare @column nvarchar(max)
declare @str nvarchar(50)
set @column=''
declare Select_Column cursor for  select name from sys.all_columns where object_id=Object_id(@name)
open Select_Column
fetch next  from Select_Column into @str
while (@@FETCH_STATUS=0)
begin
select @column=@column+@str+','+char(9)+char(13)
--print @column
fetch next  from Select_Column into @str
--print @str
end
close Select_Column
deallocate Select_Column

print @column


select * from sys.all_objects where type='P'

你可能感兴趣的:(数据库)