If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Loop through table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-12, 04:04
HendrikB HendrikB is offline
Registered User
 
Join Date: Jan 2008
Posts: 20
Loop through table

Good Day

I have a table that stores a couple of tablenames in the same db.
The tablenames in the table can change from time to time.
Is it possible to loop through the tablenames in the table and run a query
against each tablename. I cannot hard code the table names in the query
because they can change from time to time.
Not sure how to accomplish this.

Thank You
Reply With Quote
  #2 (permalink)  
Old 01-22-12, 07:47
PracticalProgram PracticalProgram is offline
Registered User
 
Join Date: Sep 2001
Location: Chicago, Illinois, USA
Posts: 551
Google "dynamic SQL".

Basically, instead of writing queries against fixed table names, you will write a stored procedure that will use T-SQL to write the queries into text strings, and you will then execute the query text strings. By having the stored procedure write the queries, they can be dynamically created at execution time.
__________________
Ken

Maverick Software Design

(847) 864-3600 x2
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On