Last active
October 16, 2015 14:20
-
-
Save ycaroafonso/e1bf64344f7ab69aa2d7 to your computer and use it in GitHub Desktop.
Converte procedure para tabela
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @NomeProcedure VARCHAR(200)= 'Nome da Tabela', | |
@NomeSchema VARCHAR(200)= 'dbo' | |
DECLARE @SqlProcedure VARCHAR(MAX)= '', | |
@Sql VARCHAR(MAX)= '' | |
SELECT @SqlProcedure += ', ' + PARAMETER_NAME + ' = ' + CASE DATA_TYPE | |
WHEN 'datetime' | |
THEN '''' + CONVERT(VARCHAR(10), GETDATE(), 120) + '''' | |
WHEN 'int' THEN '0' | |
ELSE 'NULL' | |
END | |
FROM information_schema.parameters | |
WHERE SPECIFIC_NAME = @NomeProcedure | |
ORDER BY ORDINAL_POSITION | |
SET @SqlProcedure = @NomeSchema + '.' + @NomeProcedure + ' ' + STUFF(@SqlProcedure, 1, 2, '') | |
SELECT @Sql += ', ' + name + ' ' + system_type_name | |
FROM sys.dm_exec_describe_first_result_set_for_object(OBJECT_ID(@NomeSchema + '.' + @NomeProcedure), NULL) | |
SELECT 'DECLARE @Tabela TABLE (' + STUFF(@Sql, 1, 2, '') + ') INSERT INTO @Tabela ' | |
UNION ALL | |
SELECT 'EXEC ' + @SqlProcedure | |
UNION ALL | |
SELECT '; SELECT * FROM @Tabela' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment