Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2006
    Posts
    13

    Unanswered: Problem with SELECT statement

    Hi All,

    In one of the tables, i've a varchar column by name expression which has values like min(id),max(id) where id is the other column. I'm getting the value for the expression column. Then i want to execute it.

    DECLARE @exp varchar(20)
    SELECT @var1=expression from table1 where id=1 //var1 now holds min(id)
    Now i want to return min(id) by using the varibale name.

    I tried SELECT @var1 FROM table1...but it doesn't work.

    Any help would be appreciated.

  2. #2
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    What DBMS are you using - Microsoft SQL Server? This is the general SQL forum!

  3. #3
    Join Date
    Nov 2006
    Posts
    13
    Oops!! I'm using Sybase DB.I'll post it in Sybase forum. Sorry.

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    What do you mean "it doesn't work"?
    What error do you get?
    You did not declare @var1
    You don't need a from clause when selecting @var1 but you can if you want to e.g.
    Code:
    1> declare @val1 int, @val2 int
    2> select @val1=min(dbid), @val2=max(dbid) from sysdatabases where dbid>30000
    3> select 'MinVal'=@val1
    4> select 'MaxVal'=@val2,'NewId'=dbid-@val1+1+@val2
    5> from sysdatabases where dbid>30000
    6> go
    (1 row affected)
     MinVal      
     ----------- 
           31513 
    
    (1 row affected)
     MaxVal      NewId       
     ----------- ----------- 
           31516       31517 
           31516       31518 
           31516       31519 
           31516       31520 
    
    (4 rows affected)
    Last edited by pdreyer; 12-01-06 at 07:44.

Posting Permissions

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