Problem adding data select and cross apply in a table function

jdr0606
jdr0606 used Ask the Experts™
on
Creating variable values that would then be used in a function.

I have a function that is working fine but I've added a component to initilize some data variables to use throughout the function but I'm getting the following error when try to update the (table) function.

Msg 156, Level 15, State 1, Procedure udfCheckMultiSiteOrder_20190314, Line 27 [Batch Start Line 9]
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Procedure udfCheckMultiSiteOrder_20190314, Line 27 [Batch Start Line 9]
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

I have attached the code I'm using and have notated with todays date what I added. I'm sure it's where I'm placing the new code but not sure where to place instead.

Thanks as always.PrePMultiFuntion.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
A cte is the leading part of a SELECT (or DELETE or INSERT or UPDATE) statement only.  It's not a stand-alone result set that can be referenced back to.  For that, you'd need to create a temp table.

Also, you need a BEGIN and END for the function itself:

alter FUNCTION [dbo].[udfCheckMultiSiteOrder_20190314](@sopnumbe VARCHAR(21))
RETURNS @checkresults TABLE
(
    ID int IDENTITY NOT NULL,
    checkresults nvarchar(255) NOT NULL
)
AS
BEGIN
...rest of corrected proc...
END

Author

Commented:
Thanks for the comments, it was just what I needed to get me to the end result I needed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial