字符串查询中的SQL Server错误语法

时间:2018-07-23 10:44:21

标签: sql sql-server tsql

我试图在sql-server中以字符串形式创建动态查询,如果@SupervisorId或@UnitId为null,它会完美工作,但是当我像下面进行测试时,却收到此错误(表示@SupervisorId或@ UnitId不为空):

  

消息102,级别15,状态1,行10'c33fff6'附近的语法不正确。   消息153,级别15,状态2,第14行下一步的无效用法   在FETCH语句中。消息102,级别15,状态1,第10行不正确   “ c33fff6”附近的语法。

我尝试了什么:

declare @SupervisorId       uniqueidentifier = null;
declare @UnitId             uniqueidentifier = null;
declare @Name               nvarchar(max) = null;
declare @PersonTypeId       int;
declare @PageNum            int;
declare @PageSize           int;
declare @KW nvarchar(max);
declare @Query nvarchar(max);
declare @Query2 nvarchar(max);
declare @WhereClause nvarchar(max);

set @PageNum = 0;
set @PageSize = 50;
set @PersonTypeId = 10003;
set @UnitId = '5c33fff6-ae91-4946-92e2-50f8ae9bd6f5'; 

set @WhereClause = ' 1=1 ';
if (@Name is not null)
    set @WhereClause =@WhereClause + ' And p.Name like N'''+'%'+cast(@Name as nvarchar(4000))+'%'+''' or p.Family like N'''+'%'+cast(@Name as nvarchar(4000))+'%'+''' ';
if(@SupervisorId is not null)
    set @WhereClause = @WhereClause + ' AND p.SupervisorId in ('+ CAST(@SupervisorId as nvarchar(4000)) +') ';
if(@UnitId is not null)
    set @WhereClause = @WhereClause + ' AND p.UnitId in (' + CAST(@UnitId as nvarchar(4000)) + ') ';

    Set @Query = '
     SELECT u.*, p.*, uu.Name as UnitName, jj.Name as JobName, pp.Name + '' '' + pp.Family as SupervisorName, s.IsActive as IsActive
        FROM [Membership].Users u 
        LEFT JOIN [Membership].Persons p on p.ID = u.ID 
        INNER JOIN [kernel].BaseEntity s on s.ID = p.ID and s.IsDelete = 0
        LEFT JOIN Base.Units uu on uu.ID = p.UnitId
        LEFT JOIN Base.Jobs jj on jj.ID = p.JobId
        LEFT JOIN [Membership].Persons pp on pp.ID = p.SupervisorId

    Where '+@WhereClause+ ' and p.PersonTypeId = '+ CAST(@PersonTypeId as nvarchar(100)) +' 

    Order by p.Family DESC
    Offset '+CAST(@PageSize as nvarchar(10))+' * ('+CAST(@PageNum as nvarchar(10))+') Rows
    Fetch next '+CAST(@PageSize as nvarchar(10))+' ROWS ONLY';


    set @Query2 ='
     SELECT COUNT(*)
        FROM [Membership].Users u 
        LEFT JOIN [Membership].Persons p on p.ID = u.ID 
        INNER JOIN [kernel].BaseEntity s on s.ID = p.ID and s.IsDelete = 0
        LEFT JOIN Base.Units uu on uu.ID = p.UnitId
        LEFT JOIN Base.Jobs jj on jj.ID = p.JobId
        LEFT JOIN [Membership].Persons pp on pp.ID = p.SupervisorId

    Where '+@WhereClause + ' and p.PersonTypeId = '+ CAST(@PersonTypeId as nvarchar(100)) +'' ;

    exec (@Query);
    exec (@Query2); 

1 个答案:

答案 0 :(得分:1)

您需要将值括在单引号中。这需要加倍报价。所以代替:

...

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        refresh();
    }
});

//some code here

cards = Card.getActiveCards();
if (cards.isEmpty()) {
    showMessage("Cards unavailable");
    return view;
}
...

您需要:

set @WhereClause = @WhereClause + ' AND p.UnitId in (' + CAST(@UnitId as nvarchar(4000)) + ') ';

请注意,如果实际上有多个值,则还需要将列表中的单引号加倍。