简体   繁体   English

SQL查询错误-语法错误,接近1,期望值为%

[英]SQL query error - Syntax error near 1 expecting %

Hi I am getting an error as syntax error near 1 expecting % @ line 16(Kauf.ID = %1). 嗨,我收到一个错误,因为语法错误接近1,期望%@第16行(Kauf.ID =%1)。 Please help : 请帮忙 :

select 

      SubWork_Title as "Workflow Title", 
      SubWorkTask_Title as "Step Name",
      KUAF.Name "User Account", 
      KUAF.LastName + ', ' + KUAF.FirstName "User Name", 
      Work_DateInitiated "Workflow Initiation Date", 
      SubWorkTask_PerformerID "User Id", 
      SubWork_WorkID "Workflow Id", 
      SubWork_SubWorkID "Subworkflow Id" 

from WSubWork, WSubWorkTask, WWork, KUAF 

where SubWork_SubWorkID = SubWorkTask_SubWorkID 
       and SubWorkTask_PerformerID = KUAF.ID 
       and SubWork_WorkID = Work_WorkID 
       and SubWorkTask_Status > 0 
       and KUAF.ID =%1 
       and Work_Status > 0

What you're really trying to do here is: 您实际上要在这里做的是:

AND RIGHT(KUAF.ID,1) = 1

This, however, is going to be a hit on the performance of your query, as it'll make the query non-SARGable. 但是,这将对查询的性能造成影响,因为它将使查询不可SARG。 Thus, a better way would be to use a modulus: 因此,更好的方法是使用模数:

AND KUAF.UD % 10 = 1

Example: 例:

WITH VTE  AS (
    SELECT 654365465  AS  ID
    UNION ALL
    SELECT 869735476851  AS  ID
    UNION ALL
    SELECT 548947981  AS  ID
    UNION ALL
    SELECT 897684984  AS  ID
    UNION ALL
    SELECT 45679841  AS  ID
    UNION ALL
    SELECT 984768477  AS  ID
    UNION ALL
    SELECT 9876541  AS  ID
    UNION ALL
    SELECT 1  AS  ID
    )
SELECT ID
FROM VTE
WHERE ID % 10 = 1;

This returns: 返回:

ID
---------------------------------------
869735476851
548947981
45679841
9876541
1

With SQL Server ODBC, OLE DB and JDBC drivers, ? 在SQL Server ODBC,OLE DB和JDBC驱动程序, ? is used for parameter markers within the query and those are mapped to supplied values by ordinal. 用于查询中的参数标记,并按序将其映射到提供的值。 The %1 here is interpreted as a T-SQL bitwise AND with literal 1, not a parameter. 此处的%1被解释为T-SQL按位与,且带有文字1,而不是参数。 With the SqlClient API, parameter names are used as parameter markers within queries (eg @YourParameter ) and passed by name from the app code. 使用SqlClient API,参数名称用作查询中的参数标记(例如@YourParameter ),并按名称从应用程序代码传递。

If you need help with properly passing parameters, post the client code and specify the language and driver are you using. 如果您需要有关正确传递参数的帮助,请发布客户端代码并指定您使用的语言和驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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