Trigger UPDATE write TXT file

3

How can I create a trigger that fires every time I have a UPDATE in x column, get the new value to create a new txt file and insert into it. (for synchronization purposes).

For example:

produtos

Layout do txt:
55 | nome_produto | quantidadeEstoque | peso

In update events, it takes some values and inserts this layout into a new txt.

Up to the trigger part and etc. I've created, though, I do not know how to work with TXT manipulation.

    
asked by anonymous 24.04.2014 / 16:21

1 answer

3

The best-known method is to create a common trigger and use the master..xp_cmdshell to run any commands in the operating system environment that, in turn, write the data to the file and location.

Here are some methods to create text file :

BCP

It is a SQL tool that executes a query and writes to a text file. Example:

master..xp_cmdshell 'bcp banco..tabela out c:\arquivo.bcp -S -U -P -c '

Output Redirection to a File

It is basically using commands from the opperational system to generate the file. Example:

exec master..xp_cmdshell 'echo meu-texto-aqui > c:\arquivo.txt'

Note that you can call any program and pass some value using a variable, for example.

Considerations

Using a trigger to write to a file is a bad idea , since you will be directly affecting the performance of the database and hence the system, outside the response time to the user. / p>

A workaround is to write the data to an auxiliary table or use a control flag to then export the new data to a periodically executed task, for example, every N minutes.

    
24.04.2014 / 16:40