Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 3rd, 2004, 03:47 PM
Authorized User
Join Date: Sep 2003
Location: , , .
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default Selecting records from a dynamic tablename

Lets say that i have a set of tables that are similar in structure, Is there a way to pass a tablename to a stored procedure and have it select the records from it?

For instance

Create Procedure sp_GetMeRecords

Set nocount on

if exists(Select ID FROM @TableName)
  SELECT * FROM @TableName


when i do this, it keeps telling me that @TableName isn't declared!

please help thanks!

Reply With Quote
  #2 (permalink)  
Old August 3rd, 2004, 05:02 PM
Friend of Wrox
Join Date: Oct 2003
Location: Cairo, , Egypt.
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000

could be as follows:

Create Procedure sp_GetMeRecords
@TableName varchar(100)

exec('if exists(Select ID FROM ' + @TableName + ')
     SELECT * FROM ' + @TableName

Ahmed Ali
Software Developer
Reply With Quote
  #3 (permalink)  
Old August 4th, 2004, 11:14 AM
Authorized User
Join Date: Jun 2003
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts

Also check out undocumented stored procedures at: http://www.mssqlcity.com/Articles/Un...000UndocSP.htm

Sometimes, you need to perform the same actions for all tables in the database. You can create cursor for this purpose, or you can also use the sp_MSforeachtable stored procedure to accomplish the same goal with less work.

For example, you can use the sp_MSforeachtable stored procedure to rebuild all the indexes in a database:

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?')"

Joe Horton
Database Developer / Software Engineer
WISHA/Legal Services Software Development
Department of Labor and Industries
Voice (360) 902-5928 fax (360) 902-6200
Reply With Quote
  #4 (permalink)  
Old September 30th, 2004, 08:05 AM
Authorized User
Join Date: Sep 2004
Location: Delhi, Delhi, India.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Lalit_Pratihari


You will have to use EXEC or EXECUTE statements for dynamic table names.


Life Means More...;)
Reply With Quote

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 Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting records in a query firefighter2045 Access VBA 5 August 28th, 2008 08:14 AM
Selecting one of possibly many related records rodmcleay SQL Server 2000 2 March 24th, 2007 12:49 AM
selecting records from excel androoo Pro VB 6 1 January 12th, 2005 12:18 PM
Selecting the Latest Records gordbro Access 3 September 16th, 2004 07:21 PM
ASP - Selecting Multiple Records Ei0nN Classic ASP Databases 2 May 28th, 2004 03:38 PM

All times are GMT -4. The time now is 06:00 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.