Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get the fields data from a Table dynamically

Author  Topic 

vision99
Starting Member

14 Posts

Posted - 2009-06-22 : 05:21:33

HI,

am having one temporary table in that am inserting the Table Name from which i have to retrive the data

CREATE TABLE #SRCNAME
(
Source1 Varchar(20)
)
INSERT INTO
#SRCNAME
SELECT
DISTINCT 'PRMCL_'+SOURCE
FROM
PrmTable
WHERE
DATE = '06/17/2009'



SELECT * FROM #SRCNAME will display result something like

PRMCL_table1

now my requirement is get the required columns data from 'PRMCL_table1'. using sql not using any cursors....


plz give some idea...




-Vision

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-22 : 05:24:40
There is no difference between a WHILE LOOP or a CURSOR.
Have your manager notice this.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

vision99
Starting Member

14 Posts

Posted - 2009-06-22 : 05:29:02
am not using any WHILE LOOP here...., just i want to display results without using cursor...

-Vision
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-22 : 05:31:09
And what if there are two records in the #SRCNAME table?


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

vision99
Starting Member

14 Posts

Posted - 2009-06-22 : 05:36:37
It will give only one record, b'coz prmtable have only one source for one date...

INSERT INTO
#SRCNAME
SELECT
DISTINCT 'PRMCL_'+SOURCE
FROM
PrmTable
WHERE
DATE = '06/17/2009'



-Vision
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-22 : 05:51:21
declare @table_name varchar(100)
SELECT @table_name =Source1 FROM #SRCNAME
EXEC('SELECT * FROM '+@table_name )


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -