I am having trouble including an XML namespace in the following table-valued function. I need this namespace in order to perform an xpath join. The issue seems to be my use of the WITH method, but this syntax is required when I execute the query in straight SQL.
The exact error reads:
“Incorrect syntax near ‘default’. Expected ID or QUOTED_ID.”
Does anyone know the exact syntax for including an XML namespace in this T-SQL table-valued function body? Here is the T-SQL code:
USE [Context]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetStuff]()
RETURNS @X TABLE(
DisplayName NVARCHAR(250) NULL,
StandardCode NVARCHAR(250) NULL
)
--WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
DECLARE @MyTempTable TABLE (
DisplayName NVARCHAR(250) NULL,
StandardCode NVARCHAR(250) NULL
);
;WITH XMLNAMESPACES (default 'http://www.mynamespace.org/someSchema')
INSERT INTO @MyTempTable (DisplayName, StandardCode)
SELECT Distinct OI.DisplayName, OI.StandardCode
FROM db..Actor B
JOIN db..Part DP ON B.Id = DP.Id
JOIN db..Intent OI ON OI.StandardCode = DP.XML.value('(/US/California/Orange/LA/Pizza)[1]','nvarchar(max)')
JOIN db..Status PS ON B.Id = PS.Id
WHERE PS.StandardCode in ('Happy','Employed')
AND OI.Active = 1
ORDER BY OI.DisplayName
RETURN;
END
I don’t think this solves the syntax error, but you are creating a second table variable for nothing. This:
Should be: