Hello,
Generally we need to set default value to particular column in Database Table like
isActive is
true, IsDeleted is
false.
same way many times we set
CreatedOn Date as CurrentDate and that is by query of insert
GetDate().
Static Values :
Its very easy to set some static value as Default value for column and that is as follow :
Functions :
Set Default Date for
Createdon Field as Follow :
User Defined Functions :
Set User Defined function as Default Value as follow:
In my case I am supposed to get Random Hexa Code for Color. so I Have created an User Defined function for that
CREATE FUNCTION [dbo].[GetRandomColorCode]
(
)
RETURNS varchar(6)
AS
BEGIN
Declare @Color varchar(6)
RETURN (SELECT MyNewID FROM dbo.MyNewID)
END
Now set the function in columns as follow :
If this do not allow us to set Default value try following query to set
ALTER TABLE dbo.Table_1 ADD CONSTRAINT
DF_Table_1_ColorCode DEFAULT ([dbo].[GetRandomColorCode]()) FOR ColorCode
GO
All the best
Happy Coding..