Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use a column name in the DEFAULT field?

Status
Not open for further replies.

sgursahaney

Programmer
Jun 11, 2001
57
0
0
US
I would like to define a default value for a column using a computation of the value from another column. However, SQL Server complains when I specify another column in the default clause. For example, I would like to use the following formula as the default for column EXT:

SUBSTRING(CUST_PHONE,9,4)

where CUST_PHONE is another column in the table that has a phone number in it.

Does anybody know how to do this?

Thanks in advance.
 
You cannot set the default value equal to another column. You can create a computed column if you are using SQL Server 7 or 2000. The computed column is not physically stored and is resolved each time it is selected.

CREATE TABLE (..., CUST_PHONE varchar(18), CUST_EXT As SUBSTRING(CUST_PHONE,9,4), ...)

Another method available for setting a column based on another column is TRIGGERS. You could create an INSERT and UPDATE TRIGGER on the table that would update the extension column when the phone column is inserted or updated. Terry

"I shall try to correct errors when shown to be errors, and I shall adopt new views so fast as they shall appear to be true views." - Abraham Lincoln
 
Thanks. I considered the trigger approach, but I was concerned that my INSERT would fail because the EXT column is setup not to accept nulls. Isn't the trigger executed after the INSERT is performed?
 
Why not just insert the extension, when you insert the phone?

INSERT tbl (col1, col2, phone, ext, colA, ...)
VALUES (val1, val2, '000000001234', substring('000000001234',9,4), valA, ...)

NOTE: Generally, derived or calculated values are not stored in a relational database as that violates normalization rules. Sometimes, databases are denormalized for specific purposes, generally realted to performance. In your case, would it make sense to derive the extension when querying the database rather than storing the extension physically on the table? Terry

"The greatest obstacle to discovery is not ignorance -- it is the illusion of knowledge." - Daniel J Boorstin
 
Actually the field contains a rather complex calculation that uses the last 4 digits of the phone number field. I just simplified it for the purpose of this posting. My problem is that other people inserting records into the table are populating the field incorrectly. I wanted to find a way where I could populate the field automatically when records are inserting. Thanks.
 
Just a note on selecting the last values in a string.

In Oracle SQL, the substr function allows the use of negative numbers, thus
substr(string, -4);
would return the last 4 chars in the string.
 
phil1976,

I appreciate your willingness to participate but want to note that this thread has been inactive for nine months. In addition, this is a MS SQL Server forum and the Oracle statement is without value. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Actually, I am still monitoring this thread in hopes that a solution is forthcoming. I appreciate the contrasts to Oracle but I still need a solution for SQL Server.

Regards,
Suresh
 
sgursahaney,

Consider this when thinking about Triggers.

1) If you have SQL 2000, you can use Instead of Triggers which fire before the insert or update.

2) You could set up a default value on the column so inserts will not fail. Then the trigger could fire after the update and perform the complex calculation.

3) You could insert a default value and then update in a trigger. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
This is a very simplified example of something we do for some of our tables - I have removed the stuff for deriving the value as it is not really relevant since your own formula will be what you use. But it might get you started.

Code:
CREATE TRIGGER [triggerName]ON dbo.tblSomeTable 
INSTEAD OF INSERT
NOT FOR REPLICATION
AS

	DECLARE
	@SomeInt INTEGER,
	@SomeVal NVARCHAR(40)


	SELECT @SomeVal = SomeVal FROM Inserted 
	
	SELECT @SomeInt = <Derive your value here>

	INSERT tblSomeTable (SomeVal,SomeInt) 
	VALUES (@SomeVal, @SomeInt)


Good Luck :)
Crystal
crystalized_s@yahoo.com

--------------------------------------------------

Experience is one thing you can't get for nothing.

-Oscar Wilde

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top