python: pymssql stored procedures insert output

sql script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Insert_BookKindOut')
DROP PROCEDURE proc_Insert_BookKindOut
GO
CREATE PROCEDURE proc_Insert_InsuranceMoneyOut
(
    @InsuranceName NVarChar(1000),
    @InsuranceCost float,
    @IMonth Int,
    @ID Int output
)
AS
Begin
INSERT INTO InsuranceMoney
(
    [InsuranceName] ,
    [InsuranceCost],
    [IMonth]
)
    VALUES
(
    @InsuranceName,
    @InsuranceCost,
    @IMonth
)
SELECT @ID=@@IDENTITY
END
GO

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def insertOutProc(self,iobject):
    """
    插入操作 返回值 存储过程 https://github.com/pymssql/pymssql/issues/441
    python pymssql stored procedures insert output geovindu
    param:iobject 输入保险类
    :return:
    """
 
    conn = pymssql.connect(
        server=self._strserver,
        user=self._struser,
        password=self._strpwd,
        database=self._strdatabase
    )
    cursor = conn.cursor()
    #cursor.callproc("")
    outid=pymssql.output(int)
    args=(iobject.getInsuranceName(), iobject.getInsuranceCost(), iobject.getIMonth(), outid)  #pymssql.output
    #cursor.execute(f"exec 存储过程名称 @参数1='xxx',@参数2='xxx',@参数3='xxx',@参数4='xxx'")
    newout=cursor.callproc("dbo.proc_Insert_InsuranceMoneyOut",args)
    print(newout[3])  #  ('医疗', 900, 3, 221)
    conn.commit()
    conn.close()
    return newout[3]

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sql = """INSERT INTO [database].[file name].[table name] ([column1],[column2],[column3],[column4]...)
                                  VALUES({value1},{value2},'{value3}',{value4}...);
                                                """.format(
    value1=Parameter 1,
    value2=Parameter 2,
    value3=Parameter 3,
    value4=Parameter 3,
    ...)
cursor.execute(sql)
conn.commit()
conn.close()
 
import pymssql
"""Configuration"""
server='xxx'
user ='xxx'
password='xxx'
database='xxx'
"""Connect to the database"""
conn = pymssql.connect(server, user, password, database)
cursor = conn.cursor()
cursor.execute(f"exec stored procedure name @Parameter1='xxx',@Parameter2='xxx',@Parameter3='xxx',@Parameter4='xxx'")
result = cursor.fetchall()  #Get the result set
for i in result:
  print(i)  #Traverse and print the data of the query result set

  

posted @   ®Geovin Du Dream Park™  阅读(65)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-06-17 csharp:Conversion Between DataTable and List
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示