如何在动态 sql 中写入 '1=1'

How to write a '1=1' within dynamic sql

有人可以更正吗?

declare @g varchar(max)
set @g = 'select *
from person.person
where firstname like '%t' '
set @g = @g + 'or firstname  like''' +('a'= 'a')+ ''''

exec (@g)

您的基本查询应该有 1=1

set @g = 'select *
from person.person
where 1=1 '
set @g = @g + 'and firstname like ''%t'' '
set @g = @g + 'or firstname  like ''' +('a'= 'a')+ ''''

如果您需要对条件进行分组,它们都需要在一起:

set @g = @g +'(firstname like ''%t%'' or firstname like ''%a'')'