User-defined function and dynamic SQL.

User-defined function and dynamic SQL.

Post by John Peterso » Fri, 09 Nov 2001 13:50:13



(SQL Server 2000, SP1)

Hello, all!

I'm sure this question has been asked, but a cursory glance in this forum
and on the Internet didn't yield in the answer for me, so I thought I'd post
it here.

I'm trying to create a user-defined function that I can use within a SELECT
statement that will effectively use a dynamically created SQL statement to
return a value:


varchar(8000)
as
begin


convert(varchar(8000), SomeField) from SomeTable */

      begin



      end


end

But, upon compilation, I'm notified that the EXECUTE statement can't be used
with anything other than an extended stored procedure.  So then I tried to
modify this to use temporary tables.  Again, it appears as if user-defined
functions can't leverage temporary tables.  Short of creating an extended
stored procedure, is there any way to get a user-defined function to execute
some dynamic SQL?

Thanks for any help you can provide!  :-)

John Peterson

 
 
 

User-defined function and dynamic SQL.

Post by BP Margoli » Fri, 09 Nov 2001 14:05:40


John,

You should be able to use table variables in place of temporary tables.

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


> (SQL Server 2000, SP1)

> Hello, all!

> I'm sure this question has been asked, but a cursory glance in this forum
> and on the Internet didn't yield in the answer for me, so I thought I'd
post
> it here.

> I'm trying to create a user-defined function that I can use within a
SELECT
> statement that will effectively use a dynamically created SQL statement to
> return a value:


> varchar(8000)
> as
> begin


> convert(varchar(8000), SomeField) from SomeTable */

>       begin



>       end


> end

> But, upon compilation, I'm notified that the EXECUTE statement can't be
used
> with anything other than an extended stored procedure.  So then I tried to
> modify this to use temporary tables.  Again, it appears as if user-defined
> functions can't leverage temporary tables.  Short of creating an extended
> stored procedure, is there any way to get a user-defined function to
execute
> some dynamic SQL?

> Thanks for any help you can provide!  :-)

> John Peterson


 
 
 

User-defined function and dynamic SQL.

Post by John Peterso » Fri, 09 Nov 2001 14:31:53


Hello, BP!

Thanks for the quick response!  Though...I must confess my ignorance when it
comes to table variables.  Could you give me an example of said, or a link?

Thanks again!

John Peterson


> John,

> You should be able to use table variables in place of temporary tables.

> -------------------------------------------
> BP Margolin
> Please reply only to the newsgroups.
> When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
> can be cut and pasted into Query Analyzer is appreciated.



> > (SQL Server 2000, SP1)

> > Hello, all!

> > I'm sure this question has been asked, but a cursory glance in this
forum
> > and on the Internet didn't yield in the answer for me, so I thought I'd
> post
> > it here.

> > I'm trying to create a user-defined function that I can use within a
> SELECT
> > statement that will effectively use a dynamically created SQL statement
to
> > return a value:


> > varchar(8000)
> > as
> > begin


> > convert(varchar(8000), SomeField) from SomeTable */

> >       begin



> >       end


> > end

> > But, upon compilation, I'm notified that the EXECUTE statement can't be
> used
> > with anything other than an extended stored procedure.  So then I tried
to
> > modify this to use temporary tables.  Again, it appears as if
user-defined
> > functions can't leverage temporary tables.  Short of creating an
extended
> > stored procedure, is there any way to get a user-defined function to
> execute
> > some dynamic SQL?

> > Thanks for any help you can provide!  :-)

> > John Peterson

 
 
 

User-defined function and dynamic SQL.

Post by BP Margoli » Fri, 09 Nov 2001 14:55:07


John,

Check out the "CREATE FUNCTION" (tsqlref.chm::/ts_create_7r1l.htm) section
in the SQL Server 2000 Books Online, examples B and C.

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


> Hello, BP!

> Thanks for the quick response!  Though...I must confess my ignorance when
it
> comes to table variables.  Could you give me an example of said, or a
link?

> Thanks again!

> John Peterson



> > John,

> > You should be able to use table variables in place of temporary tables.

> > -------------------------------------------
> > BP Margolin
> > Please reply only to the newsgroups.
> > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.)
which
> > can be cut and pasted into Query Analyzer is appreciated.



> > > (SQL Server 2000, SP1)

> > > Hello, all!

> > > I'm sure this question has been asked, but a cursory glance in this
> forum
> > > and on the Internet didn't yield in the answer for me, so I thought
I'd
> > post
> > > it here.

> > > I'm trying to create a user-defined function that I can use within a
> > SELECT
> > > statement that will effectively use a dynamically created SQL
statement
> to
> > > return a value:


> > > varchar(8000)
> > > as
> > > begin


> > > convert(varchar(8000), SomeField) from SomeTable */

> > >       begin



> > >       end


> > > end

> > > But, upon compilation, I'm notified that the EXECUTE statement can't
be
> > used
> > > with anything other than an extended stored procedure.  So then I
tried
> to
> > > modify this to use temporary tables.  Again, it appears as if
> user-defined
> > > functions can't leverage temporary tables.  Short of creating an
> extended
> > > stored procedure, is there any way to get a user-defined function to
> > execute
> > > some dynamic SQL?

> > > Thanks for any help you can provide!  :-)

> > > John Peterson

 
 
 

User-defined function and dynamic SQL.

Post by John Peterso » Sat, 10 Nov 2001 00:13:42


Hello, BP!

Well...I took a peek at that section, but I'm still not getting it.  Here's
what I would have done if I could have used temporary tables in this
function:


returns varchar(8000)
as
begin

   create table #Value (Value varchar(8000))


   drop table #Value


end

As you can see...I'm looking for some mechanism with which to execute some
dynamically generated SQL from within a function, and to retrieve the
results of that dynamically generated SQL (I'm guaranteed that there will
always be one row returned).

I would welcome any additional thoughts you might have!  Thanks for your
help thus far!  :-)

John Peterson


> John,

> Check out the "CREATE FUNCTION" (tsqlref.chm::/ts_create_7r1l.htm) section
> in the SQL Server 2000 Books Online, examples B and C.

> -------------------------------------------
> BP Margolin
> Please reply only to the newsgroups.
> When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
> can be cut and pasted into Query Analyzer is appreciated.



> > Hello, BP!

> > Thanks for the quick response!  Though...I must confess my ignorance
when
> it
> > comes to table variables.  Could you give me an example of said, or a
> link?

> > Thanks again!

> > John Peterson



> > > John,

> > > You should be able to use table variables in place of temporary
tables.

> > > -------------------------------------------
> > > BP Margolin
> > > Please reply only to the newsgroups.
> > > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.)
> which
> > > can be cut and pasted into Query Analyzer is appreciated.



> > > > (SQL Server 2000, SP1)

> > > > Hello, all!

> > > > I'm sure this question has been asked, but a cursory glance in this
> > forum
> > > > and on the Internet didn't yield in the answer for me, so I thought
> I'd
> > > post
> > > > it here.

> > > > I'm trying to create a user-defined function that I can use within a
> > > SELECT
> > > > statement that will effectively use a dynamically created SQL
> statement
> > to
> > > > return a value:


> > > > varchar(8000)
> > > > as
> > > > begin


> > > > convert(varchar(8000), SomeField) from SomeTable */

> > > >       begin



> > > >       end


> > > > end

> > > > But, upon compilation, I'm notified that the EXECUTE statement can't
> be
> > > used
> > > > with anything other than an extended stored procedure.  So then I
> tried
> > to
> > > > modify this to use temporary tables.  Again, it appears as if
> > user-defined
> > > > functions can't leverage temporary tables.  Short of creating an
> > extended
> > > > stored procedure, is there any way to get a user-defined function to
> > > execute
> > > > some dynamic SQL?

> > > > Thanks for any help you can provide!  :-)

> > > > John Peterson

 
 
 

User-defined function and dynamic SQL.

Post by John Peterso » Sat, 10 Nov 2001 01:40:41


Heya, BP!

Well...I took a closer look at the table variable example and tried to
rewrite my function to be:


returns varchar(8000)
as
begin





end

However, I get an error indicating that EXECUTE cannot be used as a source
for a table variable.  <sigh>  I feel like I'm thwarted at every turn...

Any other insight you might have would be *much* appreciated.  Thanks!  :-)

John Peterson


> Hello, BP!

> Well...I took a peek at that section, but I'm still not getting it.
Here's
> what I would have done if I could have used temporary tables in this
> function:


> returns varchar(8000)
> as
> begin

>    create table #Value (Value varchar(8000))


>    drop table #Value


> end

> As you can see...I'm looking for some mechanism with which to execute some
> dynamically generated SQL from within a function, and to retrieve the
> results of that dynamically generated SQL (I'm guaranteed that there will
> always be one row returned).

> I would welcome any additional thoughts you might have!  Thanks for your
> help thus far!  :-)

> John Peterson



> > John,

> > Check out the "CREATE FUNCTION" (tsqlref.chm::/ts_create_7r1l.htm)
section
> > in the SQL Server 2000 Books Online, examples B and C.

> > -------------------------------------------
> > BP Margolin
> > Please reply only to the newsgroups.
> > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.)
which
> > can be cut and pasted into Query Analyzer is appreciated.



> > > Hello, BP!

> > > Thanks for the quick response!  Though...I must confess my ignorance
> when
> > it
> > > comes to table variables.  Could you give me an example of said, or a
> > link?

> > > Thanks again!

> > > John Peterson



> > > > John,

> > > > You should be able to use table variables in place of temporary
> tables.

> > > > -------------------------------------------
> > > > BP Margolin
> > > > Please reply only to the newsgroups.
> > > > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.)
> > which
> > > > can be cut and pasted into Query Analyzer is appreciated.



> > > > > (SQL Server 2000, SP1)

> > > > > Hello, all!

> > > > > I'm sure this question has been asked, but a cursory glance in
this
> > > forum
> > > > > and on the Internet didn't yield in the answer for me, so I
thought
> > I'd
> > > > post
> > > > > it here.

> > > > > I'm trying to create a user-defined function that I can use within
a
> > > > SELECT
> > > > > statement that will effectively use a dynamically created SQL
> > statement
> > > to
> > > > > return a value:


> > > > > varchar(8000)
> > > > > as
> > > > > begin


> > > > > convert(varchar(8000), SomeField) from SomeTable */

> > > > >       begin



> > > > >       end


> > > > > end

> > > > > But, upon compilation, I'm notified that the EXECUTE statement
can't
> > be
> > > > used
> > > > > with anything other than an extended stored procedure.  So then I
> > tried
> > > to
> > > > > modify this to use temporary tables.  Again, it appears as if
> > > user-defined
> > > > > functions can't leverage temporary tables.  Short of creating an
> > > extended
> > > > > stored procedure, is there any way to get a user-defined function
to
> > > > execute
> > > > > some dynamic SQL?

> > > > > Thanks for any help you can provide!  :-)

> > > > > John Peterson

 
 
 

User-defined function and dynamic SQL.

Post by Bruc » Sat, 10 Nov 2001 07:43:16


Interestingly, I needed to do the same thing today: build a string and
EXECUTE(string) that string inside of a function. As far as I can tell from
BOL, you aren't allowed to call EXEC() from inside a function. Hopefully I'm
wrong...

Bruce


> Hello, BP!

> Well...I took a peek at that section, but I'm still not getting it.
Here's
> what I would have done if I could have used temporary tables in this
> function:


> returns varchar(8000)
> as
> begin

>    create table #Value (Value varchar(8000))


>    drop table #Value


> end

 
 
 

User-defined function and dynamic SQL.

Post by John Peterso » Sat, 10 Nov 2001 08:40:41


Hello, Bruce!

Heh...as far as I can tell, it's not possible.  But, I'm hopeful that one of
the SQL Gurus here will be able to help.  :-)

The only context that I've been able to find that EXECUTE works in a
function is with the INSERT command.  Now, in my case, in that I wanted to
return some information from the dynamic SQL, I thought that'd work like a
charm.  However, I don't want to have to create a static table, and it
appears as if temporary tables aren't permitted in functions.  Nor does
EXECUTE work with a table variable.

I'm not sure how else to execute dynamic SQL other than with the EXECUTE
statement.  I wish there were a default extended stored procedure that
essentially did this.  ;-)


> Interestingly, I needed to do the same thing today: build a string and
> EXECUTE(string) that string inside of a function. As far as I can tell
from
> BOL, you aren't allowed to call EXEC() from inside a function. Hopefully
I'm
> wrong...

> Bruce



> > Hello, BP!

> > Well...I took a peek at that section, but I'm still not getting it.
> Here's
> > what I would have done if I could have used temporary tables in this
> > function:


> > returns varchar(8000)
> > as
> > begin

> >    create table #Value (Value varchar(8000))


> >    drop table #Value


> > end

 
 
 

User-defined function and dynamic SQL.

Post by BP Margoli » Sat, 10 Nov 2001 09:39:25


John, Bruce,

Sorry, but one of the fundamentals of UDF's is, as stated in the section
"CREATE FUNCTION" right at the top: "User-defined functions cannot be used
to perform a set of actions that modify the global database state." This
means that one cannot do INSERTs, UPDATEs, and DELETEs to permanent tables,
for example, from within a UDF. Dynamic SQL is a very powerful tool, and one
that can "modify the global database state". While it might be nice if the
code within the dynamic SQL were examined at each call of the UDF to see if
it does modify the global database state, that is just too much, at least at
the present, to ask of SQL Server. Since it is impossible to absolutely
determine in advance all cases when dynamic SQL will modify the global
database state, currently SQL Server simply takes the easier path of
disallowing dynamic SQL.

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


> Hello, Bruce!

> Heh...as far as I can tell, it's not possible.  But, I'm hopeful that one
of
> the SQL Gurus here will be able to help.  :-)

> The only context that I've been able to find that EXECUTE works in a
> function is with the INSERT command.  Now, in my case, in that I wanted to
> return some information from the dynamic SQL, I thought that'd work like a
> charm.  However, I don't want to have to create a static table, and it
> appears as if temporary tables aren't permitted in functions.  Nor does
> EXECUTE work with a table variable.

> I'm not sure how else to execute dynamic SQL other than with the EXECUTE
> statement.  I wish there were a default extended stored procedure that
> essentially did this.  ;-)



> > Interestingly, I needed to do the same thing today: build a string and
> > EXECUTE(string) that string inside of a function. As far as I can tell
> from
> > BOL, you aren't allowed to call EXEC() from inside a function. Hopefully
> I'm
> > wrong...

> > Bruce



> > > Hello, BP!

> > > Well...I took a peek at that section, but I'm still not getting it.
> > Here's
> > > what I would have done if I could have used temporary tables in this
> > > function:


> > > returns varchar(8000)
> > > as
> > > begin

> > >    create table #Value (Value varchar(8000))


> > >    drop table #Value


> > > end

 
 
 

User-defined function and dynamic SQL.

Post by Tibor Karasz » Sat, 10 Nov 2001 19:15:55


I think that a workaround might be to execute the stuff using OPENROWSET/OPENQUERY. Not very
nice, and remember that MS tried to explicitly avoid dynamic SQL inside UDF's. Something to
consider before using such a workaround.

--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sql...


> John, Bruce,

> Sorry, but one of the fundamentals of UDF's is, as stated in the section
> "CREATE FUNCTION" right at the top: "User-defined functions cannot be used
> to perform a set of actions that modify the global database state." This
> means that one cannot do INSERTs, UPDATEs, and DELETEs to permanent tables,
> for example, from within a UDF. Dynamic SQL is a very powerful tool, and one
> that can "modify the global database state". While it might be nice if the
> code within the dynamic SQL were examined at each call of the UDF to see if
> it does modify the global database state, that is just too much, at least at
> the present, to ask of SQL Server. Since it is impossible to absolutely
> determine in advance all cases when dynamic SQL will modify the global
> database state, currently SQL Server simply takes the easier path of
> disallowing dynamic SQL.

> -------------------------------------------
> BP Margolin
> Please reply only to the newsgroups.
> When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
> can be cut and pasted into Query Analyzer is appreciated.



> > Hello, Bruce!

> > Heh...as far as I can tell, it's not possible.  But, I'm hopeful that one
> of
> > the SQL Gurus here will be able to help.  :-)

> > The only context that I've been able to find that EXECUTE works in a
> > function is with the INSERT command.  Now, in my case, in that I wanted to
> > return some information from the dynamic SQL, I thought that'd work like a
> > charm.  However, I don't want to have to create a static table, and it
> > appears as if temporary tables aren't permitted in functions.  Nor does
> > EXECUTE work with a table variable.

> > I'm not sure how else to execute dynamic SQL other than with the EXECUTE
> > statement.  I wish there were a default extended stored procedure that
> > essentially did this.  ;-)



> > > Interestingly, I needed to do the same thing today: build a string and
> > > EXECUTE(string) that string inside of a function. As far as I can tell
> > from
> > > BOL, you aren't allowed to call EXEC() from inside a function. Hopefully
> > I'm
> > > wrong...

> > > Bruce



> > > > Hello, BP!

> > > > Well...I took a peek at that section, but I'm still not getting it.
> > > Here's
> > > > what I would have done if I could have used temporary tables in this
> > > > function:


> > > > returns varchar(8000)
> > > > as
> > > > begin

> > > >    create table #Value (Value varchar(8000))


> > > >    drop table #Value


> > > > end

 
 
 

User-defined function and dynamic SQL.

Post by John Peterso » Sat, 10 Nov 2001 23:06:25


Hello, Tibor!

Yeah...I had considered OPENQUERY/OPENROWSET, but given that my dynamic
query is in a string, and that those functions don't take variables as
arguments...I wasn't able to get too far with it.  ;-)

I appreciate what BP reiterated about not allowing UDFs to change the global
database state (and that it would be too difficult to programmatically
determine what your query was doing to ascertain whether it was just a
simple SELECT).

But I wish it did...  ;-)

Thanks guys!

John Peterson



Quote:> I think that a workaround might be to execute the stuff using

OPENROWSET/OPENQUERY. Not very
Quote:> nice, and remember that MS tried to explicitly avoid dynamic SQL inside
UDF's. Something to
> consider before using such a workaround.

> --
> Tibor Karaszi, SQL Server MVP
> Archive at:

http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sql...




> > John, Bruce,

> > Sorry, but one of the fundamentals of UDF's is, as stated in the section
> > "CREATE FUNCTION" right at the top: "User-defined functions cannot be
used
> > to perform a set of actions that modify the global database state." This
> > means that one cannot do INSERTs, UPDATEs, and DELETEs to permanent
tables,
> > for example, from within a UDF. Dynamic SQL is a very powerful tool, and
one
> > that can "modify the global database state". While it might be nice if
the
> > code within the dynamic SQL were examined at each call of the UDF to see
if
> > it does modify the global database state, that is just too much, at
least at
> > the present, to ask of SQL Server. Since it is impossible to absolutely
> > determine in advance all cases when dynamic SQL will modify the global
> > database state, currently SQL Server simply takes the easier path of
> > disallowing dynamic SQL.

> > -------------------------------------------
> > BP Margolin
> > Please reply only to the newsgroups.
> > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.)
which
> > can be cut and pasted into Query Analyzer is appreciated.



> > > Hello, Bruce!

> > > Heh...as far as I can tell, it's not possible.  But, I'm hopeful that
one
> > of
> > > the SQL Gurus here will be able to help.  :-)

> > > The only context that I've been able to find that EXECUTE works in a
> > > function is with the INSERT command.  Now, in my case, in that I
wanted to
> > > return some information from the dynamic SQL, I thought that'd work
like a
> > > charm.  However, I don't want to have to create a static table, and it
> > > appears as if temporary tables aren't permitted in functions.  Nor
does
> > > EXECUTE work with a table variable.

> > > I'm not sure how else to execute dynamic SQL other than with the
EXECUTE
> > > statement.  I wish there were a default extended stored procedure that
> > > essentially did this.  ;-)



> > > > Interestingly, I needed to do the same thing today: build a string
and
> > > > EXECUTE(string) that string inside of a function. As far as I can
tell
> > > from
> > > > BOL, you aren't allowed to call EXEC() from inside a function.
Hopefully
> > > I'm
> > > > wrong...

> > > > Bruce



> > > > > Hello, BP!

> > > > > Well...I took a peek at that section, but I'm still not getting
it.
> > > > Here's
> > > > > what I would have done if I could have used temporary tables in
this
> > > > > function:


> > > > > returns varchar(8000)
> > > > > as
> > > > > begin

> > > > >    create table #Value (Value varchar(8000))


> > > > >    drop table #Value


> > > > > end

 
 
 

User-defined function and dynamic SQL.

Post by Tibor Karasz » Sat, 10 Nov 2001 23:27:04


Hi John,

Ahh, I didn't reflect over OPENQUERY not accepting a variable for query. Well, you learn
something every day :-).

--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sql...


> Hello, Tibor!

> Yeah...I had considered OPENQUERY/OPENROWSET, but given that my dynamic
> query is in a string, and that those functions don't take variables as
> arguments...I wasn't able to get too far with it.  ;-)

> I appreciate what BP reiterated about not allowing UDFs to change the global
> database state (and that it would be too difficult to programmatically
> determine what your query was doing to ascertain whether it was just a
> simple SELECT).

> But I wish it did...  ;-)

> Thanks guys!

> John Peterson



> > I think that a workaround might be to execute the stuff using
> OPENROWSET/OPENQUERY. Not very
> > nice, and remember that MS tried to explicitly avoid dynamic SQL inside
> UDF's. Something to
> > consider before using such a workaround.

> > --
> > Tibor Karaszi, SQL Server MVP
> > Archive at:
> http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sql...



> > > John, Bruce,

> > > Sorry, but one of the fundamentals of UDF's is, as stated in the section
> > > "CREATE FUNCTION" right at the top: "User-defined functions cannot be
> used
> > > to perform a set of actions that modify the global database state." This
> > > means that one cannot do INSERTs, UPDATEs, and DELETEs to permanent
> tables,
> > > for example, from within a UDF. Dynamic SQL is a very powerful tool, and
> one
> > > that can "modify the global database state". While it might be nice if
> the
> > > code within the dynamic SQL were examined at each call of the UDF to see
> if
> > > it does modify the global database state, that is just too much, at
> least at
> > > the present, to ask of SQL Server. Since it is impossible to absolutely
> > > determine in advance all cases when dynamic SQL will modify the global
> > > database state, currently SQL Server simply takes the easier path of
> > > disallowing dynamic SQL.

> > > -------------------------------------------
> > > BP Margolin
> > > Please reply only to the newsgroups.
> > > When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.)
> which
> > > can be cut and pasted into Query Analyzer is appreciated.



> > > > Hello, Bruce!

> > > > Heh...as far as I can tell, it's not possible.  But, I'm hopeful that
> one
> > > of
> > > > the SQL Gurus here will be able to help.  :-)

> > > > The only context that I've been able to find that EXECUTE works in a
> > > > function is with the INSERT command.  Now, in my case, in that I
> wanted to
> > > > return some information from the dynamic SQL, I thought that'd work
> like a
> > > > charm.  However, I don't want to have to create a static table, and it
> > > > appears as if temporary tables aren't permitted in functions.  Nor
> does
> > > > EXECUTE work with a table variable.

> > > > I'm not sure how else to execute dynamic SQL other than with the
> EXECUTE
> > > > statement.  I wish there were a default extended stored procedure that
> > > > essentially did this.  ;-)



> > > > > Interestingly, I needed to do the same thing today: build a string
> and
> > > > > EXECUTE(string) that string inside of a function. As far as I can
> tell
> > > > from
> > > > > BOL, you aren't allowed to call EXEC() from inside a function.
> Hopefully
> > > > I'm
> > > > > wrong...

> > > > > Bruce



> > > > > > Hello, BP!

> > > > > > Well...I took a peek at that section, but I'm still not getting
> it.
> > > > > Here's
> > > > > > what I would have done if I could have used temporary tables in
> this
> > > > > > function:


> > > > > > returns varchar(8000)
> > > > > > as
> > > > > > begin

> > > > > >    create table #Value (Value varchar(8000))


> > > > > >    drop table #Value


> > > > > > end

 
 
 

User-defined function and dynamic SQL.

Post by Erland Sommarsko » Tue, 13 Nov 2001 04:56:48



> I'm sure this question has been asked, but a cursory glance in this forum
> and on the Internet didn't yield in the answer for me, so I thought I'd
> post it here.

> I'm trying to create a user-defined function that I can use within a
> SELECT statement that will effectively use a dynamically created SQL
> statement to return a value:

BP has already given you very good explanations why can't do this.
There is one more thing worth mentioning, though, and that is that
the performance of scalar UDF are not good. Basically a scalar UDF
serializes the query like a cursor. This means that if the UDF is
called in the wrong place, you will very get vary bad performance.

If you post your actuall problem, we might be able to suggest a
good solution.

Note: the performance problem I mentioned to not apply to table
functions. Inlined table functions works more or less as a view
I believe.

--
Erland Sommarskog, Abaris AB

SQL Server MVP