首页 > 数据库技术 > 详细

SQLserver中的xp_cmdshell

时间:2015-12-09 11:25:11      阅读:656      评论:0      收藏:0      [点我收藏+]

shell是用户与操作系统对话的一个接口,通过shell告诉操作系统让系统执行我们的指令

xp_cmdshell在sqlserver中默认是关闭的存在安全隐患。

--打开xp_cmdshell
 EXEC sp_configure show advanced options, 1;RECONFIGURE;EXEC sp_configure xp_cmdshell, 1;RECONFIGURE;
--关闭xp_cmdshell
EXEC sp_configure show advanced options, 1;RECONFIGURE;EXEC sp_configure xp_cmdshell, 0;RECONFIGURE;

以下是跟xp_cmdshell有关的小例子。

1.显示C盘下的内容,这个比较简单

 exec xp_cmdshell dir c:\  

 

执行结果如下:

技术分享

2.将变量写入文本文件

DECLARE @cmd sysname, @var sysname
SET @var = Today:2015-12-09
SET @cmd = echo  + @var +  > c:\Today.txt
EXEC master..xp_cmdshell @cmd

执行上述语句之后,你会在c盘下看到Today这个文件

技术分享

--在查询分析器上执行(EXEC master..xp_cmdshell)
EXEC master..xp_cmdshell bcp "select * from 数据库名.dbo.表名" queryout c:\currency.txt -S 数据库实例 -U"用户" -P"密码" -c

--把SQL语句生成一个.sql文件,然后调用
--注:路径的文件夹名称中间不能有空格
exec master..xp_cmdshell osql -S 数据库实例 -U 用户 -P 密码 -i    C:\cmdshellTest.sql  

--将数据导入到currency表中
EXEC master..xp_cmdshell bcp 数据库名.dbo.表名 in c:\currency.txt -c -T
--导入数据也同样可以使用-F和-L选项来选择导入数据的记录行。
EXEC master..xp_cmdshell bcp 数据库名.dbo.表名 in c:\currency.txt -c -F 10 -L 13 -T

 

SQLserver中的xp_cmdshell

原文:http://www.cnblogs.com/OliverQin/p/5032014.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!