Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Write String Default Value Function

Author  Topic 

ljp099
Yak Posting Veteran

79 Posts

Posted - 2008-06-03 : 16:31:57
I have a datetime column and I set the Default value of the column to: getDate().

How can I set a nvarchar columns Default value to write string data (such as: "test")?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-06-03 : 16:46:31
Same way:

create table #t (
i int
,d datetime default(getdate())
,nv nvarchar(20) default(N'test')
)
go
insert #t (i) values (1)
select * from #t
go
drop table #t


Be One with the Optimizer
TG
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2008-06-03 : 16:47:14
you would achieve this the same way:

ALTER TABLE dbo.YourTable
ADD CONSTRAINT DF_YourTable_YourColumn DEFAULT N'yak' FOR YourColumn



Nathan Skerl
Go to Top of Page

ljp099
Yak Posting Veteran

79 Posts

Posted - 2008-06-03 : 16:54:28
Should the default value be set every time a record is modified?

So if I set:

last_updated_by nvarchar(20) default(N'test')


any time a record is modified, shouldnt the value of the last_updated_by column be set to 'test'?

That is what I thought would happen, but Im updating a db record and the value currently set in the last_updated_by field is not getting reset to 'test'.

Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2008-06-03 : 17:02:47
not modified, inserted

Nathan Skerl
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-04 : 00:05:20
quote:
Originally posted by ljp099

Should the default value be set every time a record is modified?

So if I set:

last_updated_by nvarchar(20) default(N'test')


any time a record is modified, shouldnt the value of the last_updated_by column be set to 'test'?

That is what I thought would happen, but Im updating a db record and the value currently set in the last_updated_by field is not getting reset to 'test'.



For making that happen, You would need an after update trigger on your table.
Go to Top of Page
   

- Advertisement -