SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Return result of dynamic query from function


Return result of dynamic query from function

Author
Message
Matt Miller (4)
Matt Miller (4)
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30041 Visits: 19009
rose_red1947 (10/24/2007)
Try with SET clause before @RESULT = Execute @SQLQuery


No....

Let's try this again. from BOL:


The types of statements that are valid in a function include:

DECLARE statements can be used to define data variables and cursors that are local to the function.

Assignments of values to objects local to the function, such as using SET to assign values to scalar and table local variables.

Cursor operations that reference local cursors that are declared, opened, closed, and deallocated in the function. FETCH statements that return data to the client are not allowed. Only FETCH statements that assign values to local variables using the INTO clause are allowed.

Control-of-flow statements except TRY...CATCH statements.

SELECT statements containing select lists with expressions that assign values to variables that are local to the function.

UPDATE, INSERT, and DELETE statements modifying table variables that are local to the function.

EXECUTE statements calling an extended stored procedure.

Notice the last line - you can only call Extended stored procs using exec. Dynamic SQL is not allowed.

And lest you ask: sp_executeSQL is not an extended stored proc, so it's not legal either.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
vinod.andani-874416
vinod.andani-874416
SSC Veteran
SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)

Group: General Forum Members
Points: 293 Visits: 82
Its because, semicolon missing at the end of return statement.
Use the simplified one below :

CREATE FUNCTION dbo.ExecuteStringAsQuery (@empID as nvarchar(500))
RETURNS Varchar(8000)
AS
BEGIN
/* Build Transact-SQL String with parameter value */
return(select JoinDateQuery from NewHireEmployee where empid= + @empID);
END
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)

Group: General Forum Members
Points: 219008 Visits: 42002
vinod.andani-874416 (10/1/2015)
Its because, semicolon missing at the end of return statement.
Use the simplified one below :

CREATE FUNCTION dbo.ExecuteStringAsQuery (@empID as nvarchar(500))
RETURNS Varchar(8000)
AS
BEGIN
/* Build Transact-SQL String with parameter value */
return(select JoinDateQuery from NewHireEmployee where empid= + @empID);
END


Not likely. There is currently no requirement in SQL to require a RETURN to be followed by a semi-colon unless the next statement is something like a CTE which wouldn't make sense. The real problem was that folks were trying to execute dynamic SQL from within a function.

Only because it matters to some people, I'll also point out that this post is 8 years old.

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search