Posts

Showing posts with the label default values

SQL Server, how to use a user function as default for a column

Image
Hi guys, Welcome this post mates. Today a super light post only to show how to use a function as a default value for a column of a SQL server Table. Simply and easy as usual! Enjoy the reading!   How to use a function as default for a column   A columns of a table in SQL Server can have a default value. Simply, if I insert a row using the insert statement and I do not specify the name of a field, in that case the default value will be used for that field. Let's see how a default is defined on a field. To add a default value you simply add the default and a value after the type definition of the field. CREATE TABLE ORDTES (ID INT IDENTITY (1,1), CODE VARCHAR (20), TOTAL_VALUE FLOAT DEFAULT 0 ) Obviously we can specify any value consistent with the type of the field. Now, if you do not specift the field total_value in the insert statement: INSERT INTO ORDTES (CODE) VALUES ( 'AAA' ) The default value will be used: Sometimes, however, our default is not a fixed value. ...