Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I Want to user the variable table name in the From clause

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I am storing the infomation about the tables in one table called Table_Name(Id, Name). I want to use the table names stored in the Table_Name in the From clause to get the data stored in those tables. How can I get it?
 
You can try setting up a cursor...Something like this:

DECLARE ActiveTable CURSOR
FOR SELECT TableName
FROM TableRoot.dbo.TablesTable
WHERE COLUMNPROPERTY( OBJECT_ID('' + TableName +
''),'CreateDate','AllowsNull') IS NOT NULL

Then delcare a var:
DECLARE @TblNm varchar(6)

Open the Cursor:
OPEN ActiveTable

Get the Data:
FETCH FROM ActiveTable into @TblNm
BEGIN

Select the data:
Select ''' + @TblNm + ''' AS TableName .........


I hope this helps...(note: this is syntax for a Stored Proc.)

RogueSuit
 
I solved the problem by using function, cursor and dynamic sql.

thanks
smita
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top