Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:56:30+00:00 2026-06-15T08:56:30+00:00

my stored procedure works fine on its own but my python script fails to

  • 0

my stored procedure works fine on its own but my python script fails to fully execute the stored procedure with my downloaded files. the purpose of the python script is to download files using ftp and store the files locally.It first compares the remote location and the local location to find new files and then download the new files to the local location. And then executes the stored procedure on each new file.

python script:

import os
import ftplib
import pyodbc

connection to sql server*
conn = pyodbc.connect(‘DRIVER={SQL Server};SERVER=localhost;DATABASE=Development;UID=myid;PWD=mypassword’)
cursor = conn.cursor()

ftp = ftplib.FTP("myftpaddress.com")
ftp.login("loginname", "password")
print 'ftp on'

#directory listing 
rfiles = ftp.nlst()
print 'remote listing'

#save local directory listing to files
lfiles = os.listdir(r"D:\Raw_Data\myFiles")
print 'local listing'

#compare and find files in rfiles but not in lfiles
nfiles = set(rfiles) - set(lfiles)
nfiles = list(nfiles)
print 'compared listings'

#loop through the new files
#download the new files and open each file and run stored proc
#close files and disconnect to sql server
for n in nfiles:
   local_filename = os.path.join(r"D:\Raw_Data\myFiles",n)
   lf = open(local_filename, "wb")
   ftp.retrbinary("RETR " + n, lf.write, 1024)
   lf.close()
   print 'file written'
  cursor.execute("exec SP_my_Dailyfiles('n')")
  conn.close()
  lf.close()
  print 'sql executed'


ftp.quit()

stored proc:

ALTER PROCEDURE [dbo].[SP_my_Dailyfiles] 
    -- Add the parameters for the stored procedure here
@file VARCHAR(255)
-- Add the parameters for the stored procedure here

AS
BEGIN


IF EXISTS(SELECT * FROM sysobjects WHERE name = 'myinvoice')
DROP TABLE dbo.myinvoice
----------------------------------------------------------------------------------------------------
CREATE TABLE myinvoice(

     [Billing] varchar(255)
    ,[Order] varchar(45)
    ,[Item] varchar(255)
    ,[Quantity in pack] varchar(255)
    ,[Invoice] varchar(255)
    ,[Date] varchar(255)
    ,[Cost] varchar(255)
    ,[Quantity of pack] varchar(255)
    ,[Extended] varchar(255)
    ,[Type] varchar(25)
    ,[Date Due] varchar(255)

)

----------------------------------------------------------------------------------------------------            
DECLARE @SourceDirectory VARCHAR(255)
DECLARE @SourceFile VARCHAR(255)



EXEC (' BULK
    INSERT dbo.myinvoice 
    FROM ''D:\Raw_Data\myfile\'+@file+'''
    WITH
    (
        FIRSTROW = 1,
        FIELDTERMINATOR = '','',
        ROWTERMINATOR = ''0x0a''
    )'
)
-------------------------------------------------------------------------------------------------------------


INSERT INTO [Development].[dbo].[my_Dailyfiles](

     [Billing]
    ,[Order] 
    ,[Item] 
    ,[Quantity in pack] 
    ,[Invoice] 
    ,[Date]  
    ,[Cost] 
    ,[Quantity of pack] 
    ,[Extended] 
    ,[Type] 
    ,[Date Due] 
    ,[FileName] 
    ,[IMPORTEDDATE] 

    )
    SELECT

         replace([Billing], '"', '') 
          ,replace([Order], '"', '') 
          ,replace([Item], '"','') 
          ,replace([Quantity in pack],'"','') 
          ,replace([Invoice],'"','') 
          ,cast(replace([Date],'"','') as varchar(255)) as date
          ,replace([Cost],'"','')
          ,replace([Quantity of pack],'"','') 
          ,replace([Extended],'"','') 
          ,replace([Type],'"','') 
          ,cast(replace([Date Due],'"','') as varchar(255)) as date
          ,@file,
          GetDate()      


 FROM [myinvoice] WHERE [Bill to] <> ' '  and ndc != '"***********"'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:56:31+00:00Added an answer on June 15, 2026 at 8:56 am

    I think the problem may be that you are closing the DB connection immediately after you execute the stored procedure, whilst still in the loop.

    This means the second time around the loop, the DB connection is closed when you try to execute the SP. I would actually expect an error to be thrown the second around the loop.

    The way I would structure this is something like:

    conn = pyodbc.connect(...)
    for n in nfiles:
        ...
        cursor = conn.cursor()
        cursor.execute("exec SP_my_Dailyfiles('n')")
        conn.commit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that works fine on its own. A recent requirement
I create the following stored procedure to calculate the percentage ,it works fine but
I'm using LINQ to SQL to access a stored procedure. This works fine on
I'm using devexpress and i have two problems. My insert stored procedure work fine.
I have a line in my SQL Stored Procedure that looks like this (works
Im having trouble with a stored procedure, I can't commit after I execute it.
I have a crystal report that gets its data from one stored procedure, this
I need help to return the 'xmlstring' (varchar(MAX)) from a Stored Procedure. It works
The stored procedure ins_address works if used stand-alone, no errors. When using the VB
I am using php to consume a mssql stored procedure, my code works with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.