How to set the default value of an attribute to the value of another attribute in another table using Visual Database Tools?

Recommended Answers

All 7 Replies

What is an attribute in MSSQL? Are you sure you're asking in the right forum?

What is an attribute in MSSQL? Are you sure you're asking in the right forum?

i have two tables

table A:
column B: some data

table C:
column D: default value taken from table A - column B

any suggestions please

Use a trigger to default the value if its null when inserting. That will allow you to use a custom query to fetch the values

thanks sknake
but could u help me out with this query

/*
table A:
column B: some data

table C:
column D: default value taken from table A - column B
*/

IF OBJECT_ID('TableA', 'U') IS NOT NULL DROP TABLE TableA
Create Table TableA
(
  ColumnB varchar(50)
)

GO

Insert Into TableA (ColumnB) Values ('DefaultValue')

GO

IF OBJECT_ID('TableC', 'U') IS NOT NULL DROP TABLE TableC
Create Table TableC
(
  RecordId int identity(1000, 1) PRIMARY KEY NONCLUSTERED,
  ColumnJunk varchar(30),
  ColumnD varchar(50)
)

GO

IF OBJECT_ID ('TableC.DefaultValue', 'TR') IS NOT NULL
   DROP TRIGGER TableC.DefaultValue;
GO

CREATE TRIGGER DefaultValue
ON TableC
INSTEAD OF INSERT
AS 
BEGIN
  Insert Into TableC (ColumnJunk, ColumnD)
  Select i.ColumnJunk,
  (
    Select Top 1 ColumnB
    From TableA
  )
  From Inserted i
END

GO

Insert Into TableC (ColumnJunk) Values ('abc')

GO

Select *
From TableC

thanks a lot sknake :)

You're welcome

Please mark this thread as solved if you have found the answer to your original question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.