SET NOCOUNT ON
declare @procfunctable as table (dbname varchar(64),schemaname varchar(128),
storedprocname varchar(128),
[type] varchar(10),parameterid varchar(128),
parametername varchar(128),parameterdatatype varchar(128),
isoutputparameter int)
insert into @procfunctable
execute sp_MSforeachdb N'use ?; SELECT ''?'',
schema_name(schema_id) as schemaname,SO.name,
SO.type,coalesce(P.parameter_id,0) as [ParameterID],
coalesce(P.name,''NO PARAMETER'') as [ParameterName],
type_name(P.user_type_id),P.is_output
from sys.all_objects as SO
LEFT OUTER JOIN sys.parameters as P
ON SO.Object_id=P.object_id
where so.object_id in (select object_id
from sys.objects where type in(''P'',''FN'')
and is_ms_shipped=0
and not exists(
select ep.[major_id]
from [sys].[extended_properties] ep
where ep.[major_id]=SO.[object_id]
and ep.[minor_id]=0
and ep.[class]=1
and ep.[name]=N''microsoft_database_tools_support'')
)
order by [schemaname],so.name,P.parameter_id'
--select * from @procfunctable
declare @viewstable as table (dbname varchar(64),schemaname varchar(128),
viewname varchar(128),
[type] varchar(20))
insert into @viewstable
execute sp_MSforeachdb N'use ?; SELECT ''?'',
schema_name(schema_id) as schemaname,SO.name,
so.type
from sys.all_objects as so
where so.object_id in (select object_id from sys.objects where type in (''V'')
and is_ms_shipped=0)
order by [schemaname],so.name'
--select * from @viewstable
declare @finaletable as table (dbname varchar(64),schemaname varchar(128),
objectname varchar(128),
[objecttype] varchar(128),result varchar(128),
errormessage varchar(4000))
insert into @finaletable
select dbname,schemaname,storedprocname,
case type
when 'p' then 'SQL stored procedure'
when 'FN' then 'SQL scalar function'
end as type,
null,'no error'
from @procfunctable
where dbname not in ('master','tempdb','model','msdb','dba')
insert into @finaletable select dbname,schemaname,viewname,'view',null,'no error'
from @viewstable where dbname not in ('master','tempdb','model','msdb','dba')
--select * from @finaletable
declare listofobjects cursor fast_forward
for
select dbname,schemaname,objectname,objecttype
from @finaletable
order by [objecttype]
declare @dbname varchar(128),@schemaname nvarchar(4000),@objectname
nvarchar(4000),@objecttype nvarchar(4000),@result nvarchar='';
open listofobjects
fetch next from listofobjects into @dbname,@schemaname,@objectname,@objecttype
while @@FETCH_STATUS = 0
begin
if(@objecttype='SQL stored procedure' or @objecttype='SQL scalar function')
begin
declare listofparameters cursor fast_forward
for
select dbname,parametername,parameterdatatype,isoutputparameter,parameterid from
@procfunctable
where storedprocname=@objectname and dbname=@dbname and schemaname=@schemaname
declare @statement nvarchar(500)='',@parametername nvarchar(4000),@isoutputparameter
nvarchar(4000),@paramtype varchar(100),@parameterid nvarchar(40),@errormessge
nvarchar(max);
open listofparameters
fetch next from listofparameters into
@dbname,@parametername,@paramtype,@isoutputparameter,@parameterid
DECLARE @parameter nvarchar(4000)='',@outputparamdeclare
nvarchar(4000)='',@outputparameter nvarchar(4000)='',@paramdefinition
nvarchar(4000)='',@parametersetting nvarchar(4000)='',@date nvarchar(200)='20170619'
while @@FETCH_STATUS=0
Begin
if(@parameter='varchar' and @isoutputparameter=0)
set @parameter =@parameter+'''abcd'','
else if(@parameter='int' and @isoutputparameter=0)
set @parameter =@parameter+'''23'','
else if (@objecttype='SQL stored procedure' and @isoutputparameter=1)
begin
set @outputparamdeclare=@outputparamdeclare+'declare '+'@parameter'+@parameterid+'
'+@paramtype + char(10)
set @parameter=@parameter + '@parameter' + @parameterid +' '+'output, '
end
fetch next from listofparameters into
@dbname,@parametername,@paramtype,@isoutputparameter,@parameterid
end
begin try
set @parameter=
case @parameter when null then null
else (
case len(@parameter) when 0 then @parameter
else left(@parameter,len(@parameter)-1)
end
)end
set @outputparamdeclare=
case @outputparamdeclare when null then null
else (
case len(@outputparamdeclare) when 0 then @outputparamdeclare
else left(@outputparamdeclare,len(@outputparamdeclare)-1)
end
)end
set @outputparameter =
case @outputparameter when null then null
else (
case len(@outputparameter) when 0 then @outputparameter
else left(@outputparameter,len(@outputparameter)-1)
end
)end
if(@objecttype='SQL stored procedure'and @isoutputparameter=0)
begin
declare @sqlquery nvarchar(4000),@sqlquerywithparameter nvarchar(4000);
set @sqlquery='['+@dbname+']'+'.'+'['+@schemaname+']'+'.'+'['+@objectname+']'
set
@sqlquerywithparameter='['+@dbname+']'+'.'+'['+@schemaname+']'+'
.'+'['+@objectname+']'+'
'+@parameter
--set @statement=N'EXEC sp_refreshsqlmodule '''+@sqlquery+'''';
--execute @result=sp_executesql @statement
execute @result=sp_executesql @sqlquerywithparameter
end
else if (@objecttype='SQL stored procedure' and @isoutputparameter=1)
begin
set @statement='exec '+'['+@dbname+']'+'.'+'['+@schemaname+']'+'.
'+'['+@objectname+']'+' '+@parameter
declare @finalstatement varchar(max);
set @finalstatement=@outputparamdeclare+';'+@statement;
execute(@finalstatement)
end
else if (@objecttype='SQL scalar function' and @isoutputparameter=0)
begin
declare @funcquerywithparam nvarchar(max),@funcquery nvarchar(max),@command
nvarchar(max);
set @funcquerywithparam='['+@dbname+']'+'.'+'['+@schemaname+']'+'
.'+'['+@objectname+']'+' '+@parameter
set @funcquery='['+@dbname+']'+'.'+'['+@schemaname+']'+'.'+'['+@objectname+']'
execute @result=sp_executesql @funcquerywithparam
end
else if(@paramtype is null and @isoutputparameter is null)
begin
set @result=1
update @finaletable
set errormessage='no parameter available'
where objectname=@objectname
end
update @finaletable
set result=@result
where objectname=@objectname
end try
begin catch
update @finaletable
set errormessage=ERROR_MESSAGE(),result=1
where objectname=@objectname
end catch
close listofparameters
deallocate listofparameters
end
if (@objecttype='view')
begin
begin try
declare @viewquery nvarchar(max),@viewname nvarchar(max)
set @viewname=@dbname+'.'+@schemaname+'.'+@objectname
set @viewquery='select top 1 * from '+@viewname
execute @result=sp_executesql @viewquery
update @finaletable
set result=@result
where objectname=@objectname
end try
begin catch
print'error found in '+@objectname
update @finaletable
set errormessage=ERROR_MESSAGE(),result=1
where objectname=@objectname
end catch
end
fetch next from listofobjects into @dbname,@schemaname,@objectname,@objecttype
end
close listofobjects
deallocate listofobjects
select distinct dbname,schemaname,objectname,[objecttype],
case result
when 0 then 'pass'
when 1 then 'fail'
end as result,
errormessage
from @finaletable
order by [objecttype]
set nocount off