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 > Sybase > Using a Variable for a table name in FROM clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-25-11, 09:48
hary hary is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Using a Variable for a table name in FROM clause

Hi,

I'm new with sybase and I'm using ASE 12.5.1.

I want to copy data from many tables by using variable for table name in FROM clause.

I found in this forum that I can use following command for variable table name:

exec ('select * from ' + @tablename)

But my question is, how about the data copy, is it possible to do like following:

exec ('insert desttable
select col1,col2 from ' + @tablename + ' where mycondition ')

I did it but there is nothing in the destination table after that.

If someone can advise.
Reply With Quote
  #2 (permalink)  
Old 02-28-11, 04:52
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
Does select col1,col2 from ' + @tablename + ' where mycondition return any data?
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #3 (permalink)  
Old 02-28-11, 08:13
hary hary is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Quote:
Originally Posted by Martijnvs View Post
Does select col1,col2 from ' + @tablename + ' where mycondition return any data?
Thanks for the reply.

Yes, if I give a value to @tablename I get result.

It's the same for

exec ('insert desttable
select col1,col2 from ' + @tablename + ' where mycondition ')

when I fixed in advance the value of my variable, means:

select @tablename = 'myvalue'
exec ('insert desttable
select col1,col2 from ' + @tablename + ' where mycondition ')

I got data copy into "desttable". but when I put this command inside a loop command, the copy is not working.
Reply With Quote
  #4 (permalink)  
Old 03-03-11, 02:26
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
Then it's something wrong with your loop
__________________
Florin Aparaschivei
Iasi, Romania
Reply With Quote
  #5 (permalink)  
Old 03-08-11, 06:40
hary hary is offline
Registered User
 
Join Date: Feb 2011
Posts: 8
Quote:
Originally Posted by aflorin27 View Post
Then it's something wrong with your loop
Hi,

Thanx a lot for your support. It's working fine now.
You're right, there was something wrong with my loop command.
Reply With Quote
Reply

Tags
from clause, sybase, variable

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