0

I'm slightly new to python and am trying to execute a stored procedure to insert a record into a sql server with no success.The record is not being commited despite calling the cursor.commit() or even setting autocommit=True The code to the stored procedure is as follows

USE [cec_GESA]
GO
/****** Object:  StoredProcedure [dbo].[InsertCustomer]    Script Date: 6/30/2021 11:47:16 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[InsertCustomer]
    @CustName NVARCHAR(100),
    @CustAddr NVARCHAR(70),
    @Phone1 NVARCHAR(25) = '',
    @Phone2 NVARCHAR(25) = '',
    @Mobile NVARCHAR(25) = '',
    @Town NVARCHAR(20) = '',
    @Fax NVARCHAR(15) = '',
    @EmailAddress NVARCHAR(MAX) = '',
    @VATNO NVARCHAR(12) = '',
    @PIN NVARCHAR(15) = '',
    @NOTES NVARCHAR(MAX) = '',
    @CatID INT = 2,
    @CreditLimit NUMERIC(18, 2) = 0,
    @SalesRep INT = NULL,
    @Latitude FLOAT = NULL,
    @Longitude FLOAT = NULL,
    @Location NVARCHAR(50) = NULL,
    @Terms INT = 0,
    @SendUpdates NVARCHAR(25) = NULL,
    @Active BIT = 1,
    @Rating INT = 1
AS
BEGIN
    SET NOCOUNT ON;
    IF NOT EXISTS
    (
        SELECT TOP 1
               CustName
        FROM dbo.Customer
        WHERE CustName = @CustName
    )
    BEGIN
        INSERT INTO Customer
        (
            --CustID,
            CustName,
            CustAddr,
            Phone1,
            Phone2,
            Mobile,
            Town,
            Fax,
            EmailAddress,
            VATNO,
            PIN,
            NOTES,
            CatID,
            CreditLimit,
            SalesRep,
            Latitude,
            Longitude,
            Location,
            Terms,
            SendUpdates,
            Active,
            Rating
        )
        VALUES
        (
            --(SELECT ISNULL(MAX(CustID), 1) + 1 FROM Customer (NOLOCK)), 
            @CustName, @CustAddr, @Phone1, @Phone2, @Mobile, @Town, @Fax, @EmailAddress, @VATNO, @PIN, @NOTES, @CatID,
            @CreditLimit, @SalesRep, @Latitude, @Longitude, @Location, @Terms, @SendUpdates, @Active,
            @Rating);
            
        UPDATE dbo.Customer
        SET Account = dbo.CreateAccountNo(@CustName, (SELECT CustID FROM dbo.Customer WHERE CustName = @CustName))
        WHERE CustID =
        (
            SELECT CustID FROM dbo.Customer WHERE CustName = @CustName
        );

        SELECT MAX(CustID) FROM dbo.Customer;
    END;
    ELSE
    BEGIN
        SELECT 0;
    END;
END;

The fields I want passed are @CustName,@CustAddr,@Phone1,@Phone2,@Mobile,@Town,@EmailAddress ,@VATNO,@PIN,@NOTES,@CatID and @Location.

Can someone help me please on how I can write the python code to execute it. Thanks in advance

10
  • Side note, the logic in your Procedure appears to be incorrect as well. If no row exists you both INSERT and UPDATE the row; is that what you are after? Also a name is not a good unique identifier (trust me, I share my real life name with plenty of people). Finally, SELECT MAX(CustID) FROM dbo.Customer; may well not return the ID of the row you inserted; it could easily suffer from race conditions. Look into SCOPE_IDENTITY or the OUTPUT clause. Commented Jun 30, 2021 at 9:25
  • The procedure is working fine when i execute it in sql management studio,but when I execute it in python it doesn't commit Commented Jun 30, 2021 at 9:55
  • You've not given any python code here, so we have no way of telling you why your Python Code isn't working. I would suggest asking a new question, asking why your Python isn't working, not how to execute/call a Procedure. Commented Jun 30, 2021 at 9:56
  • I did ask that but didn't get a response Commented Jun 30, 2021 at 10:03
  • I don't see the question in your non-deleted ones. Commented Jun 30, 2021 at 10:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.