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. Using a variable table [modified]

Using a variable table [modified]

Scheduled Pinned Locked Moved Database
sharepointdatabasehelpquestionannouncement
2 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.
  • M Offline
    M Offline
    MAW30
    wrote on last edited by
    #1

    I am trying to read table data from all my tables in a database to a summary table which contains the table anme in a column, but I am not sure what is the best way to go about doing this. Should I use temp tables or sp_executesql or some other method. I want to use a variable for the table name but I don't think that will work unless I split the string below in 2 and insert a table name. Or should I use temp tables which I am not familiar with or other method, if so how would I do that. I appreciate any help I can get. Thanks in advance, Michael SET @SQLString1 = N' DECLARE @dtLatestDate DATETIME SELECT @dtLatestDate = B.dtDateTime FROM ( SELECT TOP (1) dtDateTime FROM ' (split here the add tablename, see below) @TableName SET @SQLString2 = N' ORDER BY dtDateTime DESC ) AS B' OPEN strTableName_cursor; FETCH NEXT FROM strTableName_cursor INTO @strTableName; WHILE @@FETCH_STATUS = 0 BEGIN EXEC sp_executesql @SQLString1 + @TableName + @SQLString2 UPDATE [dbo].[Summary] SET dtLatestDate = @dtLatestDate WHERE strTableName = @strTableName FETCH NEXT FROM strTableName_cursor INTO @strTableName END CLOSE strSymbol_cursor

    modified on Saturday, May 14, 2011 3:50 PM

    J 1 Reply Last reply
    0
    • M MAW30

      I am trying to read table data from all my tables in a database to a summary table which contains the table anme in a column, but I am not sure what is the best way to go about doing this. Should I use temp tables or sp_executesql or some other method. I want to use a variable for the table name but I don't think that will work unless I split the string below in 2 and insert a table name. Or should I use temp tables which I am not familiar with or other method, if so how would I do that. I appreciate any help I can get. Thanks in advance, Michael SET @SQLString1 = N' DECLARE @dtLatestDate DATETIME SELECT @dtLatestDate = B.dtDateTime FROM ( SELECT TOP (1) dtDateTime FROM ' (split here the add tablename, see below) @TableName SET @SQLString2 = N' ORDER BY dtDateTime DESC ) AS B' OPEN strTableName_cursor; FETCH NEXT FROM strTableName_cursor INTO @strTableName; WHILE @@FETCH_STATUS = 0 BEGIN EXEC sp_executesql @SQLString1 + @TableName + @SQLString2 UPDATE [dbo].[Summary] SET dtLatestDate = @dtLatestDate WHERE strTableName = @strTableName FETCH NEXT FROM strTableName_cursor INTO @strTableName END CLOSE strSymbol_cursor

      modified on Saturday, May 14, 2011 3:50 PM

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      If you want to vary the table name dynamically then you must use dynamic SQL. There is no alternative. Either sp_executesql or 'execute' can be used. Are you sure it needs to be dynamic though?

      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