November 24, 2010 at 11:06 am
Hi,
I have Column Names in a SQL Table.
Now I want to use those Column Names in my Query but the issue which i am facing right now is
while executing the Query in Function some how i am getting it as string (as my return type of the function is char 🙂
Now What i want is to retrieve the Column Name and Use them in my query
1. SET @ColName=" Function which will return the Column name"
2. Select @Result=Select @Colname from <my Table>
Thanks in advance
Parth Rawal
November 24, 2010 at 11:13 am
1. SET @ColName=" Function which will return the Column name"
2. Select @Result='Select ' + @Colname + ' from <my Table>'
-- Cory
November 24, 2010 at 9:52 pm
You will have to use Dynamic SQL for the same
EXECUTE sp_executeSQL @result
Check for sp_executeSQL in Books Online or Google.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
November 25, 2010 at 4:30 am
Thanks a ton,
actually i have used both the answers in my stored procedure, one que though when i tried using the execute statement in my function it gave me an error as i can execute anything in function 🙁
Now I had tried using the "OPenQuery" but that also didint work properly, can anyone tell me how can i run the same in my function.
Thanks
Parth
November 25, 2010 at 4:46 am
You can't use Dynamic queries and temporary tables in a function. So, if you have to implement the Dynamic SQL you will have to use it in a stored procedure.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
November 25, 2010 at 6:38 am
That is what i am doing...:)
Anyways really thanks for the reply. This makes me confident about my approach 🙂
Thanks
Parth
November 25, 2010 at 8:53 pm
parth83.rawal (11/25/2010)
That is what i am doing...:)Anyways really thanks for the reply. This makes me confident about my approach 🙂
Thanks
Parth
You'll have to understand that we didn't think so because of what you wrote...
parth83.rawal (11/25/2010)
...when i tried using the execute statement in my function it gave me an error as i can execute anything in function..
--Jeff Moden
Change is inevitable... Change for the better is not.
November 25, 2010 at 9:25 pm
My Mistake and typo, as I meant to say CANT "Execute" anything in Function 🙂
November 26, 2010 at 6:17 am
Hi
1. SET @ColName= select function_name (@parameter)
2. Select @Result=' Select '+ @Colname +' from <my Table> '
3. execute (@Result)
Thanks
Siva Kumar
November 26, 2010 at 6:22 am
I would really like to hear you're logic behind this, it sounds like bad design to me.
November 26, 2010 at 7:08 am
Hi Dave
I think the query is answered!!!, would you need any more explanation about those 3 lines of code.
Regards
Siva Kumar J
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply