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. SELECT then INSERT data in a matrix way

SELECT then INSERT data in a matrix way

Scheduled Pinned Locked Moved Database
databasehelpquestion
3 Posts 2 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.
  • F Offline
    F Offline
    Farhad Eft
    wrote on last edited by
    #1

    Hi, I have a question regarding SQL queries. In my select statement in my stored procedure I will receive a table of information like this: UserId ------ 2 4 5 After this select statement I'd like to add a insert statement to add records For each of these UserIds (as UserIdFrom) with the other ones (as UserIdTo) I want to Insert into a table information like following: UserIdFrom UserIdTo ---------- -------- 2 4 2 5 4 2 4 5 5 2 5 4 I would be grateful if someone help me Smile | :) SELECT UserId FROM Users INSERT INTO myTable (UserIdFrom, UserIdTo) VALUES (@UserIdFrom, @UserIdTo)

    Richard DeemingR 1 Reply Last reply
    0
    • F Farhad Eft

      Hi, I have a question regarding SQL queries. In my select statement in my stored procedure I will receive a table of information like this: UserId ------ 2 4 5 After this select statement I'd like to add a insert statement to add records For each of these UserIds (as UserIdFrom) with the other ones (as UserIdTo) I want to Insert into a table information like following: UserIdFrom UserIdTo ---------- -------- 2 4 2 5 4 2 4 5 5 2 5 4 I would be grateful if someone help me Smile | :) SELECT UserId FROM Users INSERT INTO myTable (UserIdFrom, UserIdTo) VALUES (@UserIdFrom, @UserIdTo)

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try something like this:

      INSERT INTO myTable
      (
      UserIdFrom,
      UserIdTo
      )
      SELECT
      F.UserId,
      T.UserId
      FROM
      Users As F
      CROSS JOIN Users As T
      WHERE
      T.UserId != F.UserId
      ;


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      F 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Try something like this:

        INSERT INTO myTable
        (
        UserIdFrom,
        UserIdTo
        )
        SELECT
        F.UserId,
        T.UserId
        FROM
        Users As F
        CROSS JOIN Users As T
        WHERE
        T.UserId != F.UserId
        ;


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        F Offline
        F Offline
        Farhad Eft
        wrote on last edited by
        #3

        thank u it helped me a lot! :)

        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