DECLARE @IKC VARCHAR(50),
@TSQL NVARCHAR(500),
@MDResult NVARCHAR(500),
@ParmDefinition NVARCHAR(500),
@Result NVARCHAR(500)
SET QUOTED_IDENTIFIER OFF
SET @IKC = '80700-08396-70846-400143'
Set @TSQL = "SELECT @Result = MED_SUB_ID FROM OPENQUERY([PACT-A],'SELECT M.MED_SUB_ID FROM MEDIA_DETAIL MDD JOIN MEDIA M ON MDD.MDD_MED_ID = M.MED_ID WHERE MDD.MDD_STRING_VALUE=''"+ @IKC +"''')"
SET @ParmDefinition = N'@Result varchar(30) OUTPUT';
--Exec @TSQL
Exec sp_ExecuteSQL @TSQL, @ParmDefinition, @Result OUTPUT
--sp_ExecuteSQL 'SELECT * FROM OPENQUERY([PACT-A],''SELECT * FROM MEDIA_DETAIL MDD JOIN MEDIA M ON MDD.MDD_MED_ID = M.MED_ID WHERE MDD.MDD_STRING_VALUE=''''+ @IKC +''''')' -- RKNRegistrationAct and field: vchInstallKey
Select @Result