IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_proc_params_default]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_proc_params_default]
GO
CREATE PROCEDURE [dbo].[mps_gen_proc_params_default]
@tablename varchar(100),
@excludefileds varchar(max),
@result varchar(max) out
AS
BEGIN
SET NOCOUNT ON;
declare @sql nvarchar(max)
declare @tbname nvarchar(100)
declare @dbname nvarchar(100)
declare @index int
declare @s varchar(max)
declare @s1 varchar(max)
declare @parsedfields varchar(max)

--分离数据库名称和表名称
set @index=CHARINDEX('.',@tablename)
if @index > 0
begin
set @dbname=SUBSTRING(@tablename,1,@index-1)+'.sys.';
set @tbname=SUBSTRING(@tablename,@index+1,LEN(@tablename)-@index);
set @index=CHARINDEX('.',@tbname)
set @tbname=SUBSTRING(@tbname,@index+1,LEN(@tbname)-@index);
end else begin
set @dbname='sys.'
set @tbname=@tablename
end

--分离不包含的字段名称
set @parsedfields=''
set @s=@excludefileds
while (@s>'') 
begin
set @index=CHARINDEX(',',@s)
if (@index>0)
begin
set @s1 = substring(@s,1,@index-1) 
set @s  = substring(@s,@index+1,len(@s)-@index)
end else 
begin
set @s1 = @s  
set @s = '' 
end 
if (@s1>'')
begin
set @parsedfields += ''''+@s1+''''+','
end
end
if(LEN(@parsedfields) >1)
set @parsedfields=SUBSTRING(@parsedfields,1,LEN(@parsedfields)-1);
else begin
set @parsedfields+=''''''
end

--组装SQL语句
set @result=''
set @sql='
select @result+=char(9)+''@''+_columns.name
   +'' ''+_types.name
   +case when _types.name in(''decimal'',''numeric'') then ''(''+CONVERT(NVARCHAR,_columns.precision)+'',''+CONVERT(NVARCHAR,_columns.scale)+'')''
         when _types.name in(''char'',''nchar'',''varchar'',''nvarchar'') then ''(''+CASE WHEN _columns.max_length=-1 THEN ''MAX'' ELSE CONVERT(NVARCHAR,case when _types.name in(''nchar'',''nvarchar'')  then _columns.max_length / 2 else _columns.max_length end ) END +'')'' else '''' end
   +''=default,''+char(13)+char(10)
from '+@dbname+'columns _columns,sys.types _types
where object_id=OBJECT_ID('''+@tablename+''') 
and _columns.user_type_id=_types.user_type_id
and _columns.name not in('+@parsedfields+')
if LEN(@result) > 3
set @result=SUBSTRING(@result,1,LEN(@result)-3)'
--print @sql
exec sp_executesql @sql, N'@result nvarchar(max) out', @result out
END