You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用SQL*Plus执行@l.sql时出现SP2-0310错误的排查求助

Python调用SQL*Plus执行@l.sql时出现SP2-0310错误的排查求助

我最近在写Python脚本批量调用SQL*Plus执行SQL文件时遇到了问题,想请大家帮忙看看哪里出了问题。

我的操作步骤和代码如下:

首先,我先切换到目标SQL文件所在的目录:

import os

# Specify the directory you want to change to, take input from keyboard
directory_path = r"C:\code\xyz\deployment\tbd\example"

# Change the directory
os.chdir(directory_path)

然后我实现了一个执行SQL*Plus命令的工具函数:

def execute_sqlplus_command(command):
    """Executes a SQL*Plus command and returns the output."""

    session = subprocess.Popen(
        ["sqlplus", "-S", "test/password@database"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    sql_command = command.encode() + b";\n"
    stdout, stderr = session.communicate(sql_command)

    return stdout.decode(), stderr.decode()

接下来我用这个函数执行两条SQL命令:

# calls for running script
command = "set role l;"
output, error = execute_sqlplus_command(command)

command = r"@l.sql;"
output, error = execute_sqlplus_command(command)

现在的问题是,第一条set role l执行完全正常,但第二条@l.sql;却抛出了错误:
Output: SP2-0310: unable to open file "l.sql;"

我已经尝试过以下几种排查方法,但都没有解决问题:

  • 直接在SQL*Plus客户端里运行@l.sql;,是可以正常执行的
  • 我试过把@l.sql;换成指向文件的完整绝对路径,依然报错无法打开文件
  • 我检查并调整过l.sql文件的权限设置,确保没有访问限制

另外补充一下,这个目录下还有其他SQL相关的调用,我希望脚本能保持通用性,因为后续要批量运行几百个同名的SQL文件。

有没有朋友能帮我分析下,是不是我的Python脚本里有什么细节没处理对?

备注:内容来源于stack exchange,提问作者BBBBBBBBBBBBBBBBBBBBBBBBB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:14:36