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. SQL Stored Procedure INSERT with random records from a 2nd table [modified]

SQL Stored Procedure INSERT with random records from a 2nd table [modified]

Scheduled Pinned Locked Moved Database
databasequestionsharepointhelplounge
5 Posts 3 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.
  • M Offline
    M Offline
    munklefish
    wrote on last edited by
    #1

    Hi, I need to submit data from a form to an SQL2005 database via a stored procedure. The difficult part is that i also need to get 5 random records from a secondary table and insert these as part of the inserted record in table 1. My structure is akin to this:

    Tbl_Campaigns (table to be inserted to)

    Key | field_1 | field_2 | field_3 | field_4 | field_5 etc

    Tbl_Organisations (table to get the 5 random records from)

    Key | field_1

    I need to get 5 unique / random records from 'tbl_Organisations', catch them and insert them into a single record along with the data input to the stored procedure. I understand arrays arent an option in SQL2005(?). So how do i catch these records and then insert them as a single record along with the SP Inputs? Any help would be greatly appreciated as i am trying my best to get to grips with the complexities of SQL Thanks.

    modified on Tuesday, July 7, 2009 6:04 AM

    D 1 Reply Last reply
    0
    • M munklefish

      Hi, I need to submit data from a form to an SQL2005 database via a stored procedure. The difficult part is that i also need to get 5 random records from a secondary table and insert these as part of the inserted record in table 1. My structure is akin to this:

      Tbl_Campaigns (table to be inserted to)

      Key | field_1 | field_2 | field_3 | field_4 | field_5 etc

      Tbl_Organisations (table to get the 5 random records from)

      Key | field_1

      I need to get 5 unique / random records from 'tbl_Organisations', catch them and insert them into a single record along with the data input to the stored procedure. I understand arrays arent an option in SQL2005(?). So how do i catch these records and then insert them as a single record along with the SP Inputs? Any help would be greatly appreciated as i am trying my best to get to grips with the complexities of SQL Thanks.

      modified on Tuesday, July 7, 2009 6:04 AM

      D Offline
      D Offline
      DoctorMick
      wrote on last edited by
      #2

      This may be useful....

      select top 5 key, field_1 from tbl_organisations order by 100 * rand()

      However, I haven't tested it cos I don't have SQL Server here so it may not work at all! :laugh:

      M 1 Reply Last reply
      0
      • D DoctorMick

        This may be useful....

        select top 5 key, field_1 from tbl_organisations order by 100 * rand()

        However, I haven't tested it cos I don't have SQL Server here so it may not work at all! :laugh:

        M Offline
        M Offline
        munklefish
        wrote on last edited by
        #3

        Hi, I would get a random record using the following:

        SELECT TOP 5 (*) from tbl_organisations AS randomOrgs

        However my issue is with how i would then catch these 5 records and insert them into the other table as a single record with the SP inputs.

        B 1 Reply Last reply
        0
        • M munklefish

          Hi, I would get a random record using the following:

          SELECT TOP 5 (*) from tbl_organisations AS randomOrgs

          However my issue is with how i would then catch these 5 records and insert them into the other table as a single record with the SP inputs.

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          SELECT TOP 5 (*) from tbl_organisations order by newid()


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          M 1 Reply Last reply
          0
          • B Blue_Boy

            SELECT TOP 5 (*) from tbl_organisations order by newid()


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            M Offline
            M Offline
            munklefish
            wrote on last edited by
            #5

            Blue_boy, Please read the original post in this thread. Thanks for your help but you appear to have misinterpretated my question.

            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