Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sqlalchemy“SELECT*INTO-tentable”配方无法使用文本绑定进行编译_Python_Sql_Sqlalchemy - Fatal编程技术网

Python sqlalchemy“SELECT*INTO-tentable”配方无法使用文本绑定进行编译

Python sqlalchemy“SELECT*INTO-tentable”配方无法使用文本绑定进行编译,python,sql,sqlalchemy,Python,Sql,Sqlalchemy,对于我的特定用例,我需要: 从一些表格中选择*进入tmp SQL server构造,我需要在语句实际发送到数据库之前,在命令行上将包含内联参数的完整编译语句显示给用户 我使用了以下对SQL Server语法稍加调整的方法: 虽然它确实有效,但我无法让它内联显示参数 以下是实际代码: from sqlalchemy.sql import Select, table, column from sqlalchemy.ext.compiler import compiles class Select

对于我的特定用例,我需要:

从一些表格中选择*进入tmp

SQL server构造,我需要在语句实际发送到数据库之前,在命令行上将包含内联参数的完整编译语句显示给用户

我使用了以下对SQL Server语法稍加调整的方法:

虽然它确实有效,但我无法让它内联显示参数

以下是实际代码:

from sqlalchemy.sql import Select, table, column
from sqlalchemy.ext.compiler import compiles

class SelectInto(Select):
    def __init__(self, columns, into, *arg, **kw):
        super(SelectInto, self).__init__(columns, *arg, **kw)
        self.into = into

@compiles(SelectInto)
def s_into(element, compiler, **kw):
    text = compiler.visit_select(element)
    text = text.replace('FROM', f'INTO {element.into} \nFROM')
    return text


employee = table('employee', column('id'), column('name'))
select_into = SelectInto([employee.c.id, employee.c.name], "##tmp").select_from(employee).where(employee.c.id.in_([1, 3, 6]))
print(select_into.compile(compile_kwargs={'literal_binds': True}).string)
然而,这返回:

SELECT employee.id, employee.name 
INTO ##tmp 
FROM employee 
WHERE employee.id IN (:id_1, :id_2, :id_3)
而不是:

SELECT employee.id, employee.name 
INTO ##tmp 
FROM employee 
WHERE employee.id IN (1, 3, 6)
我花了这么多时间想弄清楚原因,但我就是不知道。这个SelectInto类是sqlalchemy Select类的子类,该类使用内联文本绑定编译语句,没有任何问题

为什么它不在这里工作


非常感谢您的帮助

在自定义编译器中转发关键字参数:

@compiles(SelectInto)
def s_into(element, compiler, **kw):
    text = compiler.visit_select(element, **kw)
    text = text.replace('FROM', f'INTO {element.into} \nFROM')
    return text
目前,VISICE\u select没有看到文字绑定参数,因此它默认为编译占位符。

可能重复: