2012-08-24 2 views
0

임시 테이블이 있는지 확인하려고하고 있는데 그렇다면 pyodbc를 사용하여 SQL Server에 삭제하십시오. 쿼리 관리 스튜디오에서 작동하지만, 파이썬에서 나는 이전 SQL 쿼리pyobdc를 사용하여 SQL Server에서 임시 테이블 삭제

cnxn = pyodbc.connect('APP=MyApp;DRIVER={SQL Server Native Client 10.0};SERVER=powerapp6-WRON;DATABASE=ICP;DSN=myserver;Trusted_Connection=yes') 
cursor = cnxn.cursor() 
cnxn.commit() 

for mc in range(0,3,1): 
    speardata = (""" if object_id('tempdb..#tempCSID') is not null 
    begin 
     drop table #tempCSID 
    end 
    create table #tempCSID (csid int) 
    insert into #tempCSID (csid) 
    select top 500 [CSID] 
    from [ICP].[dbo].[CSID] 
    order by newid() 
    SELECT [ICP].[dbo].[CSunit_L].[CSID] ,[Nitrogen] ,[Incorp0] ,[Incorp200] ,[yield] ,[xdays] ,[accum_rain] ,[avg_maxt] 
      ,[avg_mint] ,[accum_radn] ,[xgdays] ,[accum_g_rain] ,[avg_g_maxt] ,[avg_g_mint] ,[accum_g_radn] ,[effrain_g] ,[xshdays] 
      ,[accum_sh_rain] ,[avg_sh_maxt] ,[avg_sh_mint] ,[accum_sh_radn] ,[effrain_sh] 
    FROM [ICP].[dbo].[CSunit_L] 
    inner join #tempCSID 
    on [ICP].[dbo].[CSunit_L].[CSID] = #tempCSID.csid and [ICP].[dbo].[CSunit_L].[Nitrogen] < 201 
    """) 
    ndata = np.array(cursor.execute(speardata).fetchall()) 

가의 cnxn 설정을 할 수있는 뭔가가 있다면 궁금하지 않았다

오류를 얻을 tempdb의 처음 두 줄은 ICP 데이터베이스와 다른 위치에 있습니까?

답변

1

오류 pyodbc.ProgrammingError: No results. Previous SQL was not a query은 SQL 구문 오류를 나타냅니다. 이 경우 라인 종결 자 (\n)로만 구분되는 여러 DDL 및 DML 쿼리가 있습니다.

귀하의 경우에는 전화로 각 진술을 나눠서 임시 테이블 삽입 후 connection.commit()을 보장하는 것이 가장 좋습니다. 그런 다음 select을 실행하여 이전에 시도한 것처럼 배열을 채 웁니다.

+0

업데이트 쿼리를 실행하려고하면 connection.commit()이 (가) 누락되었습니다. – HelloW