我正在iBatis中处理分页的一些查询工作。
当我运行以下查询时,似乎执行正常:
SELECT
COUNT(1) OVER (PARTITION BY NULL) AS TotalRows,
ROW_NUMBER() OVER (ORDER BY [User].Username ASC) AS RowNum
FROM
dbo.[USER]
LEFT OUTER JOIN Account
on [User].AccountId = Account.AccountID
LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
LEFT OUTER JOIN PasscodeAccount
on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
WHERE
[User].Active = 1
但是,当我尝试将不同的数据子集替换为" TotalRows"的值时,我收到以下T-SQL错误:
Incorrect syntax near the keyword 'OVER'
请参阅以下提供错误的查询:
SELECT
COUNT(1) OVER (PARTITION BY NULL) FROM (
SELECT DISTINCT
[User].Id as "User_Id",
[User].AccountId as "User_AccountId",
[User].AccountId as "User_AccountAssociation",
[User].Username as "User_Username",
[User].Password as "User_Password",
[User].Name as "User_Name",
[User].PhoneNumber as "User_PhoneNumber",
[User].EmailAddress as "User_EmailAddress",
[User].CreatedOn as "User_CreatedOn",
[User].CreatedBy as "User_CreatedBy",
[User].ChangedOn as "User_ChangedOn",
[User].ChangedBy as "User_ChangedBy",
[User].LastLogin as "User_LastLogin",
[User].TemporaryPassword as "User_TemporaryPassword",
[User].Active as "User_Active",
Account.AccountID as "Account_AccountId",
Account.CompanyID as "Account_CompanyId",
Account.AccountName as "Account_AccountName",
Account.AccountType as "Account_AccountType",
Account.LoginID as "Account_LoginID",
Account.LoginPassword as "Account_LoginPassword",
Account.AccountBillingCode as "Account_AccountBillingCode",
Account.DefaultTimeZone as "Account_DefaultTimeZone",
Account.AuthorizationLevelID as "Account_AuthorizationLevelID",
Account.CreatedBy as "Account_CreatedBy",
Account.ChangedBy as "Account_ChangedBy",
Account.RegistrationFormID as "Account_RegistrationFormID",
Account.CreatedOn as "Account_CreatedOn",
Account.ChangedOn as "Account_ChangedOn",
Account.DataPresenterID as "Account_DataPresenterID",
Account.DefaultLoginPIN as "Account_DefaultLoginPIN",
Account.CopyrightText as "Account_CopyrightText",
Account.LiveParticipantListEnabled as "Account_LiveParticipantListEnabled",
Account.MaxLiveParticipantListusers as "Account_MaxLiveParticipantListusers",
Account.LoginText as "Account_LoginText",
Account.CMeetingIntegration AS "Account_CMeetingIntegration"
FROM
[User]
LEFT OUTER JOIN Account on [User].AccountId = Account.AccountID
LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
LEFT OUTER JOIN PasscodeAccount on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
WHERE
[User].Active = 1
GROUP BY [User].Id,[User].AccountId,[User].AccountId,[User].Username,[User].Password,[User].Name,[User].PhoneNumber,[User].EmailAddress,
[User].CreatedOn,[User].CreatedBy,[User].ChangedOn,[User].ChangedBy,[User].LastLogin,[User].TemporaryPassword,[User].Active,
Account.AccountID,Account.CompanyID,Account.AccountName,Account.AccountType,Account.LoginID,Account.LoginPassword,Account.AccountBillingCode,
Account.DefaultTimeZone,Account.AuthorizationLevelID,Account.CreatedBy,Account.ChangedBy,Account.RegistrationFormID,Account.CreatedOn,
Account.ChangedOn,Account.DataPresenterID,Account.DefaultLoginPIN,Account.CopyrightText,Account.LiveParticipantListEnabled,
Account.MaxLiveParticipantListusers,Account.LoginText,Account.CMeetingIntegration)
AS TotalRows,
ROW_NUMBER() OVER (ORDER BY [User].Username ASC) AS RowNum
FROM
dbo.[USER]
LEFT OUTER JOIN Account on [User].AccountId = Account.AccountID
LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
LEFT OUTER JOIN PasscodeAccount on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
WHERE
[User].Active = 1
请注意,上述查询将执行正常,直到它到达以下行:
ROW_NUMBER() OVER (ORDER BY [User].Username ASC) AS RowNum
我想我的问题是"为什么第一个查询运行正常,但是当我尝试使用一部分数据来填充“TotalRows'关于' RowNum'的OVER子句,我收到了错误的语法错误如上面的陈述?"
请耐心等待我,因为我不太熟悉T-SQL。
如果您有任何问题或需要进一步澄清,我将很乐意为您提供帮助。
答案 0 :(得分:1)
你的语法错误在ROW_NUMBER()OVER附近(ORDER BY [User] .Username ASC))AS RowNum
您可以按以下方式修改查询
;with cte as (
SELECT DISTINCT
[User].Id as "User_Id",
[User].AccountId as "User_AccountId",
[User].AccountId as "User_AccountAssociation",
[User].Username as "User_Username",
[User].Password as "User_Password",
[User].Name as "User_Name",
[User].PhoneNumber as "User_PhoneNumber",
[User].EmailAddress as "User_EmailAddress",
[User].CreatedOn as "User_CreatedOn",
[User].CreatedBy as "User_CreatedBy",
[User].ChangedOn as "User_ChangedOn",
[User].ChangedBy as "User_ChangedBy",
[User].LastLogin as "User_LastLogin",
[User].TemporaryPassword as "User_TemporaryPassword",
[User].Active as "User_Active",
Account.AccountID as "Account_AccountId",
Account.CompanyID as "Account_CompanyId",
Account.AccountName as "Account_AccountName",
Account.AccountType as "Account_AccountType",
Account.LoginID as "Account_LoginID",
Account.LoginPassword as "Account_LoginPassword",
Account.AccountBillingCode as "Account_AccountBillingCode",
Account.DefaultTimeZone as "Account_DefaultTimeZone",
Account.AuthorizationLevelID as "Account_AuthorizationLevelID",
Account.CreatedBy as "Account_CreatedBy",
Account.ChangedBy as "Account_ChangedBy",
Account.RegistrationFormID as "Account_RegistrationFormID",
Account.CreatedOn as "Account_CreatedOn",
Account.ChangedOn as "Account_ChangedOn",
Account.DataPresenterID as "Account_DataPresenterID",
Account.DefaultLoginPIN as "Account_DefaultLoginPIN",
Account.CopyrightText as "Account_CopyrightText",
Account.LiveParticipantListEnabled as "Account_LiveParticipantListEnabled",
Account.MaxLiveParticipantListusers as "Account_MaxLiveParticipantListusers",
Account.LoginText as "Account_LoginText",
Account.CMeetingIntegration AS "Account_CMeetingIntegration"
FROM
[User]
LEFT OUTER JOIN Account on [User].AccountId = Account.AccountID
LEFT OUTER JOIN UserPasscodeAccount on [User].Id = UserPasscodeAccount.UserID
LEFT OUTER JOIN PasscodeAccount on UserPasscodeAccount.PasscodeAccountID = PasscodeAccount.ID
WHERE
[User].Active = 1
GROUP BY [User].Id,[User].AccountId,[User].AccountId,[User].Username,[User].Password,[User].Name,[User].PhoneNumber,[User].EmailAddress,
[User].CreatedOn,[User].CreatedBy,[User].ChangedOn,[User].ChangedBy,[User].LastLogin,[User].TemporaryPassword,[User].Active,
Account.AccountID,Account.CompanyID,Account.AccountName,Account.AccountType,Account.LoginID,Account.LoginPassword,Account.AccountBillingCode,
Account.DefaultTimeZone,Account.AuthorizationLevelID,Account.CreatedBy,Account.ChangedBy,Account.RegistrationFormID,Account.CreatedOn,
Account.ChangedOn,Account.DataPresenterID,Account.DefaultLoginPIN,Account.CopyrightText,Account.LiveParticipantListEnabled,
Account.MaxLiveParticipantListusers,Account.LoginText,Account.CMeetingIntegration)
SELECT
COUNT(1) OVER (PARTITION BY NULL) AS TotalRows,
ROW_NUMBER() OVER ( ORDER BY cte.Username ASC) AS RowNum
FROM cte