diff --git a/advanced-topics/write_csv_sql_server.py b/advanced-topics/write_csv_sql_server.py new file mode 100644 index 0000000..143bdf6 --- /dev/null +++ b/advanced-topics/write_csv_sql_server.py @@ -0,0 +1,22 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + + + + cursor.close() + connection.close() + +except pyodbc.Error as ex: + print() + print("exception: ",ex) + cursor.close() + connection.close() + print() + exit() + +print() diff --git a/functions/call_func.py b/functions/call_func.py new file mode 100644 index 0000000..3a10de1 --- /dev/null +++ b/functions/call_func.py @@ -0,0 +1,37 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + + sql_cmd="select dbo.ufn_get_cus_code(?) as cus_code" + + prm_id=4 + + cursor.execute(sql_cmd,prm_id) + + print("using udf to retrieve cus code...") + + while 1: + row = cursor.fetchone() + if not row: + break + print("Customer code for id ", prm_id, " - ", row.cus_code) + + print() + + + cursor.close() + connection.close() + +except pyodbc.Error as ex: + print() + print("exception: ",ex) + cursor.close() + connection.close() + print() + exit() + +print() diff --git a/functions/call_sys_func.py b/functions/call_sys_func.py new file mode 100644 index 0000000..f585e3c --- /dev/null +++ b/functions/call_sys_func.py @@ -0,0 +1,54 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + cursor.execute("select HOST_NAME() as host_name") + + print("[system function 1 - host_name results...]") + while 1: + row = cursor.fetchone() + if not row: + break + print("host name:",row.host_name) + + print() + + cursor.execute("select DB_NAME() as current_db_name") + print("[system function 2 - current database results...]") + + while 1: + row = cursor.fetchone() + if not row: + break + print("current database name:", row.current_db_name) + + print() + + # sys func 3 + prm_is_numeric = "202424" + cursor.execute("select ISNUMERIC(?) as is_numeric_value", prm_is_numeric) + + print("[System function 3 - is numeric...]") + while 1: + row = cursor.fetchone() + if not row: + break + print("checking to see if param", prm_is_numeric, "is numeric. check resutl (0 / false, 1 / true):", row.is_numeric_value) + + print() + + cursor.close() + connection.close() + +except pyodbc.Error as ex: + print() + print("exception: ",ex) + cursor.close() + connection.close() + print() + exit() + +print() diff --git a/functions/create_function.py b/functions/create_function.py new file mode 100644 index 0000000..3c82053 --- /dev/null +++ b/functions/create_function.py @@ -0,0 +1,38 @@ +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 function ufn_get_cus_code (@cus_id int) + returns varchar(50) + as + begin + declare @cus_code varchar(50) + set @cus_code = (select code from tblCustomers where id = @cus_id) + + return @cus_code + + end; + """ + + cursor.execute(sql_cmd) + connection.commit() + + print("User defined function successfully created or altered...") + + + cursor.close() + connection.close() + +except pyodbc.Error as ex: + print() + print("exception: ",ex) + cursor.close() + connection.close() + print() + exit() + +print() diff --git a/stored-procedures/call_stored_procedure_with_no_results.py b/stored-procedures/call_stored_procedure_with_no_results.py new file mode 100644 index 0000000..2ef46d1 --- /dev/null +++ b/stored-procedures/call_stored_procedure_with_no_results.py @@ -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() diff --git a/stored-procedures/call_stored_procedure_with_results.py b/stored-procedures/call_stored_procedure_with_results.py index 143bdf6..a019bae 100644 --- a/stored-procedures/call_stored_procedure_with_results.py +++ b/stored-procedures/call_stored_procedure_with_results.py @@ -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() diff --git a/stored-procedures/create_stored_procedure.py b/stored-procedures/create_stored_procedure.py index d85c3c9..d159efc 100644 --- a/stored-procedures/create_stored_procedure.py +++ b/stored-procedures/create_stored_procedure.py @@ -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()