วันพฤหัสบดีที่ 2 ธันวาคม พ.ศ. 2553

[SQL] การตั้งให้ run commandshell ผ่าน store procedure ได้

เช่น เราต้องการให้ Store Procedure ไปเรียก bat file ใดๆบนระบบ เพื่อกระทำการอะไรสักอย่าง
ถ้าไม่ไปอนุญาติก่อน จะไม่สามารถสั่งคำสั่งได้

เงื่อนไข
bat , exe ต้องอยู่บน server ที่รัน database นั้นไว้อยู่เท่านั้น

-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

รันแล้ว จะอยู่ในสถานะ enable ให้รัน command shell ได้แล้ว

command สำหรับรัน command shell

DECLARE
@CMD AS VARCHAR(500)
SET @CMD = '"c:\execute.bat"'
EXEC master..xp_cmdshell @CMD

โดย @CMD คือ พาธของไฟล์ที่จะรัน เช่น c:\execute.bat

ไม่มีความคิดเห็น:

แสดงความคิดเห็น