Try using
sp_executesql[
^] instead of exec as it allows parameter definitions (both in and out)
Something like this might work (warning - I have not been able to test this)
Begin
Declare @result int
DECLARE @sSQL nvarchar(max)
DECLARE @ParmDefinition nvarchar(max)
SET sSQL = 'Set @resultOUT=SELECT OBJECTPROPERTY( OBJECT_ID(N''' + @TableName + '''), ''' +'TableHasIdentity''' + ')'''
SET @ParmDefinition = N'@resultOUT int OUTPUT';
EXEC sp_executesql @sSQL, @ParmDefinition, @resultOUT=@result OUTPUT;
Key thing to note is that the output parameter is not defined outside of the SQL - i.e. notice the difference between
@result
and
@resultOUT