Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004
    Posts
    38

    Unanswered: "limit" in sql server

    someone suggested I use:

    select top 20 * from t
    where pkc not in (select top 10 pkc from t order by pkc) order by pkc

    to simulate the limit function in mysql.

    i want to replace the 10 in the inner select with a variable. When I do this:

    select top 20 * from t
    where pkc not in (select top @counter pkc from t order by pkc) order by pkc

    it gives me in an error in sproc. Pls help!!

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    It needs to be dynamic

    Code:
    DECLARE @SQL varchar(8000), @Counter int
    SELECT @Counter = 10
    SELECT @SQL = 'select top 20 * from t'
    	+ ' where pkc not in (select top ' 
    	+ CONVERT(varchar(15),@counter) 
    	+ ' pkc from t order by pkc) order by pkc'
    SELECT @SQL
    EXEC(@SQL)
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

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