0

Is it possible to call a function on every insert or update to MSSQL database? Specifically, I have to remove all extra spaces (regex: " {2,}") from every string inserted into the database. The project is already too big, it's impossible to change this behavior everywhere.

If it's not achievable using the database configuration, can it be done with Entity Framework?

Phronux
  • 125
  • 1
  • 9
  • I have removed the answer but you can check this post and you can search what is the beset for your use case: http://stackoverflow.com/questions/12452488/sqldependency-reliablity – Bassam Alugili Jun 03 '16 at 10:49
  • As discussed with Bassam under their now-removed answer, SqlDependency is _not_ the way to go for this requirement. In the first place because you'll execute the logic after the fact, so you'll have a (hopefully brief) moment of time where the data in your database is "dirty" (i.e. not trimmed). Of course you'd better just fix this at the application side, i.e. before the data even enters the database. – CodeCaster Jun 03 '16 at 13:32
  • @CodeCaster I understand that triggers are to be done on _every_ table to work properly. Is there any way to make it globally? – Phronux Jun 10 '16 at 09:27
  • @Phronux I'm sure that question's already been asked and answered. You're looking for a quick fix for a big problem, and I'm afraid there is none. Cleaning user input should be the first concern to address when working with user input... – CodeCaster Jun 10 '16 at 09:28
  • @CodeCaster that thing has just changed throughout the writing process and I had little to none possibilities to be a part of planning that. The problem is not that big actually, I assume it's more of a helpful feature. – Phronux Jun 10 '16 at 10:15

1 Answers1

0

Yes, it is possible. They are called Triggers. So search for "Triggers on MSSQL". You dont have to change your code, you only have to edit your database.

atakanyenel
  • 1,367
  • 1
  • 15
  • 20
  • 1
    That's a great answer, I've never heard of them! I'm still new to databases and I didn't know what to look for. Thanks! – Phronux Jun 03 '16 at 10:24
  • @Phronux after your research , if Triggers solve your problem you can accept this as the accepted answer so next time if someone searches the same topic , they will see the solution – atakanyenel Jun 03 '16 at 10:30