IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[get_columns]') AND type = 'P')
DROP PROCEDURE [dbo].[get_columns]
GO
CREATE PROCEDURE [dbo].[get_columns]
@tableName varchar(100),
@excludeFileds varchar(1000)='',
@result varchar(1000)='' out
AS
BEGIN
SET NOCOUNT ON;
--生成SQL
declare @databaseName varchar(100),@index int,@sql nvarchar(max)
set @databaseName='sys.columns'
set @result=''
--判断是否是带了数据库名称的,如果带了则取出
set @index = CHARINDEX('.dbo.',@tableName)
if @index > 0
begin
set @databaseName=SUBSTRING(@tableName,1,@index-1)+ '.sys.columns'
end
--处理exclude
set @excludeFileds='('''+REPLACE(@excludeFileds,',',''',''')+''')'
set @sql='select @result+=name+'','' from '+@databaseName +' where object_id=object_id('''+@tableName+''') and name not in '+@excludeFileds
exec sp_executesql @sql,N'@result varchar(1000) out',@result out
set @result=SUBSTRING(@result,1,LEN(@result)-1)
print @result
END