Dynamic Python




Many times in programming you need to encorporate dynamic programming. Meaning that you want to apply a variable or many variables into a programming statement and run it. This is a pretty common need and can be achieved in MS SQL as follows:


DECLARE @DYNSQL VARCHAR(MAX)
DECLARE @TABLENAME VARCHAR(MAX)

IF OBJECT_ID('TEMPDB..#TEMP_A') IS NOT NULL DROP TABLE #TEMP_A
CREATE TABLE #TEMP_A
(
TABLENAME VARCHAR(MAX)
)


INSERT INTO #TEMP_A
SELECT 'SYS.all_objects'
UNION
SELECT 'SYS.all_views'
UNION
SELECT 'SYS.all_columns'



WHILE EXISTS(SELECT * FROM #TEMP_A)
BEGIN
	SET @TABLENAME = (SELECT TOP 1  TABLENAME  FROM #TEMP_A)
	SET @DYNSQL = 'SELECT * FROM ' +  @TABLENAME 
	EXEC(@DYNSQL)
	DELETE FROM #TEMP_A WHERE TABLENAME = @TABLENAME
END

DROP TABLE #TEMP_A




Here we can see that we have a static part of a query ‘SELECT * FROM ‘ and the dynamic part is the table name.

The beauty of this approach is we can achieve very complex query criteria and reduce our line of code counts by utalizing CURSORs and while loops, I prefer the ladder.

This same functionality is achievable in Python, here is a code sample :

prog = 'print("The sum of 5 and 10 is", (5+10))'
exec(prog)
#The sum of 5 and 10 is 15

expression = '5 + 3 * a'
a = 5
eval(expression)



Ian Fogelman

Ian Fogelman

My name is Ian Fogelman. I like to develop data driven solutions with SQL Server, Python, .NET and predictive analytics.