December 22, 2021 at 3:27 am
I'm trying to do what should be simple, that is send the output of this list of python packages out to a SQL temp table. Have spent hours trying to do equivalent of a "select into" etc but still no joy. Any help appreciated. Its SQL 2017
This is the working code:
EXECUTE sp_execute_external_script
@language = N'Python',
@script = N'
import pkg_resources
import pandas as pd
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])
df = pd.DataFrame(installed_packages_list)
OutputDataSet = df
'
WITH RESULT SETS (( PackageVersion nvarchar (150) ))
December 22, 2021 at 3:17 pm
I have not tested this, but I am 99.99999% confident you could create a temp table (CREATE TABLE #tmpTable(col1 datatype1, col2 datatype2, ...)) with the columns you expect then do an "INSERT INTO #tmpTable EXECUTE sp_execute_external_script ..." and it will put your output into the temp table like you are wanting. You would need to create the temp table first, but it shouldn't be that hard to do as you can run your script to get a list of all of the columns, then create the table and do your insert.
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply