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'
3
  • 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. Commented Aug 5, 2022 at 11:08
  • Does this answer your question? How to list files inside a folder with SQL Server Commented Aug 5, 2022 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. Commented Aug 5, 2022 at 11:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.