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 dynamic SQL Values into Temp Table

Insert dynamic SQL Values into Temp Table

Scheduled Pinned Locked Moved Database
databasexmlhelptutorial
3 Posts 2 Posters 2 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, I need to insert Dynamic SQL Values into Temp table, but I am able to insert values into Global Temp table but I don't know how to insert Global Temp table values into Temp table back again. Any help would be greatly helpful. Any sort of implementation to insert Dynamic SQL values into Temp table is fine, directly or indirectly is fine. Here is how I am able to insert the Dynamic SQL Values into Global temp table, but not able to do the same for the Temp table, even if I can insert from Global Temp table to Temp table is also fine. Any help would be greatly helpful - thanks in advance buddies.

    DECLARE @sqlCommand varchar(max) = 'SELECT TABLE_NAME INTO ##TableNames FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='''+ @schema + ''''

    EXEC (@sqlCommand)
    EXEC ('SELECT \* FROM \[##TableNames\] ')
    

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

    Richard DeemingR 1 Reply Last reply
    0
    • I indian143

      Hi, I need to insert Dynamic SQL Values into Temp table, but I am able to insert values into Global Temp table but I don't know how to insert Global Temp table values into Temp table back again. Any help would be greatly helpful. Any sort of implementation to insert Dynamic SQL values into Temp table is fine, directly or indirectly is fine. Here is how I am able to insert the Dynamic SQL Values into Global temp table, but not able to do the same for the Temp table, even if I can insert from Global Temp table to Temp table is also fine. Any help would be greatly helpful - thanks in advance buddies.

      DECLARE @sqlCommand varchar(max) = 'SELECT TABLE_NAME INTO ##TableNames FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='''+ @schema + ''''

      EXEC (@sqlCommand)
      EXEC ('SELECT \* FROM \[##TableNames\] ')
      

      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 have to create the temp table before executing the dynamic SQL:

      CREATE TABLE #TableNames (TABLE_NAME sysname NOT NULL);

      DECLARE @sqlCommand nvarchar(2000) = N'INSERT INTO #TableNames (TABLE_NAME) SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema';

      EXEC sp_executesql @sqlCommand, N'@schema nvarchar(128)', @schema = @schema;

      SELECT * FROM #TableNames;

      DROP TABLE #TableNames;

      You also need to avoid string concatenation, otherwise you'll introduce a SQL Injection[^] vulnerability. sp_executesql (Transact-SQL) | Microsoft Docs[^] But in this example, you don't need dynamic SQL at all.


      "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 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You have to create the temp table before executing the dynamic SQL:

        CREATE TABLE #TableNames (TABLE_NAME sysname NOT NULL);

        DECLARE @sqlCommand nvarchar(2000) = N'INSERT INTO #TableNames (TABLE_NAME) SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = @schema';

        EXEC sp_executesql @sqlCommand, N'@schema nvarchar(128)', @schema = @schema;

        SELECT * FROM #TableNames;

        DROP TABLE #TableNames;

        You also need to avoid string concatenation, otherwise you'll introduce a SQL Injection[^] vulnerability. sp_executesql (Transact-SQL) | Microsoft Docs[^] But in this example, you don't need dynamic SQL at all.


        "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

        Thank you very much that did it great, its a great help. Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

        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