简体   繁体   中英

sql 2000 is'not executing the query that 2008 can execute

If I execute the following SQL statement in SQL server 2008 it works perfectly, but when execute the same statement in SQL Server 2000 it doesn't work:

Statement:

select top 1  
  [k].[FixbiUnitPrice] 
from  (
  select top (select COUNT(*) 
              from [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817') f) 
    [FixbiUnitPrice],
    BDate,
    biNumber 
  From [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817') f  
  where f.BAccCustID != 0x0 
  order by f.BDate desc,f.BNumber desc,f.biNumber desc
) [k]

Output in SQL Server 2000:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FixbiUnitPrice'.

What's wrong with syntax for SQL Server 2000

Support for 'dynamic' top in sql server started in 2005 version. for sql server 2000 you can only use a constant number after top . you can probably use SET ROWCOUNT for your query.

Also, Read this post and it's answers.

In addition to @Zohar's point that a variable TOP @N isn't supported in Sql 2000, what you could also do is generate Dynamic Sql and then execute it, ie:

DECLARE @TopCount INT
DECLARE @Sql NVARCHAR(2000)

SELECT @TopCount = COUNT(*) 
FROM [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817')

SET @Sql = 
'select top 1 
  [k].[FixbiUnitPrice] 
from  (
  select top ' + CONVERT(NVARCHAR(50), @TopCount) + '
    [FixbiUnitPrice],
    BDate,
    biNumber 
  From [dbo].[mnrFnBI_Fixed](''4E591E71-33BD-4ECC-8703-771BE8A76817'') f  
  where f.BAccCustID != 0x0 
  order by f.BDate desc,f.BNumber desc,f.biNumber desc
) [k]'

EXEC sp_executesql @Sql

That said, Sql 2000 is really an unsupported, legacy technology and you need to consider upgrading to a more recent version of Sql Server as soon as possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM