bizzydint Posted June 8, 2004 Posted June 8, 2004 I need to do a simple query retrieving the value from a particular column of the database...ie SELECT myColumn FROM myTable WHERE user_id = 100 But - the "myColumn" can vary and cant be hardcoded, so i'm passing the column name as a nvarchar parameter. exec('select ' + @colname + ' FROM myTable where user_id = 100') But...i then need to do something with that value that gets returned. How do i get hold of it? I cant seem to declare another variable and set it when using the "exec". Cheers. van Quote Grant me the serenity to accept the things I cannot change, the courage to change the things I cannot accept and the wisdom to hide the bodies of those people I had to kill today cos they pi**ed me off.
*Experts* Nerseus Posted June 8, 2004 *Experts* Posted June 8, 2004 Haven't tried this in awhile, but I think you need to use a temp table. Try something like: CREATE TABLE #temp ([iD] int) exec('insert into #temp select ' + @colname + ' FROM myTable where user_id = 100') SELECT [iD] FROM #temp -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
bizzydint Posted June 9, 2004 Author Posted June 9, 2004 perfect! just had to add a DROP TABLE #temp to the end. Cheers - you're a star! Quote Grant me the serenity to accept the things I cannot change, the courage to change the things I cannot accept and the wisdom to hide the bodies of those people I had to kill today cos they pi**ed me off.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.