Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    25

    Unanswered: Using a Variable for a table name in FROM clause

    Hi,

    I am working on sybase 12. I have a cursor which fetches the record from the table and store it in a variable. This Variable actually contain a table name which is later on passed to the query in the FROM clause as a table name.

    But Sybase is not able to convert this variable and throws a syntax error.

    Any specific technic to achieve this.

    set nocount on
    go
    DECLARE c1_old cursor
    for select kdb.KdbTables_Name
    from kplus..KdbTables kdb
    where kdb.KdbTables_Name IN ('SpotDeals', 'BondsDeals')
    for read only
    go
    open c1_old
    declare @var1 varchar(20)
    FETCH c1_old INTO @var1
    while (@@sqlstatus != 2)

    begin
    declare @tname varchar(20)
    select @tname = 'kplus..' + @var1

    select *
    from
    @tname

    FETCH c1_old INTO @var1
    end


    Note: This is just an example not an actual query what I want. Just to keep the example simple.

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    use dynamic sql
    Code:
    exec ('select * from '+@tname)

  3. #3
    Join Date
    Jan 2010
    Posts
    25
    thanks....
    It works

Posting Permissions

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