The officer or person upon whom the writ of habeas corpus is served shall produce the body of the applicant before the court at the hearing of the cause of imprisonment or detainer. If the applicant is in the custody of a civil officer, the court or judge who granted the writ shall determine the expense of bringing the applicant to court, which shall be paid prior to the hearing. If remanded, security must be given to pay the charges for carrying the applicant back. If the applicant is confined in a state correctional facility or state hospital, the court shall issue an order commanding the sheriff of the county in which the application is made to take custody of the applicant during the pendency of any proceedings before the court and to transport the applicant from and return the applicant to a state correctional facility or state hospital if the applicant is not released.
SDCL 21-27-9.2