Tuesday, March 1, 2011

getting dynamic results from a dynamic query in sql server

file this under "hack-tacular" or "kludge-errific" -- if you need to get a dynamic result of a dynamic sql statement, you can use this method.  the reasons behind why we're using dynamic sql and getting dynamic values are not important right now :)

                create table #tmp_hack_for_getting_value (val varchar(100))

SET @value_sql = '
truncate table #tmp_hack_for_getting_value
declare @v varchar(100)
SELECT @v = (' + ISNULL(@record_count_sql,'0') + ')
INSERT INTO #tmp_hack_for_getting_value (val) values (@v)
'


exec(@value_sql)
SELECT @value = val FROM #tmp_hack_for_getting_value

No comments:

Post a Comment