MSSQL 的解决方案(故意健壮,parselist 函数可以帮助您将 db 标准化为更理智的东西)
帮助功能:
CREATE FUNCTION [dbo].[udf_GetNumeric]
(@strAlphaNumeric VARCHAR(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
BEGIN
WHILE @intAlpha > 0
BEGIN
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
END
END
RETURN ISNULL(@strAlphaNumeric,0)
END
GO
CREATE FUNCTION [dbo].[ParseList_IntAny]
(
@List nvarchar(1000)
)
RETURNS @Result TABLE (
IntValue int not null
)
as
begin
declare @Value nvarchar(20), @Position int
select @List = LTRIM(RTRIM(@List))+ ','
select @Position = CHARINDEX(',', @List, 1)
if REPLACE(@List, ',', '') <> ''
begin
while @Position > 0
begin
select @Value = LTRIM(RTRIM(LEFT(@List, @Position - 1)))
if @Value <> ''
begin
declare @IntValue int
select @IntValue = dbo.udf_GetNumeric(@Value)
insert into @Result(IntValue) values (@IntValue)
end
select @List = RIGHT(@List, LEN(@List) - @Position)
select @Position = CHARINDEX(',', @List, 1)
end
end
return
end
GO
declare @tmp table(ID int, pagename nvarchar(400))
insert into @tmp
select 1,'1'
union select 2,'01'
union select 3,'01, 15'
union select 4,'01, 01 Aaa, 15'
union select 5,'02'
union select 6,'03'
union select 7,'100'
union select 8,'101'
union select 9,'115'
select * from @tmp
where exists(select top 1 1 from dbo.ParseList_IntAny(pagename) where IntValue = 1)