Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Multiple Inserts in a Stored Procedure

Multiple Inserts in a Stored Procedure

Scheduled Pinned Locked Moved Database
databasehelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RJS
    wrote on last edited by
    #1

    Is there any way to do a multiple inserts in a stored procedure in SQL2000? Please help. Thanks.

    N S 2 Replies Last reply
    0
    • R RJS

      Is there any way to do a multiple inserts in a stored procedure in SQL2000? Please help. Thanks.

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      RJS wrote: Is there any way to do a multiple inserts in a stored procedure in SQL2000? You could write a while loop inside the stored procedure, there are many ways of doing something like this. Nick Parker
      May your glass be ever full. May the roof over your head be always strong. And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing


      1 Reply Last reply
      0
      • R RJS

        Is there any way to do a multiple inserts in a stored procedure in SQL2000? Please help. Thanks.

        S Offline
        S Offline
        SimonS
        wrote on last edited by
        #3

        Can you give more details? Are these just INSERTS into multiple tables? Are the INSERTS dependent on each other? Cheers, Simon "Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)

        R 1 Reply Last reply
        0
        • S SimonS

          Can you give more details? Are these just INSERTS into multiple tables? Are the INSERTS dependent on each other? Cheers, Simon "Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)

          R Offline
          R Offline
          RJS
          wrote on last edited by
          #4

          Simon, thanks for the reply. The scenario is like this: I have (let's say) 3 records or more in my .net page. Example: "John", "123 Anywhere", "City1" "Rudy", "345 Anywhere", "City2" "Tom", "335 Anywhere", "City2" ... ... Is it possible to do a multiple inserts in a stored procedure to save those 3 records or more in a table? If so, how do I pass the parameters since I will not know what is the maximum records that will be sent at once? Thanks. :)

          M 1 Reply Last reply
          0
          • R RJS

            Simon, thanks for the reply. The scenario is like this: I have (let's say) 3 records or more in my .net page. Example: "John", "123 Anywhere", "City1" "Rudy", "345 Anywhere", "City2" "Tom", "335 Anywhere", "City2" ... ... Is it possible to do a multiple inserts in a stored procedure to save those 3 records or more in a table? If so, how do I pass the parameters since I will not know what is the maximum records that will be sent at once? Thanks. :)

            M Offline
            M Offline
            Morten Abrahamsen
            wrote on last edited by
            #5

            What you would like to do is the basically pass a recordset to a stored procedure in SQL Server 2000. (?) The only way I know of to do this is to create an Xml file, pass it as a text (ntext in .Net :) ) parameter to the stored procedure. And then use the OPENXML SQL statement, which enables you to treat an XML file as a table (check OPENXML in Books Online). You can then easily use something like INSERT INTO MyTable (val1, val2, val3) SELECT val1, val2, val3 FROM OPENXML(...) It very performant and works like a charm! :) Hope this helps, Morty

            S 1 Reply Last reply
            0
            • M Morten Abrahamsen

              What you would like to do is the basically pass a recordset to a stored procedure in SQL Server 2000. (?) The only way I know of to do this is to create an Xml file, pass it as a text (ntext in .Net :) ) parameter to the stored procedure. And then use the OPENXML SQL statement, which enables you to treat an XML file as a table (check OPENXML in Books Online). You can then easily use something like INSERT INTO MyTable (val1, val2, val3) SELECT val1, val2, val3 FROM OPENXML(...) It very performant and works like a charm! :) Hope this helps, Morty

              S Offline
              S Offline
              SimonS
              wrote on last edited by
              #6

              That's one way, Morten. Another, which is very dependent on the type of data you're passing through, could pass a delimited list (a big string) to the stored proc and use some of the following code: DECLARE @Array varchar(50) DECLARE @iStart int DECLARE @iEleFound int DECLARE @iMaxEleLen int DECLARE @iNextDelimIndex int DECLARE @iEleLen int SET @Array = '1,2,3,4,5,6,7,8,9,65,66,67,100,101,102,654321' SET @iStart = 0 SET @iEleFound = 0 SET @iMaxEleLen = 5 SET @iNextDelimIndex=0 SET @iEleLen =0 print '---------' print 'LENGTH of string: ' + CAST(LEN(@Array) AS VARCHAR) while CHARINDEX ( ',' , @Array,@iStart ) > 0 -- for each element in the array begin SET @iEleLen =1 --default --if not ',' or '' if (CAST(SUBSTRING(@Array, @iStart + 1, 1) AS char(1)) <> ',') and not(len (SUBSTRING (@Array, @iStart + 1, 1)) = 0) begin--element found at this index SET @iNextDelimIndex = CHARINDEX ( ',' , @Array,@iStart+1) if(@iNextDelimIndex=0) begin SET @iNextDelimIndex = CAST (LEN(@Array) AS VARCHAR)+1 print 'latest element is next:' end SET @iEleLen = @iNextDelimIndex - @iStart --DB function would occur here print 'NEXT DELIM LOCATION-->' + CAST(@iNextDelimIndex AS VARCHAR(10)) print 'VALUE FOUND-->' + RTRIM( CAST(SUBSTRING(@Array, @iStart + 1, @iNextDelimIndex - @iStart-1) AS char(10)) ) + '<--' SET @iEleFound = @iEleFound + 1 end else --no element found at this index begin print 'do nothing' end SET @iStart = @iStart + @iEleLen end --if (CAST(SUBSTRING(@Array, @iStart + 1, 1) AS char(1)) <> ',') and not(len (SUBSTRING (@Array, @iStart + 1, 1)) = 0) print '------ ELEMENTS FOUND: ' + CAST(@iEleFound AS CHAR(2)) + ' ---------' Note: not fully tested, but should give you a starting point. Cheers, Simon "Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups