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. Insert within Select Statement

Insert within Select Statement

Scheduled Pinned Locked Moved Database
question
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.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    Hi All, I have a situation that I have 3 tables like TabA, TabB and TabC, I have to insert into TabC by using a select statement from TabB but one value I have to take from Scope_Identity from inserting a row into TabA. So my Insert should look like below. Insert into TabC (Col1, Col2, TabAId) Select Col1, Col2, (Insert into TabA(CurrentDate) Values (DateTime) Scope_Identity) FROM TabB. So the Scope_Identity of the TabA insert should be used in the Select. Do we have any way to do it? Any sort of advice, code snippet and link is greatly helpful. Thanks in advance.

    Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

    Richard DeemingR M 2 Replies Last reply
    0
    • I indian143

      Hi All, I have a situation that I have 3 tables like TabA, TabB and TabC, I have to insert into TabC by using a select statement from TabB but one value I have to take from Scope_Identity from inserting a row into TabA. So my Insert should look like below. Insert into TabC (Col1, Col2, TabAId) Select Col1, Col2, (Insert into TabA(CurrentDate) Values (DateTime) Scope_Identity) FROM TabB. So the Scope_Identity of the TabA insert should be used in the Select. Do we have any way to do it? Any sort of advice, code snippet and link is greatly helpful. Thanks in advance.

      Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

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

      You can't embed an INSERT statement within a SELECT statement. Try something like this:

      DECLARE @TabAId int;
      INSERT INTO TabA (CurrentDate) VALUES (GetUtcDate());
      SET @TabAId = Scope_Identity();

      INSERT INTO TabC (Col1, Col2, TabAId)
      SELECT Col1, Col2, @TabAId FROM TabB;


      "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

      I 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        You can't embed an INSERT statement within a SELECT statement. Try something like this:

        DECLARE @TabAId int;
        INSERT INTO TabA (CurrentDate) VALUES (GetUtcDate());
        SET @TabAId = Scope_Identity();

        INSERT INTO TabC (Col1, Col2, TabAId)
        SELECT Col1, Col2, @TabAId FROM TabB;


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

        I Offline
        I Offline
        indian143
        wrote on last edited by
        #3

        This works if we have only one TabAId, but we need multiple TabAIds like different TabAIds for every row that SELECT Col1, Col2, @TabAId FROM TabB; generates, thats my problem. Can you help me pls? Even if I can use Insert withing Select that's also fine only thing if I can retrieve that Scope_Identity in selected rows.

        Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You can't embed an INSERT statement within a SELECT statement. Try something like this:

          DECLARE @TabAId int;
          INSERT INTO TabA (CurrentDate) VALUES (GetUtcDate());
          SET @TabAId = Scope_Identity();

          INSERT INTO TabC (Col1, Col2, TabAId)
          SELECT Col1, Col2, @TabAId FROM TabB;


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

          I Offline
          I Offline
          indian143
          wrote on last edited by
          #4

          Or even if I can do it in the following way but I am getting errors in the below query, it is saying its not able to recognize the columns, htge.[Name], htge.[Description]. Can I get those columns into Table variable some how? It will fix all my problems.

          DECLARE @MyTableVar table (DocumentListId int, [Name] varchar(max), [Description] varchar(max));
          INSERT INTO dbo.DocumentList (CreatedDate,CreatedBy,ModifiedDate,ModifiedBy)
          OUTPUT INSERTED.DocumentListId,
          htge.[Name],
          htge.[Description]
          INTO @MyTableVar
          SELECT GETDATE() ,CURRENT_USER,GETDATE(),CURRENT_USER, htge.[Name], htge.[Description]
          FROM OPENROWSET('SQLNCLI', 'Server=xxx;UID=xx;PWD=xxx',
          'SELECT * FROM WEB_WebAdmin.dbo.HealthTopicGroup where HealthTopicGroupId < 45') as htge;

          SELECT * frOM @MyTableVar

          Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

          1 Reply Last reply
          0
          • I indian143

            Hi All, I have a situation that I have 3 tables like TabA, TabB and TabC, I have to insert into TabC by using a select statement from TabB but one value I have to take from Scope_Identity from inserting a row into TabA. So my Insert should look like below. Insert into TabC (Col1, Col2, TabAId) Select Col1, Col2, (Insert into TabA(CurrentDate) Values (DateTime) Scope_Identity) FROM TabB. So the Scope_Identity of the TabA insert should be used in the Select. Do we have any way to do it? Any sort of advice, code snippet and link is greatly helpful. Thanks in advance.

            Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            You could use an onInsert trigger spit to insert the new records into TabC. Caveat an error on the trigger is a bitch to locate in the future.

            Never underestimate the power of human stupidity RAH

            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