In this video, we are going to learn about adding a computed column by using a user-defined function, also known as Custom Function. --Create a function (NON DETERMINISTIC) CREATE FUNCTION [dbo].[MyFunc] (@val int) RETURNS int AS BEGIN RETURN @val*2; END --Test the function select dbo.myFunc (200); -- should give output: 400-- Add a computed column using ALTER TABLE query ALTER TABLE jobs ADD double_salary AS dbo.myFunc(salary) Go--Get data from table SELECT * FROM jobs--Find if User Defined function is Deterministric SELECT id, Name, OBJECTPROPERTY(id, 'IsDeterministic') AS isDeterministic FROM dbo.SysObjects WHERE XType = 'FN' ORDER BY OBJECTPROPERTY(id, 'IsDeterministic') DESC, Name--Create a deterministic function WITH SCHEMABINDING CREATE FUNCTION [dbo].[MyDetFunc] (@val int) RETURNS int WITH SCHEMABINDING AS BEGIN RETURN @val*2; END -- Add a computed column with PERSISTED option ALTER TABLE jobs ADD double_salary2 AS dbo.myDetFunc(salary) persisted Go-- Again Get data from table SELECT * FROM jobs
SQL ServerUser Defined FunctionComputed Column
Creating Computed Columns With User Defined Functions
About this video
- User Defined Function
- Computed Column
- custom function
- deterministic function
- non deterministic function
Details
- Published
- Aug 10, 2018
- Technology
- SQL Server
- Publisher
- Zia Ahmed Shaikh
- Views
- 12,583
- Comments
- 1

Join the discussion and earn points toward Sharp.
Members earn 1 point for a comment, 1 point for a reaction, and 500 points for every video they publish.
1 comment
Each comment earns +1 pointJoin the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.


abi kaviPosted Aug 11, 2018, 4:52 AM
Thanks for sharing