Painless gastroenteroscopy is a widely developed diagnostic and treatment technology in clinical practice. It is of great significance in the clinical diagnosis, treatment, follow-up review and other aspects of gastric cancer patients. The application of anesthesia techniques during manipulation can be effective in reducing patient fear and discomfort. In clinical work, the adverse drug reactions of anesthesia regimens and the risk of serious adverse drug reactions are increased with the increase in propofol application dose application dose; the application of opioid drugs often causes gastrointestinal reactions, such as nausea, vomiting and delayed gastrointestinal function recovery, after examination. These adverse effects can seriously affect the quality of life of patients. To observe the effect of modified ShengYangYiwei decoction on gastrointestinal function, related complications and immune function in patients with gastric cancer during and after painless gastroscopy. A total of 106 patients with gastric cancer, who were selected from January 2022 to September 2022 in Xiamen Traditional Chinese Medicine Hospital for painless gastroscopy, were randomly divided into a treatment group (n = 56) and a control group (n = 50). Before the examination, all patients fasted for 8 h, provided their health education, and confirmed if there were contraindications to anesthesia and gastroscopy. During the examination, the patients were placed in the left decubitus position, the patients were given oxygen through a nasal catheter (6 L/min), the welling needle was opened for the venous channel, and a multifunction detector was connected for monitoring electrocardiogram, oxygen saturation, blood pressure, etc. Naporphl and propofol propofol protocols were used for routine anesthesia. Before anesthesia administration, the patients underwent several deep breathing exercises, received intravenous nalbuphine [0.nalbuphine (0.025 mg/kg)], followed by intravenous propofol [1.propofol (1.5 mg/kg)] until the palpebral reflex disappeared, and after no response, gastroscopy was performed. If palpebral reflex disappeared, and after no response, gastroscopy was performed. If any patient developed movement, frowning, or hemodynamic changes during the operation (heart rate changes during the operation (heart rate increased to > 20 beats/min, systolic blood pressure increased to > 20% of the base value), additional propofol [0.propofol (0.5 mg/kg)] was added until the patient was sedated again. The patients in the treatment group began to take the preventive intervention of Modified ShengYangYiwei decoction one week before the examination, while the patients in the control group received routine gastrointestinal endoscopy. The patients in the two groups were examined by conventional painless gastroscopy, and the characteristics of the painless gastroscopies of the patients in the two groups were recorded and compared. These characteristics included the total dosage of propofol during the examination, the incidence of complications during the operation, the time of patients' awakening, the time of independent activities, and the gastrointestinal function of the patients after examination, such as the incidence of reactions such as malignant vomiting, abdominal distension and abdominal pain, as well as the differences in the levels of various immunological indicators and inflammatory factors before anesthesia induction (T0), after conscious extubation (T1) and 24 h after surgery (T2). There was no difference in the patients' general information, American Society of Anesthesiologist classification or operation time between the two groups before treatment. In terms of painless gastroscopy, the total dosage of propofol in the treatment group was lower than that in the control group (P < 0.05), and the time of awakening and autonomous activity was significantly faster than that in the control group (P < 0.05). During the examination, the incidence of hypoxemia, hypotension and hiccups in the treatment group was significantly lower than that in the control group (P < 0.01). In terms of gastrointestinal function, the incidences of nausea, vomiting, abdominal distension and abdominal pain in the treatment group after examination were significantly lower than those in the control group (P < 0.01). In terms of immune function, in both groups, the number of CD4+ and CD8+ cells decreased significantly (P < 0.05), and the number of natural killer cells increased significantly (P < 0.05) at T1 and T2, compared with T0. The number of CD4+ and CD8+ cells in the treatment group at the T1 and T2 time points was higher than that in the control group (P < 0.05), while the number of natural killer cells was lower than that in the control group (P < 0.05). In terms of inflammatory factors, compared with T0, the levels of interleukin (IL) -6 and tumor necrosis factor-alpha in patients in the two groups at T1 and T2 increased significantly and then decreased (P < 0.05). The level of IL-6 at T1 and T2 in the treatment group was lower than that in the control group (P < 0.05). The preoperative use of modified ShengYangYiwei decoction can optimize the anesthesia program during painless gastroscopy, improve the gastrointestinal function of patients after the operation, reduce the occurrence of examination-related complications.