About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Monday, June 2, 2014

Reusability, create a global default value and bind it to various tables



Here in this article we will see, how we can create default value and bind it to various table fields.

So First we will be creating a User defined datatype if int basic type.

sp_addtype 'pin_code_type', int, null
Command(s) completed successfully.

Now lets create a Default and assign a default value.

create default pin_default as 326595
Command(s) completed successfully.

Now lets bind the Default value to the User defined Data type

sp_bindefault 'pin_default', 'pin_code_type'
Command(s) completed successfully.
Default bound to data type.
The new default has been bound to columns(s) of the specified user data type.

Lets create table and bound the  column to user defined data type ‘pin_code_type’

create table temp
(id int,
zip pin_code_type)
Command(s) completed successfully.

insert into temp(id) values(1)
select * from temp
id          zip
----------- -----------
1           326595
1           326595

Lets create on more Table an assign the default variable to  the table column.

create table temp2
(id int identity(1,1),
c_address varchar(10),
zip pin_code_type)

insert into temp2(c_address) values('delhi')
select * from temp2

id          c_address  zip
----------- ---------- -----------
1           delhi      326595

Conclusion: Here we can see, we created only one Default Value and bind it to more than one table. This is an example of Re-Usability.

Code makes the coder dance in the Rain; Let the life get easier, let’s welcome the coder to your life


Post Reference: Vikram Aristocratic Elfin Share

No comments:

Post a Comment