简体   繁体   中英

How can I run an exe file in SQL Server management studio

I've created a python script and saw the only way I can try to run it within an SQL Query is if I made turned it into a .exe file and try to run in within the SQL Query.

I have tried the following. But no luck and I can not find a good step by step guide on how to achieve this.

EXEC xp_cmdshell '"C:\Users\USER\Desktop\pythonexe\dist\dbtest.exe"'

Result from ssms

 1 Access is denied 2 NULL 

I've done all the necessary set ups needed for the xp_cmdshell and when I run the exe file by simply double clicking on it it works fine.

I suspect this has something to do with users and permissions. But I can not find a good guide on how to set this up.

What my python script is doing is taking data from a query and parsing this data into a text messaging gateway API. When I run this in Python it works fine. What I need to do now is have this run in sequel server management studio as part of a stored procedure. This will help in automating the process of sending text messages from the data retrieved. This is the python script

cursor = conn.cursor() cursor.execute('SELECT top 1 Phone,Message FROM sms1 WHERE LEN(Phone)=13')

for row in cursor:

    username = "sandbox"
    apikey = "bf6c41a2be6"

    africastalking.initialize(username, apikey)

    sms = africastalking.SMS

    number = [row[0]]

    recipients = number

    message = row[1]
    print(number, message)

    sender = "MegaLtd"

    try:
        response = sms.send(message, recipients, sender)
        print(response)
    except Exception as e:
        print(f"Houston, we have a problem {e}")

When I try this in ssms I get a syntax error. How can I run the above python script in SQL. I have looked into the process of sp_execute_external_script and I am running SSQL Server 2017 and have installed python and r respectively.

If you are connected to the server, then it doesn't have access to your local machine. You can try putting the .exe on your server, or swap over to running a local connection so it does have access.

Generally it's insufficient File system permissions. Check and verify that user account that runs SQL Server (eg LocalSystem or NetworkService) has permission (Read+Execute) to the executable files.

Go to -> Services.msc and check your SQL server is running with current User / Administrator user authentication which has permission of accessing your file system.

xp_cmdshell requires permission to access "C:\\WINDOWS\\system32" check if it is accessible.

Also put exe file into inner folder of C: or D: ie "D:\\dist\\dbtest.exe"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM