CROSS/OUTER APPLY позволяет впихнуть в запрос табличную функцию
к примеру есть функция, разбора строки на части:
CREATE FUNCTION dbo.f_GTDparse (
@id INT,
@gtd VARCHAR(100)
)
RETURNS @data_out TABLE (
id INT,
CustomsCode VARCHAR(25),
RegistrationDate VARCHAR(25),
GTDNumber VARCHAR(25),
GoodsNumeric VARCHAR(25)
) AS
BEGIN
DECLARE @data TABLE (
id INT,
[Selection] VARCHAR(100) NULL
)
INSERT INTO @data (id)
VALUES (@id)
;
WITH Data_RowNumber
AS (
SELECT id,
split.value,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS RowNumber
FROM @data
CROSS APPLY STRING_SPLIT(TRIM(@gtd), '/') AS split
)
INSERT INTO @data_out
SELECT id,
[1] AS CustomsCode,
[2] AS RegistrationDate,
[3] AS GTDNumber,
ISNULL([4], '0') AS GoodsNumeric
FROM Data_RowNumber
PIVOT (
MAX([value])
FOR [RowNumber] IN ([1], [2], [3], [4])
) AS p
RETURN
END
GO
И теперь, чтобы вызвать ее в запросе используем
SELECT t.*, f.* FROM table t
OUTER APPLY dbo.f_GTDparse(t.Id, t.[ГТД]) f