Home All Groups Group Topic Archive Search About

Dynamic Execute in Function?

Author
30 Sep 2005 8:54 PM
xenophon
Is it possible to dynamically execute SQL in a Function?

    Set @selectit = ' Select Startdate from [dbo].[' + @tablename
+ '] Where testcol=113'
            Exec (@selectit )

Is what I want to do but Query Analyzer won't let me do it.

SQL Server 2000.

Thanks.

Author
30 Sep 2005 9:36 PM
Hugo Kornelis
On Fri, 30 Sep 2005 16:54:52 -0400, xenophon wrote:

>
>Is it possible to dynamically execute SQL in a Function?

Hi xenophon,

No, that's not possible. See "User-Defined Functions" in Books Online.

Why would you want to do this anyway? Why don't you know the name of
your table?

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Author
3 Oct 2005 4:35 AM
Sophie Guo [MSFT]
Hello,

You are unable to use it in Function. However, you can use it in stored
procedure. The sample code for your reference:

create proc aaa (@tablename varchar(10) )
as
begin

declare @sec varchar(300)
set @sec= ' Select customerID from ' + @tablename + ' Where
customerID="ALFKI"'
exec (@sec)

end

exec aaa 'customers'

I hope the information is helpful.

Sophie Guo
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security

=====================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
2 Oct 2005 1:21 AM
--CELKO--
WHO CARES IF IT POSSIBLE???

No programmer who has has a **basic** software engineering course would
use dynamic SQL in the middle of a procedure.  Remember the terms
"coupling " and "cohesion"??

AddThis Social Bookmark Button