SQL查询错误 - 语法错误接近1期待%

时间:2018-06-14 08:22:50

标签: sql sql-server tsql

嗨我收到错误,因为语法错误接近1期待%@第16行(Kauf.ID =%1)。请帮忙:

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

2 个答案:

答案 0 :(得分:2)

你真正想做的是:

AND RIGHT(KUAF.ID,1) = 1

但是,这会对查询的性能产生影响,因为它会使查询成为非SARGable。因此,更好的方法是使用模数:

AND KUAF.UD % 10 = 1

示例:

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;

返回:

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

答案 1 :(得分:1)

使用SQL Server ODBC,OLE DB和JDBC驱动程序,?用于查询中的参数标记,并按顺序映射到提供的值。这里的%1被解释为T-SQL按位AND和文字1,而不是参数。使用SqlClient API,参数名称将用作查询中的参数标记(例如@YourParameter),并通过应用程序代码中的名称传递。

如果您需要有关正确传递参数的帮助,请发布客户端代码并指定您使用的语言和驱动程序。