The need to insert values from 3 tables into another table called myTable. The required fields in myTable are:
[Id_student] int,
[id_subjects] int
[degrees] float
[st_Name] nvarchar(30)
[Id_Class] int
[Id_Group] int
[Class] nvarchar(15)
[group] nvarchar(15))` ..
I created the stored procedure below. But after viewing the table I found that only the passed parameters were saved! ie @Id_student , @id_subjects , @degrees. Can anyone explain what is wrong with this code?
CREATE storedprocedure mySp_myTable_insert
@Id_student int,
@id_subjects int,
@degrees int
as
DECLARE @st_Name nvarchar(30)
SELECT @st_Name = pst.st_Name FROM dbo.sudents AS pst where pst.id_student=@id_student ;
INSERT [myTable]
(
[Id_student],
[id_subjects],
[degrees],
[st_Name],
[Id_Class],
[Id_Group],
[Class],
[group]
)
(select
@Id_student,
@id_subjects,
@degrees,
@st_Name
,tc.Id_Class
,tg.Id_Group
,tc.Class
,tg.group
from dbo.subjects sbj
inner join tGroup tg
inner join tClass tc
on tc.Id_Class=tg.Id_Class
on sbj.Id_Group =tg.Id_Group
where sbj.id_subjects=@id_subjects)
I think you should drop the parentheses around the
SELECTstatement and fix the order of thejoin–onkeywords/clauses.Try this version: