The Transact-SQL statements in the executed string do not have access to any of the variables declared in the batch that contains the sp_executesql or EXECUTE statement. The batch containing the sp_executesql or EXECUTE statement does not have access to variables or local cursors defined in the executed string
but I do not like to create a store procedure to get a output.
How can I get result from execute('...') like this:
declare @result int
execute('select @result=count(*) from table_cost')