我有一个SQL 2008数据库,兼容级别为80(即SQL 2000)。我已经能够使用交叉应用函数,但是当我添加一个表值函数时,这将不允许我继续。
我的会员ID格式如下:
EBS_322002_0397204_e
我需要动态地获取它的第二部分,因为第一部分可能并不总是正好3个字符长;否则,我可以使用子串函数并将其称为一天。
这是我的分割功能:
alter FUNCTION [dbo].[fnSplit] (@sep char(1),@string varchar(8000))
RETURNS TABLE
--WITH SCHEMABINDING
AS
RETURN (
WITH Pieces(pn, [start], [stop]) AS (
SELECT 1, 1, CHARINDEX(@sep, @string)
UNION ALL
SELECT pn + 1, [stop] + 1, CHARINDEX(@sep, @string, [stop] + 1)
FROM Pieces
WHERE [stop] > 0
)
SELECT pn,
SUBSTRING(@string, [start], CASE WHEN [stop] > 0 THEN [stop]-[start] ELSE LEN(@string) END) AS string
FROM Pieces
)
首次尝试:
WITH Members AS (
SELECT DISTINCT
memberid
FROM MyTable
)
SELECT * FROM Members m
CROSS APPLY dbo.fnSplit('_',m.memberid) b
产生错误:
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'm'.
第二次尝试(解决兼容性问题):
WITH Members AS (
SELECT DISTINCT
memberid
FROM MyTable
)
SELECT *
FROM Members m
OUTER APPLY (
SELECT TOP 1 string AS GroupNum
FROM dbo.fnSplit('_', m.memberid)
ORDER BY pn DESC
) Step1
产生以下错误:
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'm'.
我一直在墙上撞了几个小时,我现在,不知道该做什么......有什么建议吗?