Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 26th, 2005, 10:46 AM
Authorized User
 
Join Date: Feb 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Challenging Problem in simple SELECT

Dear Readers,

 I want to select data from a table name which is a result of a query .

Basically ,

 a sql query is giving a table name based on some conditions & i want to fetch data from that table name .

waiting for a good reply



Cheers :
K K Singh.
+91-9810481159

Why to be GOOD when U have the ability to be GREAT
__________________
Cheers :
K K Singh.
+91-9810481159

Why to be GOOD when U have the ability to be GREAT
 
Old December 27th, 2005, 08:27 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I don't understand. You are creating a temporary table with a dynamic table name, and the dynamic table name is based on conditions in the query?

I would do one of two things:

1. use a standard temp table name based on the name of the query. Perhaps purge this table before you run the query using a delete query to make sure the data is clean and complete on each run of your initial query.

2. OR use the same conditions used in the first query to determine the table name when you run the second select query. This will give you the name of the temp table if it is dynamic.

Did that help?

mmcdonal
 
Old December 27th, 2005, 12:32 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

software_developer_kk mentioned nothing about temporary tables.
I recommend that you use dynamic SQL
Example:

DECLARE @tname sysname
SELECT TOP 1 @tname = table_name FROM Information_Schema.Tables
DECLARE @SqlString varchar(8000)
SELECT @SqlString = 'SELECT * FROM [' + @tname + ']'
EXEC (@SqlString )
-- Generally you should avoid Select * but in this case you may not know the column names
-- Generally you should avoid Select without where but a where is hard to do when you may not know the column names
-- Generally sp_ExecuteSQL is better than EXEC but for this example it does not matter, but if pass in parameters then it is recommended to use sp_executesql

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com
 
Old January 6th, 2006, 05:49 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

Exxxxxxacltyyyy...david is right ;))

B. Anant





Similar Threads
Thread Thread Starter Forum Replies Last Post
challenging sql problem avats SQL Server 2000 1 July 4th, 2005 12:41 PM
Select box for loop(probably really simple) interrupt Javascript How-To 2 March 18th, 2005 12:25 PM
A challenging one....Can this be done? SteveJ Dreamweaver (all versions) 5 September 10th, 2004 04:17 PM
Challenging questions... rguru VB.NET 2002/2003 Basics 2 May 21st, 2004 01:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.