Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using trigger instead of defaults?
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01014238
Message ID:
01014324
Views:
24
You're welcome.

>BIG thanks Sergey! You've made it too easy for me. It used to be Michael Levy who used to answer all my SQL-related questions but you've taken over his post. Thanks again.
>
>>Hi John,
>>
>>You can use only constants or system functions in DEFAULTs. A trigger can accomplish what you want. Notice that table has to have a primary key in order to implement trigger.
CREATE TRIGGER trig_test
>>ON dbo.mytable
>>FOR INSERT
>>AS
>>BEGIN
>>        -- id is a PK for mytable
>>	UPDATE mytable SET field2 =
>>		(CASE WHEN inserted.field1 LIKE 'D%' THEN 'M' ELSE 'W' END)
>>		FROM mytable JOIN inserted
>>			ON inserted.id = mytable.id
>>		WHERE inserted.field2 IS NULL	
>>END
>>
>>
>>>I want to create a default value for a column when new records are added but the default value depends on value of another column and I don't know how this can be best done.
>>>
>>>For example, if I have a very simple table with just two character columns of FIELD1 & FIELD2, I want the default value of FIELD2 to be:
>>>
>>>1) If value of FIELD1='MOMMY' then default for FIELD2='WOMEN'
>>>2) If vlaue of FIELD2='DADDY' then default for FIELD2='MEN'
>>>
>>>How can I go about doing this using either DEFAULT or TRIGGER? TIA.
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform