A 2.4-litre four-cylinder Z24i engine was used until 1989, which is most likely in the '87 truck; it produced around 106 hp. In 1989, this engine was replaced by the KA24E of similar displacement which was used for the 1990 through 1997 model years, which is most likely in the '97 truck; it produced around 134 hp being the same engine as used in the 1989 through 1990 240SX.
If you want to replace the Z24i on the '87 with the KA24E from the 240SX, you'll have to change the oil pan; the sump is in the wrong place. From what I've been told, the Z24i tranny should bolt up. The electrical wiring swap has to be researched; some folks used the stock wiring and others swapped to the KA24E wiring.
Good luck on your project.