简体   繁体   中英

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). 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. 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, ? 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. With the SqlClient API, parameter names are used as parameter markers within queries (eg @YourParameter ) and passed by name from the app code.

If you need help with properly passing parameters, post the client code and specify the language and driver are you using.

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