Every court of record has power, upon the application of any party to any suit or proceeding, civil or criminal, pending in such court, to issue a writ of habeas corpus, for the purpose of bringing before the said court any prisoner who may be detained in any jail or prison within the State, for any cause, except a prisoner under sentence for a capital felony, to be examined as a witness in such suit or proceeding in behalf of the party making the application.
Such writ of habeas corpus may be issued by any magistrate or clerk of the superior court, upon application as provided in this section, to bring any person confined in the jail or prison of the same county where such magistrate or clerk may reside, to be examined as a witness before such magistrate or clerk.
In cases where the testimony of any prisoner is needed in a proceeding before a magistrate, or a clerk, and such person is confined in a county in which such magistrate or clerk does not reside, application for habeas corpus to testify may be made to any justice or judge of the General Court of Justice.
N.C. Gen. Stat. § 17-41