A. Return a list of executable files
This example shows the xp_cmdshell extended stored procedure executing a directory command.
EXEC master..xp_cmdshell 'dir *.exe'
B. Use Windows NT net commands
This example shows the use of xp_cmdshell in a stored procedure. This example notifies users (with net send) that SQL Server is about to be shut down, pauses the server (with net pause), and then shuts the server down (with net stop).
CREATE PROC shutdown10
AS
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 10 minutes. No more connections allowed.', no_output
EXEC xp_cmdshell 'net pause sqlserver'
WAITFOR DELAY '00:05:00'
EXEC xp_cmdshell 'net send /domain: SQL_USERS ''SQL Server shutting down
in 5 minutes.', no_output
WAITFOR DELAY '00:04:00'
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 1 minute. Log off now.', no_output
WAITFOR DELAY '00:01:00'
EXEC xp_cmdshell 'net stop sqlserver', no_output
C. Return no output
This example uses xp_cmdshell to execute a command string without returning the output to the client.
USE master
EXEC xp_cmdshell 'copy c:\sqldumps\pubs.dmp \\server2\backups\sqldumps',
NO_OUTPUT
D. Use return status
In this example, the xp_cmdshell extended stored procedure also suggests return status. The return code value is stored in the variable @result.
DECLARE @result int
EXEC @result = xp_cmdshell 'dir *.exe'
IF (@result = 0)
PRINT 'Success'
ELSE
PRINT 'Failure'
E. Write variable contents out to file
This example writes the contents of the current directory to a file named dir_out.txt in the current server directory.
相当与推和拉
楼主希望是数据库推到客户端,而不是客户端去拉数据
数据库不同,结果也不同,而且数据库只支持当前可能的应用程序,它不能判断你安装了什么专用的应用程序,当然也不能发消息给你,但它可以发给数据库管理系统或者邮件等通知你,你的应用程序再检测数据库管理系统或者邮件等接收这个消息,
比如:
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminder' AND type = 'TR')
DROP TRIGGER reminder
GO
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE
AS RAISERROR (50009, 16, 10)
GO
这个触发器会在sysmessages表中插入消息,参见sp_addmessage这个存储过程
你可以用各种程序,比如form中的timer、windows service中socket等检测在sysmessages是否有相关的消息发生。
trigger还有其他的方式,比如email等,你可以看MSSQL中的帮助
B. Use a trigger with a reminder e-mail message
This example sends an e-mail message to a specified person (MaryM) when the titles table changes.
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminder' AND type = 'TR')
DROP TRIGGER reminder
GO
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE, DELETE
AS
EXEC master..xp_sendmail 'MaryM',
'Don''t forget to print a report for the distributors.'
GO