Home Forums SQL Server 7,2000 T-SQL Executing dynamic SQL string within a function RE: Executing dynamic SQL string within a function

  • Robert Davis (5/24/2006)


    Try adding a USE statement to the dynamic SQL.<DT><FONT color=#1111ff>set</FONT><FONT size=2><FONT color=#1111ff> @sql</FONT> </FONT><FONT color=#808080 size=2>=</FONT><FONT size=2> N</FONT><FONT color=#ff0000 size=2>' USE [OtherDatabase] <DT>create function dbo.fn_tot_alldbs(@yourparameter int) <DT>returns int <DT>as <DT>--auto generated function code <DT>begin <DT>declare @intCount int <DT>select @intcount = 0 <DT>'</FONT></DT>

    I think you'll find that'll return an error about how CREATE must be the first thing in the batch.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)