sql将以|间隔的字符串循环插入数据库中的存储过程案例

ALTER PROCEDURE dbo.dingpiaoinsert
 (
 @userId uniqueidentifier,
 @Str nvarchar(200),
 @movie_yingtingid int,
 @date datetime
 )
AS
begin
  begin try
    DECLARE @Index int;
    DECLARE @Left nvarchar(50);
    WHILE CHARINDEX('|',@Str) > 0
       BEGIN
          SELECT @Index = CHARINDEX('|',@Str);
          SELECT @Left = LEFT(@Str,@Index-1);
          INSERT INTO Seat_User(UserId,SeatId,Movie_yingtingid,[date]) VALUES (@userId,@Left,@movie_yingtingid,@date);
          SELECT @Str = REPLACE(@Str,@Left+'|','');
       END
   IF @Str <> ''
      BEGIN
          INSERT INTO Seat_User(UserId,SeatId,Movie_yingtingid,[date]) VALUES (@userId,@Left,@movie_yingtingid,@date);
      END
      return 0
     end try
    begin catch
      return error_number()
    end catch
end

posted @ 2012-04-23 19:28  aiaito  阅读(372)  评论(0编辑  收藏  举报