存储过程中实现类似split功能(charindex)
create procedure sp_split_string
(
@string NVARCHAR(4000)
)
AS
BEGIN
DECLARE @object_id nvarchar(500)
DECLARE @i INT
DECLARE @len INT
print @string
IF (@string IS NULL) OR (LTRIM(@string) = '')
RETURN
WHILE CHARINDEX(',',@string) > 0
BEGIN
SET @len = LEN(@string)
SET @i = CHARINDEX(',', @string)
SET @object_id = LEFT(@string, @i-1)
INSERT INTO a (id) VALUES (@object_id)--少做修改,改成需要的sql语句即可
SET @string = RIGHT(@string, @len - @i)
END
SET @object_id = @string
INSERT INTO a (id) VALUES (@object_id)--少做修改,改成需要的sql语句即可
END
go
--测试
-- select * from a
-- exec sp_split_string '102,103,105,106,107,108,200,500,306,408'
-- select * from a