Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    188

    Unanswered: Use variable as table Name in SQL

    is it possible to create a temp table and iterate through each database in the temp table? Something like and this would iterate both tables in the databse and display the results. (Of course I would actually be doing this on a much larger scale and not just selecting *
    Code:
    Declare @CountCheck int
    
    Create table #databse
    (
    Name varchar(100),
    Verified varchar(100)
    )
    Insert into #databse
    Values('Foxtrot')
    Insert into #databse
    Values('Charlie')
    
    while Verified IS NULL
    
    begin 
    Select * from Name
    
    update #databse set Verified = 'Yes'
    
    end

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    You can do this, but it generally is not a good idea.

    The keys to making it happen are either EXECUTE (Transact-SQL) and/or sp_executesql (Transact-SQL)

    Note that dynamic code of any kind is vulnerable to SQL Injection attacks. Do not use it if there is any viable alternative.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Feb 2012
    Posts
    188
    I understand. THank you for the links, I will check those out and psot back if I have any issues. It's also a Terrible IDEA to have the data structure that is done here (hence needing to do this). However, it's to far to gone to try to 'fix' the past, can only do it properly moving forward.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •