From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Create a Python stored procedure

Create a Python stored procedure - SQL Server Tutorial

From the course: Complete Guide to Advanced SQL Server

Create a Python stored procedure

Once you've written a Python script that you're happy with, it's fairly simple to save the script as a stored procedure on the database server. This is particularly useful for scripts that you'd like to run over and over again, or scripts that you'd like to give other database users access to without them having to know how to write and execute Python code. I've gone ahead and written a little script that generates 10 random numbers. Let's walk through it really quickly. First, I need to make use of the random module, which isn't available by default. So the first line here simply loads the module into memory so that I can use its functions. Next, I set up a variable called MyNumbers and assign it an empty list container. After that, the script starts a for loop that'll run through the following line 10 times. The loop will take the MyNumber list and append a random integer between 1 and 101. Remember that these are not inclusive, so the values that are added to the list will be…

Contents