在Pandas中读取查询时出现PyODBC SQL类型错误

时间:2018-10-10 20:03:09

标签: python pandas odbc pyodbc

当我从pyodbc连接中写出时,正在寻求有关特定错误的帮助。我该如何解决错误:

  

尚不支持ODBC SQL类型-360。 column-index = 1 type = -360',来自PYODBC的“ HY106”错误

这是我的代码:

import pyodbc
import pandas as pd
import sqlparse

## Function created to read SQL Query
def create_query_string(sql_full_path):
    with open(sql_full_path, 'r') as f_in:
        lines = f_in.read()

    # remove any common leading whitespace from every line
    query_string = textwrap.dedent("""{}""".format(lines))
    ## remove comments from SQL Code
    query_string = sqlparse.format(query_string, strip_comments=True)

    return query_string

query_string = create_query_string("Bad Code from R.sql")

## initializes the connection string
curs = conn.cursor()

df=pd.read_sql(query_string,conn)

df.to_csv("TestSql.csv",index=None)

我们在查询字符串中使用以下SQL代码:

SELECT loss_yr_qtr_cd, 
       CASE 
         WHEN loss_qtr_cd <= 2 THEN loss_yr_num 
         ELSE loss_yr_num + 1 
       END                    AS LOSS_YR_ENDING, 
       snap_yr_qtr_cd, 
       CASE 
         WHEN snap_qtr_cd <= 2 THEN snap_yr_num 
         ELSE snap_yr_num + 1 
       END                    AS CAL_YR_ENDING, 
       cur_ctstrph_loss_ind, 
       clm_symb_grp_cd, 
       adbfdb_pol_form_nm, 
       risk_st_nm, 
       wrt_co_nm, 
       wrt_co_part_cd, 
       src_of_bus_cd, 
       rt_zip_dlv_ofc_cd, 
       cur_rst_rt_terr_cd, 
       Sum(xtra_cntrc_py_amt) AS XTRA_CNTRC_PY_AMT 
FROM   (SELECT DT.loss_yr_qtr_cd, 
               DT.loss_qtr_cd, 
               DT.loss_yr_num, 
               SNAP.snap_yr_qtr_cd, 
               SNAP.snap_qtr_cd, 
               SNAP.snap_yr_num, 
               CLM.cur_ctstrph_loss_ind, 
               CLM.clm_symb_grp_cd, 
               POL_SLCT.adbfdb_pol_form_nm, 
               POL_SLCT.adbfdb_pol_form_cd, 
               CVR.bsic_cvr_ind, 
               POL_SLCT.priv_pass_ind, 
               POL_SLCT.risk_st_nm, 
               POL_SLCT.wrt_co_nm, 
               POL_SLCT.wrt_co_part_cd, 
               POL_SLCT.src_of_bus_cd, 
               TERR.rt_zip_dlv_ofc_cd, 
               TERR.cur_rst_rt_terr_cd, 
               LOSS.xtra_cntrc_py_amt 
        FROM   ahshdm1d.vmaloss_day_dt_dim DT, 
               ahshdm1d.vmasnap_yr_mo_dim SNAP, 
               ahshdm1d.tmaaclm_dim CLM, 
               ahshdm1d.tmaapol_slct_dim POL_SLCT, 
               ahshdm1d.tmaacvr_dim CVR, 
               ahshdm1d.tmaart_terr_dim TERR, 
               ahshdm1d.tmaaloss_fct LOSS, 
               ahshdm1d.tmaaprod_bus_dim BUS 
        WHERE  SNAP.snap_yr_qtr_cd BETWEEN '20083' AND '20182' 
               AND TRIM(POL_SLCT.adbfdb_lob_cd) = 'A' 
               AND CVR.bsic_cvr_ind = 'Y' 
               AND POL_SLCT.priv_pass_ind = 'Y' 
               AND POL_SLCT.adbfdb_pol_form_cd = 'V' 
               AND POL_SLCT.src_of_bus_cd NOT IN ( 'ITC', 'INV' ) 
               AND LOSS.xtra_cntrc_py_amt > 0 
               AND LOSS.loss_day_dt_id = DT.loss_day_dt_dim_id 
               AND LOSS.cvr_dim_id = CVR.cvr_dim_id 
               AND LOSS.pol_slct_dim_id = POL_SLCT.pol_slct_dim_id 
               AND LOSS.rt_terr_dim_id = TERR.rt_terr_dim_id 
               AND LOSS.prod_bus_dim_id = BUS.prod_bus_dim_id 
               AND LOSS.clm_dim_id = CLM.clm_dim_id 
               AND LOSS.snap_yr_mo_dt_id = SNAP.snap_yr_mo_dt_id) AS TABLE1 
GROUP  BY loss_yr_qtr_cd, 
          loss_qtr_cd, 
          loss_yr_num, 
          snap_yr_qtr_cd, 
          snap_qtr_cd, 
          snap_yr_num, 
          cur_ctstrph_loss_ind, 
          clm_symb_grp_cd, 
          adbfdb_pol_form_nm, 
          risk_st_nm, 
          wrt_co_nm, 
          wrt_co_part_cd, 
          src_of_bus_cd, 
          rt_zip_dlv_ofc_cd, 
          cur_rst_rt_terr_cd 
FOR FETCH only 

只是看看如何正确地写出数据库。

谢谢

Justin

0 个答案:

没有答案