A Quick Start to Running Python Code in SQL Server

  • Comments posted to this topic are about the item A Quick Start to Running Python Code in SQL Server

  • Hi Emmitt,

    I'm having a little trouble getting the following to run. I suspect the table Result1 table should already exist for it to insert the output generated.

    Any hints on the table creation requirement here?

    DECLARE @InputQuery NVARCHAR(2000) = N'

    SELECT n from (values (1), (2), (3)) a(n)

    '

    INSERT dbo.Result1

    EXEC sp_execute_external_script

    @language =N'Python',

    @script=N'OutputDataSet = InputDataSet',

    @input_data_1 = @InputQuery

     

  • I believe this is standard SQL from reading the article. This is INSERT..EXEC, so the table would need to exist.

  • Hi Emmitt

    Great article. I have a question regarding the first script. Why are the numbers in the outcome

    1

    3

    5

    9

    7

    appearing in that order? Shouldn't 7 come before 9? I find that quite weird.

    Best

    Stelios

  • There is no ordering guarantee for the numbers when they are in a set

    https://stackoverflow.com/questions/9792664/converting-a-list-to-a-set-changes-element-order

  • Good article, thank you...

    Note: In the one example you're missing the column name;

    DECLARE @InputQuery NVARCHAR(2000) = N'
    SELECT n, o
    FROM (values (1,10), (2,20), (3,30)) a(n, o)
    '
    EXEC sp_execute_external_script
    @language =N'Python',
    @script=N'OutputDataSet = InputDataSet',
    @input_data_1 = @InputQuery
    WITH RESULT SETS
    ( ( [MissingColumnName] INT NOT NULL,
    [iplusTen] INT NULL)
    )

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply