表名 tbltest
字段(id,name,age)
我想批量插入个人的名字和年龄,这存储过程怎么写?名字和年龄之间用逗号隔开,即 name('张,王,李,赵') age('18,20,21,22')
name 和age 是传过来的变量,这个存储过程怎么写??
谢谢谢谢
create sp_name9
@name varchar(200),
@age varchar(200)
as
begin...
end
中间的咋写??谢谢谢谢

解决方案 »

  1.   

    參照精華貼,分拆完再插入或用循環
    http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html
      

  2.   

    如果只有四个用parsename,如果多个用分隔,参考如下:
    /*
    标题:分解字符串并查询相关数据
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-03-18
    地点:广东深圳
    说明:通过使用函数等方法分解字符串查询相关数据。问题:通过分解一个带某种符号分隔的字符串在数据库中查找相关数据。
    例如 @str = '1,2,3',查询下表得到记录1,4,5,6
    ID TypeID
    1  1,2,3,4,5,6,7,8,9,10,11,12
    2  2,3 
    3  3,7,8,9 
    4  2,6 
    5  4,5
    6  6,7 
    */
    -----------------------------
    create table tb (ID int , TypeID varchar(30)) 
    insert into tb values(1 , '1,2,3,4,5,6,7,8,9,10,11,12') 
    insert into tb values(2 , '2,3') 
    insert into tb values(3 , '3,7,8,9') 
    insert into tb values(4 , '2,6') 
    insert into tb values(5 , '4,5')
    insert into tb values(6 , '6,7')
    go
    -----------------------------
    --如果仅仅是一个,如@str = '1'.
    declare @str as varchar(30)
    set @str = '1'
    select * from tb where charindex(',' + @str + ',' , ',' + TypeID + ',') > 0
    select * from tb where ',' + TypeID + ',' like '%,' + @str + ',%'
    /*
    ID          TypeID                         
    ----------- ------------------------------ 
    1           1,2,3,4,5,6,7,8,9,10,11,12
    (所影响的行数为 1 行)
    */-----------------------------
    --如果包含两个,如@str = '1,2'.
    declare @str as varchar(30)
    set @str = '1,2'
    select * from tb where charindex(',' + left(@str , charindex(',' , @str) - 1) + ',' , ',' + typeid + ',') > 0 or 
      charindex(',' + substring(@str , charindex(',' , @str) + 1 , len(@str)) + ',' , ',' + typeid + ',') > 0
    select * from tb where ',' + typeid + ',' like '%,' + left(@str , charindex(',' , @str) - 1) + ',%' or 
      ',' + typeid + ',' like '%,' + substring(@str , charindex(',' , @str) + 1 , len(@str)) + ',%'
    /*
    ID          TypeID                         
    ----------- ------------------------------ 
    1           1,2,3,4,5,6,7,8,9,10,11,12
    2           2,3
    4           2,6
    (所影响的行数为 3 行)
    */-------------------------------------------
    --如果包含三个或四个,用PARSENAME函数来处理.
    declare @str as varchar(30)
    set @str = '1,2,3,4'
    select * from tb where 
      charindex(',' + parsename(replace(@str , ',' , '.') , 4) + ',' , ',' + typeid + ',') > 0 or
      charindex(',' + parsename(replace(@str , ',' , '.') , 3) + ',' , ',' + typeid + ',') > 0 or
      charindex(',' + parsename(replace(@str , ',' , '.') , 2) + ',' , ',' + typeid + ',') > 0 or
      charindex(',' + parsename(replace(@str , ',' , '.') , 1) + ',' , ',' + typeid + ',') > 0 
    select * from tb where 
      ',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 4) + ',%' or
      ',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 3) + ',%' or
      ',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 2) + ',%' or
      ',' + typeid + ',' like '%,' + parsename(replace(@str , ',' , '.') , 1) + ',%'
    /*
    ID          TypeID                         
    ----------- ------------------------------ 
    1           1,2,3,4,5,6,7,8,9,10,11,12
    2           2,3
    3           3,7,8,9
    4           2,6
    5           4,5
    (所影响的行数为 5 行)
    */---------------------------------------
    --如果超过四个,则只能使用函数或动态SQL来分解并查询数据。
    /*
    名称:fn_split函数.
    功能:实现字符串分隔功能的函数
    */
    create function dbo.fn_split(@inputstr varchar(8000), @seprator varchar(10))
    returns @temp table (a varchar(200))
    as 
    begin
      declare @i int
      set @inputstr = rtrim(ltrim(@inputstr))
      set @i = charindex(@seprator , @inputstr)
      while @i >= 1
      begin
        insert @temp values(left(@inputstr , @i - 1))
        set @inputstr = substring(@inputstr , @i + 1 , len(@inputstr) - @i)
        set @i = charindex(@seprator , @inputstr)
      end
      if @inputstr <> '\'
      insert @temp values(@inputstr)
      return 
    end
    go--调用
    declare @str as varchar(30)
    set @str = '1,2,3,4,5'select distinct m.* from tb m,
    (select * from dbo.fn_split(@str,',')) n
    where charindex(',' + n.a + ',' , ',' + m.typeid + ',') > 0drop table tb
    drop function dbo.fn_split /*
    ID          TypeID                         
    ----------- ------------------------------ 
    1           1,2,3,4,5,6,7,8,9,10,11,12
    2           2,3
    3           3,7,8,9
    4           2,6
    5           4,5
    (所影响的行数为 5 行)
    */------------------------------------------
    --使用动态SQL的语句。
    declare @str varchar(200)
    declare @sql as varchar(1000)
    set @str = '1,2,3,4,5'
    set @sql = 'select ''' + replace(@str , ',' , ''' as id union all select ''')
    set @sql = @sql + ''''
    set @sql = 'select distinct a.* from tb a , (' + @sql + ') b where charindex(' + ''','' + b.id + ' + ''',''' + ' , ' + ''','' + a.typeid + ' + ''',''' + ') > 0 '
    exec (@sql)
    /*
    ID          TypeID                         
    ----------- ------------------------------ 
    1           1,2,3,4,5,6,7,8,9,10,11,12
    2           2,3
    3           3,7,8,9
    4           2,6
    5           4,5
    (所影响的行数为 5 行)
    */
      

  3.   

    create sp_name9
    @name varchar(200),
    @age varchar(200)
    as
    begin
    select @Name=@Name+',',@age=@age+','
    while @Name>''
    begin
    insert tbltest(name,age) values(left(@Name,charindex(',',@Name)-1),left(@age,charindex(',',@age)-1)
    select @Name=stuff(@Name,1,charindex(',',@Name),''),@age=stuff(@age,1,charindex(',',@age),'')
    end
    end 
      

  4.   

    if object_id('dbo.f_split')is not null drop function dbo.f_split
    go
    create   function   f_split(@c   nvarchar(2000),@d nvarchar(2000),@split   varchar(2))   
    returns   @t   table(id int identity,a   nvarchar(20) ,b nvarchar(20))   
    as   
        begin   
              while(charindex(@split,@c)<>0)   
            begin   
              insert   @t(a,b)   values   (substring(@c,1,charindex(@split,@c)-1),substring(@d,1,charindex(@split,@d)-1))   
              set   @c   =   stuff(@c,1,charindex(@split,@c),'')   
              set   @d   =   stuff(@d,1,charindex(@split,@d),'')   
            end   
           insert   @t(a,b)   values   (@c,@d)   
          return   
        end   
    go   
    select * from dbo.f_split(N'张,王,李,赵','18,20,21,22',',')
    /*id          a                    b                    
    ----------- -------------------- -------------------- 
    1           张                    18
    2           王                    20
    3           李                    21
    4           赵                    22(影響 4 個資料列)*/
      

  5.   

    create table tbltest(id int identity,name varchar(10),age int)
    gocreate proc my_proc @s1 varchar(100) , @s2 varchar(100)
    as
    begin
      while charindex(',' , @s1) > 0
      begin
        insert into tbltest(name , age) values(left(@s1,charindex(',' , @s1)-1) , left(@s2,charindex(',' , @s2)-1))
        set @s1 = substring(@s1 ,charindex(',' , @s1)+1, len(@s1))
        set @s2 = substring(@s2 ,charindex(',' , @s2)+1, len(@s2))
      end
      insert into tbltest(name , age) values(@s1 , @s2)
    end
    goexec my_proc '张,王,李,赵','18,20,21,22'select * from tbltestdrop table tbltest
    /*
    id          name       age         
    ----------- ---------- ----------- 
    1           张          18
    2           王          20
    3           李          21
    4           赵          22(所影响的行数为 4 行)*/