0

I want to add records to the table using the xp_cmdshell command as in the example query below. Can you help me on how to do this?

INSERT INTO SDS_TBL_MF_FILE_LOCATION_CONTROL (CustomerID,CustomerName,FileName) 
SELECT '1','TestCustomerName' ,EXECUTE xp_cmdshell 'dir C:\Sales\Text\TestFolder\/b'
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    You *can't* use `xp_cmdshell` as part of a `SELECT` statement; you'll need to consume the data from `xp_cmdshell` first, perhaps into a temporary table, and then `INSERT` that data into the table. Though, honestly, I'm not a fan of using `xp_cmdshell` as a crutch like this; if you want to get details of directories in a location, use something like Powershell to consume and `INSERT` that data into SQL Server. – Thom A Aug 05 '22 at 11:08
  • Does this answer your question? [How to list files inside a folder with SQL Server](https://stackoverflow.com/questions/11559846/how-to-list-files-inside-a-folder-with-sql-server) – SMor Aug 05 '22 at 11:13
  • As you have discovered, tsql has no formal support for accessing the file system in this manner. That is a clue that you should think very differently about the goal. A securely designed system will likely not allow the database engine to access any random directory as well. – SMor Aug 05 '22 at 11:13

0 Answers0