I want to get the count of how many temporary
tables are used in a stored procedure
.
Thanks in advance
I want to get the count of how many temporary
tables are used in a stored procedure
.
Thanks in advance
Here is an algorithm to make this work for you:
Information_Schema.Routines
from the routine_definition column Declare @Def VarChar(Max) = ''
Select @Def = @Def + Routine_Definition
From Information_Schema.Routines
Read this great answer:
Turning a Comma Separated string into individual rows
Afterthought 1:
Now if you have [#One Two Three]
and [#One Two Three Four]
it will show up as #One
only, so you would have to use some TSQL trickery to strip spaces between square brackets.
Afterthought 2:
Will you have to distinguish between #Temp
and ##Temp
tables?
I don't really get why you want to do this but the following query might help.
This just counts the number of hashes that appear a stored procedures definition which is stored in the system view 'sys.sql_modules'.
SELECT
LEN(m.[definition]) -
LEN(REPLACE(m.[definition],'#','')) AS 'CountOf#Characters'
FROM
sys.sql_modules m
JOIN sys.procedures p
ON m.[object_id] = p.[object_id]
WHERE
p.[name] = 'Testing' -- <<the name of your stored procedure
Of course this assumes the procedure only contains CREATE TABLE #Table1 style statements. If hashes are used to drop the tables or even used in code comments you will need to take this into consideration with wrapping replace functions.