联合来自SQL Server

时间:2015-10-16 01:20:48

标签: sql-server

我有这个SQL块代码。

use TestDatabase

declare @keyword nvarchar(1000) = '';
declare @sqlKeyword nvarchar(1000) = '';

set @sqlKeyword =
    'select * into #tmphosovanban from TblHoSoVB
            select *  from(
                select * from (
                --tim tu khoa theo district
                        ( select * from  #tmphosovanban
                            where  DCHCHuyenID in (select DistrictID from MstDistrict where DistrictName like N''%'+@keyword+'%'' )
                            or DatHCHuyenID in (select DistrictID from MstDistrict where DistrictName like N''%'+@keyword+'%'' )
                             )
                        union

                --tim tu khoa theo town
                        (select * from  #tmphosovanban
                            where DCHCXaID in (select TownID from MstTown where TownName like N''%'+@keyword+'%'')
                            or DatHCXaID in (select TownID from MstTown where TownName like N''%'+@keyword+'%'')
                            )
                        union
                --tim tu khoa theo nguon goc dat
                        (select * from  #tmphosovanban
                            where NGSuDungDatID in (select MasterID from MstCommonMaster where MasterName like N''%'+@keyword+'%'')
                            or MDSuDungDatID in (select MasterID from MstCommonMaster where MasterName like N''%'+@keyword+'%'')
                            or LoaiTaiSanID in (select MasterID from MstCommonMaster where MasterName like N''%'+@keyword+'%'')
                            )
                        union 
                        (select * from  #tmphosovanban
                            where ViTriLuuID in (select GiaID from MstViTriGia where TenGia like N''%'+@keyword+'%'')
                        )

                ) as temp
        ) as temp2 
        drop table #tmphosovanban'
exec(@sqlKeyword)

我收到错误

  

Msg 102,Level 15,State 1,Line 21
  ')'附近的语法不正确。

当我在SQL Server上运行此块时。我不知道为什么这是错的。它在我删除下列其中一个块时运行,所以我认为我的语法是正确的,因为另一个原因它不会运行。我该如何解决这个问题?

        --tim tu khoa theo district
                ( select * from  #tmphosovanban
                    where  DCHCHuyenID in (select DistrictID from MstDistrict where DistrictName like N''%'+@keyword+'%'' )
                    or DatHCHuyenID in (select DistrictID from MstDistrict where DistrictName like N''%'+@keyword+'%'' )
                     )
                union


        --tim tu khoa theo town
                (select * from  #tmphosovanban
                    where DCHCXaID in (select TownID from MstTown where TownName like N''%'+@keyword+'%'')
                    or DatHCXaID in (select TownID from MstTown where TownName like N''%'+@keyword+'%'')
                    )
                union

        --tim tu khoa theo nguon goc dat
                (select * from  #tmphosovanban
                    where NGSuDungDatID in (select MasterID from MstCommonMaster where MasterName like N''%'+@keyword+'%'')
                    or MDSuDungDatID in (select MasterID from MstCommonMaster where MasterName like N''%'+@keyword+'%'')
                    or LoaiTaiSanID in (select MasterID from MstCommonMaster where MasterName like N''%'+@keyword+'%'')
                    )
                union 

1 个答案:

答案 0 :(得分:2)

你的问题是你定义了一个包含1000字符的变量并在记事本中计算字符串查询++我注意到该字符串包含1770+个字符,因此SQL Server截断你的字符串因此试图执行不完整的查询。

将您的变量更改为@sqlKeyword nvarchar(2000),您应该没问题。