-- 定义变量
DECLARE @OwnerCode NVARCHAR(50);
SET @OwnerCode=N'3502100082' ;
DECLARE @Code NVARCHAR(50);
SET @Code='QDJH082601' ;
-- 定义动态sql语句
DECLARE @sql_dt nvarchar(max);
SET @sql_dt = N'
select
*
from
Ent_DeclarePlan t1
left join Ent_DeclarePlandtl_'+@OwnerCode+' t2
on t1.ID=t2.PID
where t1.code='''+@Code+''' ;'; -- ''代表转义后的‘
EXEC sp_executesql @sql_dt;