Problem adding data select and cross apply in a table function

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
jdr0606Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdr0606Author Commented:
Thanks for the comments, it was just what I needed to get me to the end result I needed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.