SQL-将where子句作为变量传递(红移)

SQL-将where子句作为变量传递(红移),sql,amazon-redshift,dynamic-sql,Sql,Amazon Redshift,Dynamic Sql,我使用的SQL查询只有where子句更改。我希望将SQL查询存储为一个变量,并将每个where子句存储为单独的变量 下面是我正在使用的SQL查询: 问题1: select a.prod_name,a.prod_cat,b.sale_date from products a join sales b on a.id = b.sale_id and b.type=new 问题2: select a.prod_name,a.prod_cat,b.sale_date from produc

我使用的SQL查询只有where子句更改。我希望将SQL查询存储为一个变量,并将每个where子句存储为单独的变量

下面是我正在使用的SQL查询:

问题1:

select a.prod_name,a.prod_cat,b.sale_date 
from products a 
   join sales b on a.id = b.sale_id and b.type=new
问题2:

select a.prod_name,a.prod_cat,b.sale_date 
from products a 
  join sales b on a.id = b.sale_id and b.type=modify

我计划编写一个Python脚本,使用变量执行此查询

在Python中,您要传递变量。如果您希望使用简单的python,但是如果您的案例如此简单,那么为什么还要引入python,简单的shell脚本应该适合您。不管怎样

import psycopg2


def redshift(type_var):


    conn = psycopg2.connect(dbname='*********', host='*********.redshift.amazonaws.com', port='5439', user='****', password='*******8')
    cur = conn.cursor();

    cur.execute("begin;")

    sql ="select a.prod_name,a.prod_cat,b.sale_date from products a   join sales b on a.id = b.sale_id and b.type='%s'" %(type_var)
    cur.execute(sql)
    results = cur.fetchall()
    print("Copy executed fine!"+results)




"""Call one with new"""
redshift('new');
"""Call two with modify"""
redshift('modify');

从潜在的SQL注入的角度来看,这可能是不可取的。你如何使用红移?如果您将它与Java或C#等语言一起使用,那么很可能您可以编写一个准备好的语句来处理此要求。@TimBiegeleisen,我使用的数据库是Redshift,因此意味着我使用的是Redshift DB。此外,我还将使用Python执行此脚本。我想使用变量的原因纯粹是为了维护脚本。您不能用Python编写语句吗?