When I executes this command, it works!
DECLARE @CMD NVARCHAR(1000)
SET @CMD = 'cd.. && "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" "SELECT TOP 1 CODE FROM[TABLE] WITH (NOLOCK) ORDER BY ID DESC" queryout "\\FOLDER\FOLDER\FILE.txt" -T -c -S "[SERVERNAME]"'
EXEC master..XP_CMDSHELL @CMD
But, if I executes that from a trigger, it hangs! And I have to restart the services
ALTER TRIGGER [TRIGGER] on [TABLE] after INSERT AS BEGIN
DECLARE @CMD NVARCHAR(1000)
SET @CMD = 'cd.. && "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" "SELECT TOP 1 CODE FROM[TABLE] WITH (NOLOCK) ORDER BY ID DESC" queryout "\\FOLDER\FOLDER\FILE.txt" -T -c -S "[SERVERNAME]"'
EXEC master..XP_CMDSHELL @CMD END
Somebody knows why??
You are most likely in deadlock trying to access the table from bcp
while processing a trigger from the same table you are trying to insert.
Think of it like this: The trigger waits for bcp, that is waiting for a lock on the table to be released, but that lock is held until after the trigger.