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
 General SQL Server Forums
 New to SQL Server Programming
 How to Update Data from One table to Another?

Author  Topic 

daipayan
Posting Yak Master

181 Posts

Posted - 2009-02-25 : 15:20:29
Hi All, I need your help...please help me!

I have 4 tables:
create table dbo.Course(
CourseID smallint identity not null,
C_Name varchar(10) not null,
C_Batch varchar(10) not null,
C_Term varchar(10) not null,
C_Specialisation varchar(10) not null,
constraint PK_CourseID primary key(
CourseID
)
);

create table dbo.StudentSpecialisation(
SS_ID smallint identity not null,
Student_Name varchar(100) not null,
SS_Batch varchar(10) not null,
SS_Term varchar(10) not null,
SS_Specialisation1 varchar(10) not null,
SS_Specialisation2 varchar(10) not null,
constraint PK_SS_ID primary key(
SS_ID
)
);


create table dbo.AssignMarks(
AM_ID smallint identity not null,
CourseID smallint not null,
SS_MarksType varchar(10) null,
SS_Marks bigint null,
constraint PK_AM_ID primary key(
AM_ID
)
);


create table dbo.StudentMarks(
SM_ID smallint identity not null,
Student_Name varchar(100) not null,
CourseID smallint not null,
SS_MarksType varchar(10) null,
SS_Mark bigint null,
constraint PK_SS_ID primary key(
SM_ID
)
);


Now, let me brief you about these tables:

  • Course: Here, I will include details of the courses.

  • StudentSpecialisation: Here, I will insert details of a student specialisation, a student have to opt 2 specialisation, thats why i had given 2 columns specialisation_1 & specialisation_2

  • AssignMarks: here faculty can decide the divison of the marks of his/her particular course, e.g. for a course ASM001, divison can:
    --------------------------------------
    AM_ID CourseID SS_MarksType SS_Marks
    --------------------------------------
    1 ASM001 Mid-Term 30
    2 ASM001 End-Term 40
    3 ASM001 Project 30
    --------------------------------------

  • StudentMarks: This is the part, where I want something. I want whenever data is being input in StudentSpecialisation table, then automatically Student_Name and CourseID will be updated in this table by checking specialisation_1/specialisation_2 of dbo.StudentSpecialisation is equal to C_Specialisation of dbo.Course.
    Another thing, the SS_MarksType in dbo.StudentMarks will also be inserted by checking CourseID of dbo.StudentMarks is equal to dbo.AssignMarks of CourseID. This SS_MarksType can be come multiple times.
    Let me show, how it should look:
    -------------------------------------------------
    AM_ID Student_Name CourseID SS_MarksType SS_Mark
    -------------------------------------------------
    1 Andy ASM001 Mid-Term <NULL>
    2 Andy ASM001 End-Term <NULL>
    3 Andy ASM001 Project <NULL>
    4 Bob ASM001 Mid-Term <NULL>
    5 Bob ASM001 End-Term <NULL>
    6 Bob ASM001 Project <NULL>
    -------------------------------------------------

How can this be possible without trigger???
Daipayan

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-02-26 : 12:39:06
So why not use a trigger?

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-02-26 : 12:46:48
quote:
Originally posted by blindman

So why not use a trigger?

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________




Trigger will have to fire not only for INSERTS but also for DELETES and UPDATES. Suppose a student changes his/her idea about those two specialisations, the StudentMarks table will have to be altered to reflect that (delete or update previous records). And you will have to write triggers for all the tables that are related to StudentMarks: Course, StudentSpecialisation and AssignMarks. A change in any of those three tables - like a correction of a typing error - might have an impact on StudentMarks.


Daipayan
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-02-26 : 15:00:57
That makes no sense.
You can write triggers for inserts only, updates only, deletes only, or any combination of the tree.
How many tables need to be updated to reflect the change is dependent upon the business rules, not the method of implementation.
I can't see any reason why you could not use triggers for this.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-02-26 : 15:23:36
quote:
Originally posted by blindman

That makes no sense.
You can write triggers for inserts only, updates only, deletes only, or any combination of the tree.
How many tables need to be updated to reflect the change is dependent upon the business rules, not the method of implementation.
I can't see any reason why you could not use triggers for this.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________



Then, will you please tell me, how to write the trigger for my tables cause I never ever used trigger, am only 3 weeks old for MS SQL 2000.

Daipayan
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-02-26 : 15:59:36
What are the business rules?
Which tables should be updated when the student specialization changes?
Should they change on inserts, updates, deletes?

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-02-26 : 16:20:27
quote:
Originally posted by blindman

What are the business rules?
Which tables should be updated when the student specialization changes?
Should they change on inserts, updates, deletes?

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________



Only one table should be updated i.e. StudentMarks
The updatation will be such way that whenever data is being input in StudentSpecialisation table, then automatically Student_Name and CourseID will be updated in StudentMarks by checking specialisation_1/specialisation_2 of dbo.StudentSpecialisation is equal to C_Specialisation of dbo.Course. Another thing, the SS_MarksType in dbo.StudentMarks will also be inserted by checking CourseID of dbo.StudentMarks is equal to dbo.AssignMarks of CourseID. This SS_MarksType can be come multiple times. The StudentMarks table should look like in the following way after updatation:
-------------------------------------------------
AM_ID Student_Name CourseID SS_MarksType SS_Mark
-------------------------------------------------
1 Andy ASM001 Mid-Term <NULL>
2 Andy ASM001 End-Term <NULL>
3 Andy ASM001 Project <NULL>
4 Bob ASM001 Mid-Term <NULL>
5 Bob ASM001 End-Term <NULL>
6 Bob ASM001 Project <NULL>
-------------------------------------------------

Here, Student_Name is coming from StudentSpecialisation and CourseID & SS_MarksType from AssignMarks and SS_Mark will remain null and can be updated later.
And they should change on inserts, updates, deletes.
Is this possible?

Daipayan
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-02-26 : 16:57:24
Is Student_Name unique in the StudentSpecialization table?

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-02-26 : 17:00:58
quote:
Originally posted by blindman

Is Student_Name unique in the StudentSpecialization table?

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________



Yes sir...Student_Name is unique!

Daipayan
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-02-27 : 10:17:57
So, write your trigger to update related tables based on the Student_Name.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-02-27 : 11:31:14
quote:
Originally posted by blindman

So, write your trigger to update related tables based on the Student_Name.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________



--This is my StudentMark table, here I had inserted new column called ind_active!
create table dbo.StudentMark(
StudentMark_ID bigint identity not null,
Student_ID bigint not null,
Course_ID bigint not null,
AssgnMrk_ID bigint not null,
Mark bigint null,
ind_active CHAR(1) NOT NULL
Constraint c_ind_active CHECK (ind_active IN ('Y', 'N')),
constraint PK_SM_ID primary key(StudentMark_ID)
);

--This is my auto Insert Statement
INSERT INTO dbo.StudentMark (Student_Name, CourseID, SS_MarksType, ind_active)
SELECT StudentSpecialisation.Student_Name,
Course.CourseID,
AssignMark.AM_ID,
'Y'
FROM Course
INNER JOIN AssignMark ON
Course.CourseID = AssignMark.CourseID
INNER JOIN StudentSpecialisation ON
Course.C_Specialisation = StudentSpecialisation.SS_Specialisation1 OR
Course.C_Specialisation = StudentSpecialisation.SS_Specialisation2


Now, How should I insert this code in Trigger, am confused on this part??

Daipayan
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-02-27 : 12:09:29
Why are you inserting into StudentMark table? You said before you wanted to update.
You need to read the section in Books Online on triggers. Basically, you are going to write an update statement against the studentmark table, but instead of linking it to StudentSpecialisation you will link it to the virtual "inserted" table instead, and then wrap that statement in a trigger on the StudentSpecialisation table.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page
   

- Advertisement -