Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    8

    Unanswered: 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.

  2. #2
    Join Date
    Jan 2004
    Posts
    545
    Provided Answers: 4
    Does select col1,col2 from ' + @tablename + ' where mycondition return any data?
    I'm not crazy, I'm an aeroplane!

  3. #3
    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.

  4. #4
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    Then it's something wrong with your loop
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  5. #5
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •