1

需要将 3 个表中的值插入另一个名为myTable. myTable 中的必填字段为:

[Id_student] int,
[id_subjects] int
[degrees] float
[st_Name] nvarchar(30) 
[Id_Class] int
[Id_Group] int 
[Class] nvarchar(15)
[group] nvarchar(15))` ..

我在下面创建了存储过程。但是查看表格后发现只保存了传递的参数!即@Id_student , @id_subjects , @degrees。谁能解释这段代码有什么问题?

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)
4

1 回答 1

3

我认为您应该删除语句周围的括号SELECT并修复join-on关键字/子句的顺序。

试试这个版本:

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 on sbj.Id_Group =tg.Id_Group
inner join tClass tc on tc.Id_Class=tg.Id_Class 
where sbj.id_subjects=@id_subjects

GO
于 2012-05-01T10:00:21.750 回答