我正在尝试使用 sqlparse 模块中的 split 方法将查询文件拆分为查询列表。当我使用“创建过程”查询时,它会正确拆分。但是当我使用“替换过程”时,它将一个查询分成两个查询。它用分号拆分,忽略开始和结束。
import sqlparse
from sqlparse.keywords import *
query = """REPLACE PROCEDURE u1.SP_Employee (
IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
INSERT INTO u1.employee (
EmpNo,
EmpName
)
VALUES (
:EmpNo,
:EmpName
);
END;
"""
sql_formats = sqlparse.format(query,strip_comments=True, reindent=True)
result = sqlparse.parse(query)
for i in range(0,len(result)) :
print("index is "+str(i))
print(result[i])
print("Result printing is done for replace query")
query = """create PROCEDURE u1.SP_Employee (
IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
INSERT INTO u1.employee (
EmpNo,
EmpName
)
VALUES (
:EmpNo,
:EmpName
);
END;
"""
sql_formats = sqlparse.format(query,strip_comments=True, reindent=True)
result = sqlparse.parse(query)
for i in range(0,len(result)) :
print("index is `enter code here`"+str(i))
print(result[i])
print("Result printing is done for create query")
**Output :-**
index is 0
REPLACE PROCEDURE u1.SP_Employee (
IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
INSERT INTO u1.employee (
EmpNo,
EmpName
)
VALUES (
:EmpNo,
:EmpName
);
index is 1
END;
index is 2
Result printing is done for replace query
index is 0
create PROCEDURE u1.SP_Employee (
IN EmpNo INTEGER, IN EmpName INTEGER
)
BEGIN
INSERT INTO u1.employee (
EmpNo,
EmpName
)
VALUES (
:EmpNo,
:EmpName
);
END;
index is 1
Result printing is done for create query