Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.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
Sql server 如何通过存储过程执行修复pyodbc错误_Sql Server_Python 3.x_Stored Procedures_Pyodbc - Fatal编程技术网

Sql server 如何通过存储过程执行修复pyodbc错误

Sql server 如何通过存储过程执行修复pyodbc错误,sql-server,python-3.x,stored-procedures,pyodbc,Sql Server,Python 3.x,Stored Procedures,Pyodbc,我正在服务器上设置一个新的VM,以卸载从笔记本电脑加载的SQL server数据库。在这样做的过程中,我希望能够通过Python在数据库中执行存储过程(没有参数,只有“exec storedprocedure”),但它不起作用 当通过批处理文件和在SSMS中使用sqlcmd时,存储过程调用起作用,但我想让它完全基于python 存储过程按照以下常规格式追加事实表: --staging tbl drop and creation if object_id(stagingtbl) is not nu

我正在服务器上设置一个新的VM,以卸载从笔记本电脑加载的SQL server数据库。在这样做的过程中,我希望能够通过Python在数据库中执行存储过程(没有参数,只有“exec storedprocedure”),但它不起作用

当通过批处理文件和在SSMS中使用sqlcmd时,存储过程调用起作用,但我想让它完全基于python

存储过程按照以下常规格式追加事实表:

--staging tbl drop and creation
if object_id(stagingtbl) is not null drop tabl stagingtbl
create table stagingtbl
(fields datatypes nullable
)

--staging tbl load
bulk insert stagingtbl
from 'c:\\filepath\\filename.csv'
with (
   firstrow = 2
   , rowterminator = '\n'
   ,fieldterminator = ','
   , tablock /*don't know what tablock does but it works...*/
)

--staging table transformation
; with cte as (
    /*ETL process to transform csv file into my tbl structure*/
)

--final table load
insert final_tbl
select * from cte


/*
T-SQL update the final table's effect to date, based on subsequent effect from date. 
eg:
id, effectfromdate, effecttodate
1,1/1/19, 1/1/3000
1,1/10/19, 1/1/3000
   becomes
id, effectfromdate, effecttodate
1,1/1/19, 1/10/19
1,1/10/19, 1/1/3000
*/

存储过程在sqlcmd和ssms中运行良好,但在python(pyodbc)中执行查询“exec storedprocedure”时,我收到错误消息:

pyodbc.ProgrammingError:('42000','[42000][Microsoft][SQL Server本机客户端11.0][SQL Server]
无法大容量加载,因为无法打开文件“c:\filepath\filename.csv”。
操作系统错误代码3(系统找不到指定的路径)。(4861)(SQLExecDirectW)“”


当csv文件存在时,路径或文件名没有拼写错误,我可以在双击它时打开csv,并且没有人打开csv。

通过不断的实验,我确定问题不在于python或pyodbc。在我的笔记本电脑(db的主机)上的SSM中,存储过程工作正常,但在VM上的SSM中,存储过程会导致相同的错误。这告诉我,我的问题不是根本问题,我还有更多的挖掘工作要做。错误(在SSMS中)如下所示

Msg 4861,16级,状态1,程序附加人员,第71行[批处理起始行0]
无法大容量加载,因为无法打开文件“N:\path\filename.csv”。操作系统错误代码3(系统找不到指定的路径)

一旦我确定问题出在SSMS中,我扩大了搜索范围,发现问题是
批量插入
命令的路径必须与托管数据库的机器相关。因此,在VM(客户端机器,直到我迁移db)中,当我使用路径
c:\
时,认为这是VM的
c:\
驱动器,存储过程正在查看笔记本电脑的
c:\
,因为它是主机。通过这一点,我还了解到,在共享驱动器(
N:\
)上,访问是委派的,这是它自己的问题()


因此,我将首先关注迁移数据库,然后这将解决我的问题。感谢所有试图提供帮助的人

我无法重现您的问题。使用
sqlcmd
或SSMS进行连接时,您可能没有连接到Python脚本正在连接的同一个SQL Server实例。双击CSV文件时,是否双击服务器上共享文件夹中可从笔记本中看到的文件?您好,.CSV文件是否存在于指定位置?@GordThompson它是一个sqlexpress sql server实例,使用pyodbc游标,我可以运行
select*fromtbl
查询,但存储过程不会运行run@AmilaMGunawardanacsv文件确实存在于共享驱动器上,并且VM能够在NotePad中打开它。您这样做时csv文件是否打开?