In here I've declared two variables, one is for writing a query, the other is to store the output.
declare @EM_NUMBER nvarchar(100)
declare @SQL nvarchar(max)
set @SQL='select vendor.VD_TABLE_KEY_VALUE
from [HS_EM_VENDOR_DATA_MAP] as vendor
inner join [HS_HR_EMPLOYEE] as emp on vendor.VD_TABLE_KEY_VALUE = emp.EMP_NUMBER
where vendor.VD_TABLE_NAME = ''HS_HR_EMPLOYEE''
and vendor.VD_TENANT_UNIQUE_ID = ' + @VD_TENANT_UNIQUE_ID + '
and emp.HIE_CODE_' + @DEF_LEVEL + ' = ' + @HIE_CODE + '
and vendor.VD_TABLE_REF_VALUE = ' + @XeroEmpNumber
Once I execute this @SQL output something like this
EXEC sp_executesql @SQL
Output:
I want to store this output into @EM_NUMBER
variable