We are talking about two different matters here:
- The laws in Deut 17-25 are discussing CIVIL jurisprudence as administered by earthly authorities.
- The incident in 2 Sam 12:12, 13 involved as DIVINE judgement.
God is not bound by earthly civil law. Note the reason (NKJV) that God delivers this judgement (2 Sam 12:14),
However, because by this deed you have given great occasion to the
enemies of the LORD to blaspheme, the child also who is born to you
shall surely die.”
David's sin as king of God's chosen people, despite his sinful attempts at cover-up, were well known in the surrounding nations. This could not go un-noticed and had to dealt with swiftly and conspicuously. In His divine wisdom, God elected to deal with this David's heinous crimes in two ways:
- Show compassion and grace and spare David's life
- Deliver to David the punishment he had pronounced upon himself (2 Sam 12:6) and have four of his sons die, who were: Bathsheba's (unnamed) son, Amnon (2 Sam 13), Absalom (2 Sam 14-18), Adonijah (1 Kings 1, 2:13-25).