SQL 单引号转义

declare @userNum varchar(50),@waterNum varchar(50),@tableName varchar(20),@sql varchar(max)
select @userNum=YHBH,@waterNum=SBBH,@tableName=vlg.vlgFZ from YHXX yh left join vlgInf vlg on yh.vlgName=vlg.vlgName where (yh.vlgName+yh.YHDZ) like '恒大金碧天下13幢1单元0101室%'
set @sql='select JZQID,SBBH from '+@tableName+' where YHBH='''+@userNum+''' and SBBH='''+@waterNum+''''
exec(@sql)

where YHBH=' ''+@userNum+' '' and SBBH='''+@waterNum+''''

边界 ' 单引号 用' 单引号 转义

原文地址:https://www.cnblogs.com/kunlunmountain/p/5725878.html