1

I have come across a situation where functions are very helpful. I can create it using its syntax and then I would use them in select or procedures.

But recently I have found that a function is called automatically when a row is inserted or updated. Also it is accepting two parameters. This user-defined scalar function is neither called in C# through ORM and moreover there is no stored procedure using this function, yet it is being called and as per my assumption, this setting must be in a table itself. It is triggered automatically when a row is updated or inserted.

Can somebody please throw some light on this as I have never encountered it before. An example of it would really be helpful that how it is created and how we can call this function.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sweetie
  • 1,298
  • 6
  • 24
  • 48
  • 2
    It could be a default value on a column of the table you are inserting into. It could also be on a trigger. – Vincent Apr 27 '16 at 05:07
  • Could you please add some reference or example of something similar. It would be very helpful for me. – Sweetie Apr 27 '16 at 05:09
  • 2
    It might be called from the trigger. The below link might be useful. - https://msdn.microsoft.com/en-IN/library/ms189799.aspx – Manish Apr 27 '16 at 05:15
  • I have tried googling and found the below url for the same. They have shown how to create a function as default value but how we can call this function as this function accepts two parameters. http://stackoverflow.com/questions/442503/bind-a-column-default-value-to-a-function-in-sql-2005 – Sweetie Apr 27 '16 at 05:16
  • 1
    CREATE TABLE [dbo].[DEFAULTS_SAMPLE]( [ID] [int] NOT NULL, [GUID] [uniqueidentifier] NOT NULL, [CREATED] [datetime] NOT NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[DEFAULTS_SAMPLE] ADD CONSTRAINT [DF_DEFAULTS_SAMPLE_GUID] DEFAULT (newid()) FOR [GUID] GO ALTER TABLE [dbo].[DEFAULTS_SAMPLE] ADD CONSTRAINT [DF_DEFAULTS_SAMPLE_CREATED] DEFAULT (sysdatetime()) FOR [CREATED] GO – Vincent Apr 27 '16 at 05:16
  • 1
    @Vincent: please **do not** put code samples or sample data into comments - since you cannot format it, it's **extremely hard** to read it.... Thank you. – marc_s Apr 27 '16 at 05:40

1 Answers1

2

You can install SQL RedGate Search (it's free) which allows you to search all SQL objects to find where your functions has been used:

http://www.red-gate.com/products/sql-development/sql-search/

enter image description here

Also possible is that you make a trace on that function to see where it has been called from (or at least by who):

How to trace T-SQL function calls

Community
  • 1
  • 1
Reboon
  • 578
  • 2
  • 5
  • 12