Discussion Locked

Using xp_cmdshell in MS SQL 7.0

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

The following question looks more complicated than it really is. That is if you know SQL.
Here is the config:
Winnt Server 4.0 SP6, 256MB RAM, allmost full System partition.
Here is the problem:
when I use a trigger or query analyzer to execute the master..xp_cmdshell it will not work on *.exe.
Here is what I tried:
start>run: "ping www.yahoo.com" results: OK
In query analyzer: "exec master..cmdshell 'ping www.yahoo.com'" results: OK
Start>run: "notepad.exe" results: OK
In query analyzer: "exec master..xp_cmdshell 'notepad.exe'" results: Query analyzer froze.
I created a batch file called test.bat saved it in c:\, the following was the commands in the test.bat:
@echo off
echo Im here
pause
when i ran it in dos or start workedfine ran it from query analyzer thru xp_cmdshell worked fine. However if to the above batch file i added the command to run a *.exe file it whould run in regular dos (start or command prompt) but whould not run from xp_cmdshell.
when I tested ipconfig it worked even in xp_cmdshell.

What Im trying to do is run a custom program to whenever a record is updated in tableX. That program is writen in VB. The program works fine from a dos prompt but will not work from xp_cmdshell.
If xp_cmdshell is not made to run windows programs than whats the sullotion to run a program from within SQL thru a trigger?

8 replies

by limbo

"Programs that require user input should not be executed through the extended stored procedure xp_cmdshell. xp_cmdshell does not support running MS-DOS programs that require user input.

There is no way to get the user input from any DB-Library client utility to the server. xp_cmdshell was designed to suppress the console window on the server and not allow user input. It will run internal MS-DOS commands and 32-bit programs that require user input and return back to the client utility, but there is no way to provide user input back to the program. Running external MS-DOS commands that require this input will cause the DB-Library client to wait indefinitely for this input and never return control to the client. The SQL Server process will remain active and cannot be terminated using the Kill command.
xp_cmdshell was not designed to allow user input to programs." - (MSDN INF: xp_cmdshell Does Not Retrieve User Input)

I would consider passing the trigger event back up to VB and shell notepad from there.

Using ADO, open a persistent connection in the background. Then monitor a recordset, opened as a dynamic cursor and you can trap a trigger event.

Alternatively, create a SQL Server error message, and have that raised by the trigger. Again this can be detected by the ADO connection object.

by shmaltz

Nice try but I'm sorry.
You did not bother reviewing my question. I did not ask if you can run a program that requires user input. In fact my program does NOT require user input, I only gave notepad.exe as an example.

by shmaltz

I also tried running 'explorer.exe' (which does not require user input and it didnt work.

by shmaltz

Dear Gorge2,
Now I'm really sorry. After going thru it again I did find that the problem lies in what you say. Since you did not point me to the address of the MS article it took me some time to find it.
Please repost an answer and I'll give you the points.

by limbo

Sorry about that...

Q115184 - INF: xp_cmdshell Does Not Retrieve User Input
http:// support.microsoft.com / support / kb / articles / Q115 / 1 / 84.asp

I have also tried to create a Job in SQL Server which attempts to shell an executable using 'CmdExec' without much success.

However try this...

Create a table called [watch_table] with just one field named [alert].

Now create a new VB project and add one Label [lblWatch] and one Timer [Timer1].

Add the following code:

'Add Microsoft ActiveX Data Objects 2.x to Project References
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Dim Alert As String

Private Sub Form_Load()
Dim strConnection As String

Dim Server As String
Dim UserName As String
Dim Password As String
Dim DataBase As String

' Use your DB Enivronment settings...
Server = "ABUK_NT2"
UserName = "sa"
Password = ""
DataBase = "TestDB"

strConnection = "PROVIDER=MSDASQL;driver={SQL Server};server=" _
& Server & ";uid=" & UserName & ";pwd=" & Password _
& ";database=" & DataBase & ";"

cn.Open strConnection

rs.Open "SELECT * FROM watch_table", cn, adOpenDynamic, adLockReadOnly

Set lblWatch.DataSource = rs
lblWatch.DataField = "alert"

Alert = lblWatch.Caption

Timer1.Interval = 500
End Sub

Private Sub Timer1_Timer()
rs.MoveFirst
If Alert <> lblWatch.Caption Then
Alert = lblWatch.Caption
Shell "notepad.exe", vbNormalFocus
End If
End Sub

Private Sub Form_Terminate()
rs.Close
cn.Close
End Sub

Now update the watch_table and the .EXE will launch. The resource footprint for this application isn't as small as it could be, but it does the job!

This discussion is closed to new replies.