狐的传说
这是我写的一些代码。想法是(如其他人所述)是获取SP代码,对其进行修改并执行。但是,我的代码不会更改原始SP。第一步,获取SP的定义,去除“创建”部分,并在参数声明(如果存在)之后去除“ AS”。Declare @SPName varchar(250)Set nocount onDeclare @SQL Varchar(max), @SQLReverse Varchar(MAX), @StartPos int, @LastParameterName varchar(250) = '', @TableName varchar(36) = 'A' + REPLACE(CONVERT(varchar(36), NewID()), '-', '')Select * INTO #Temp from INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME = 'ADMIN_Sync_CompareDataForSync'if @@ROWCOUNT > 0 BEGIN Select @SQL = REPLACE(ROUTINE_DEFINITION, 'CREATE PROCEDURE [' + ROUTINE_SCHEMA + '].[' + ROUTINE_NAME + ']', 'Declare') from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = @SPName Select @LastParameterName = PARAMETER_NAME + ' ' + DATA_TYPE + CASE WHEN CHARACTER_MAXIMUM_LENGTH is not null THEN '(' + CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX' ELSE CONVERT(varchar,CHARACTER_MAXIMUM_LENGTH) END + ')' ELSE '' END from #Temp WHERE ORDINAL_POSITION = (Select MAX(ORDINAL_POSITION) From #Temp) Select @StartPos = CHARINDEX(@LastParameterName, REPLACE(@SQL, ' ', ' '), 1) + LEN(@LastParameterName) ENDelse Select @SQL = REPLACE(ROUTINE_DEFINITION, 'CREATE PROCEDURE [' + ROUTINE_SCHEMA + '].[' + ROUTINE_NAME + ']', '') from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = @SPNameDROP TABLE #TempSelect @StartPos = CHARINDEX('AS', UPPER(@SQL), @StartPos)Select @SQL = STUFF(@SQL, @StartPos, 2, '')(请注意,基于唯一标识符创建了新的表名)现在,假设这是执行返回结果集的select的代码,请在代码中找到最后一个“ From”字。Select @SQLReverse = REVERSE(@SQL)Select @StartPos = CHARINDEX('MORF', UPPER(@SQLReverse), 1)更改代码以将结果集选择到表中(该表基于uniqueidentifier)Select @StartPos = LEN(@SQL) - @StartPos - 2Select @SQL = STUFF(@SQL, @StartPos, 5, ' INTO ' + @TableName + ' FROM ')EXEC (@SQL)结果集现在在表中,表是否为空都没有关系!让我们获取表的结构Select * from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName您现在可以用这个来做魔术了别忘了删除该唯一表Select @SQL = 'drop table ' + @TableNameExec (@SQL)希望这可以帮助!