sp_ExecuteSQL is not an "extended stored procedure" or it would begin with "xp_". Neither is any form of dynamic SQL. I'm afraid that you're out of luck.
In fact, if you check out Books Online, which says the following about UDF's, you'll see that you've violated several rules for UDF's
The following statements are allowed in the body of a multi-statement function. Statements not in this list are not allowed in the body of a function:
DECLARE statements defining data variables and cursors that are local to the function.
SELECT statements containing select lists with expressions that assign values to variables that are local to the function.
Cursor operations referencing local cursors that are declared, opened, closed, and deallocated in the function. Only FETCH statements that assign values to local variables using the INTO clause are allowed; FETCH statements that return data to the client are not allowed.
INSERT, UPDATE, and DELETE statements modifying table variables local to the function.
EXECUTE statements calling an extended stored procedures.
is pronounced ree-bar and is a Modenism for R
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:
How to post code problemsHow to post performance problemsForum FAQs