分割以逗号作为分割符号的字符串并插入到表中

SQL语句如下:

DECLARE @temp varchar(50)
DECLARE @post int
DECLARE @endpost int
DECLARE @flag bit
SET @flag = 1
SET @post = 0
SET @temp='1,2,3,4,'

WHILE(@flag = 1)
BEGIN
     SET @endpost = charindex(',',@temp)
     IF(@endpost > 0)
     BEGIN
          INSERT INTO [str] (chr) VALUES (substring(@temp,1,@endpost-1))
          SET @temp = right(@temp,len(@temp)-@endpost)
     END
     ELSE
     BEGIN
          IF(@temp <> '')
          BEGIN
                 INSERT INTO [str] (chr) VALUES (@temp)
                 SET @flag = 0
          END
          ELSE
                 SET @flag = 0
     END
END

posted on 2006-05-01 01:32  老弹  阅读(1617)  评论(0编辑  收藏  举报

导航