Content

Retrieving Values from SQL Exec()

5 May

+ 0

Never had a good reason to do this up until now, but if you need to get a value from a generated sql string, there are a few hoops you must jump through. Example:

declare @table 		varchar(20) 	,@username 	varchar(30)

--can't use double quotes w/out this :) SET QUOTED_IDENTIFIER OFF

--set your variablesselect @table = 'tblcustomers'	,@username = 'webmaster'

--create a temp table to hold your valuecreate table #temp (field varchar(50))

--insert the value into temp tableinsert into #tempexec ('select email from ' + @table + ' where username = "' + @username + '"')

--voilaselect field from #temp

drop table #temp

Reference: Getting Creative with EXEC and EXEC()

Comment

#

I reserve the right to delete and / or modify the comments that contain inappropriate language, spam or other behavior not appropriate in a civilized community. If your comment does not appear, it may be that akismet caught it.