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 2000 Forums
 Transact-SQL (2000)
 Entering a default value indesign table

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-02-12 : 09:16:23
Nancy writes "I have a SQL 2000 DB with a table named employment that has a these fields among others:

PersonID (int, 4, no nulls)
Picture_path (varchar, 50,allow nulls)

Each time a new record is added to this table I want the picture_path field to be populated with the following data:

PersonID.jpg

So if the new record has 12345 in the PersonID field, I want the picture_path field to read 12345.jpg

What can I enter in the Default Value field in table design to make this happen?

Is there a better way to do this?"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-02-12 : 09:52:24
You have to do this using a trigger, a default cannot reference another column's value:

CREATE TRIGGER DefaultPicturePath ON employment FOR INSERT AS
UPDATE E SET E.picture_path=Ltrim(Str(E.PersonID, 8, 0)) + '.jpg'
FROM employment E INNER JOIN inserted I ON (E.PersonID=I.PersonID)
WHERE E.picture_path IS NULL


After the row(s) are inserted, this trigger will UPDATE any Null picture_paths with the expression you want.

Edited by - robvolk on 02/12/2002 09:53:02
Go to Top of Page
   

- Advertisement -