finish functions. starting with advanced topics csv imports
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import pyodbc
|
||||
|
||||
try:
|
||||
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes')
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
sql_cmd="""
|
||||
create or alter procedure usp_cus_details_update (@cus_id int)
|
||||
as
|
||||
update tblCustomers set lastName = lastName+' updated from sp' where id = @cus_id
|
||||
"""
|
||||
|
||||
cursor.execute(sql_cmd)
|
||||
connection.commit
|
||||
|
||||
print()
|
||||
|
||||
print("[before sp update call...]")
|
||||
cursor.execute("select * from tblCustomers")
|
||||
|
||||
while 1:
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
print(row.id, row.code, row.firstName, row.lastName)
|
||||
|
||||
|
||||
|
||||
print()
|
||||
|
||||
sql_cmd_2 ="""
|
||||
exec usp_cus_details_update @cus_id = ?;
|
||||
"""
|
||||
|
||||
prm_id = 4
|
||||
|
||||
cursor.execute(sql_cmd_2, prm_id)
|
||||
connection.commit
|
||||
|
||||
print("[After sp update call...]")
|
||||
cursor.execute("select * from tblCustomers")
|
||||
|
||||
while 1:
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
print(row.id, row.code, row.firstName, row.lastName)
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
print()
|
||||
print("exception: ",ex)
|
||||
cursor.close()
|
||||
connection.close()
|
||||
print()
|
||||
exit()
|
||||
|
||||
print()
|
||||
@@ -5,8 +5,26 @@ try:
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
sql_cmd = """
|
||||
declare @out nvarchar(max)
|
||||
exec dbo.usp_cus_details @cus_id = ?, @out = @out output;
|
||||
"""
|
||||
|
||||
prm_id=3
|
||||
|
||||
cursor.execute(sql_cmd, prm_id)
|
||||
|
||||
print("[printing sp call]")
|
||||
rows = cursor.fetchall()
|
||||
|
||||
while rows:
|
||||
print(rows)
|
||||
if cursor.nextset():
|
||||
rows = cursor.fetchall()
|
||||
else:
|
||||
rows = None
|
||||
|
||||
print()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
@@ -7,16 +7,16 @@ try:
|
||||
|
||||
|
||||
sql_cmd="""
|
||||
create procedure usp_cus_details (@cusID int, @out nvarchar(max) output)
|
||||
create or alter procedure usp_cus_details (@cus_id int, @out nvarchar(max) output)
|
||||
as
|
||||
select * from tblCustomers where id = @cusID
|
||||
select * from tblCustomers where id = @cus_id
|
||||
|
||||
"""
|
||||
|
||||
cursor.execute(sql_cmd)
|
||||
connection.commit()
|
||||
|
||||
print("stored procedure successfully created")
|
||||
print("stored procedure successfully created or altered")
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
Reference in New Issue
Block a user