任何方式自动在表中添加否

时间:2013-03-02 16:40:55

标签: sql sql-server

这些是表格和编码。

create table student
(
s_id varchar(10) unique,
s_name varchar(50),
s_report varchar(50),
)

create table student_contact
(
stid int identity primary key,
stcellphone varchar(50) unique,
s_id varchar(10),
foreign key(s_id) references student(s_id)
)

create table test
(
t_id int identity primary key,
t_marks int,
)

create table st_test
(
stt_id int identity primary key,
s_id varchar(10),
t_id int,
foreign key(s_id) references student(s_id),
foreign key(t_id) references test(t_id)
)


alter view [join]
as
select s1.s_name,s2.stcellphone,s4.t_marks,s1.s_report
from
student as s1
inner join
student_contact as s2
ON s1.s_id=s2.s_id
inner join
st_test s3
ON s1.s_id=s3.s_id
inner join
test as s4
ON s4.t_id=s3.t_id

alter procedure marks
@st_id varchar(10)='',
@name varchar(50)='',
@mobile varchar(50)='',
@marks int=''
as
begin
    if(@marks < 3)
    begin
    insert into student values(@st_id,@name,'You are enrolled for 6 months')
    insert into test values(@marks)
    insert into student_contact values(@mobile,@st_id)
    select * from [join] where s_name =@name
    end
end

这个我创建的编码我创建了一个插入数据的过程现在我想在“st_test”表中有2个列名是(s_id,t_id),它们是对学生和测试表的引用现在问题是我创建了一个视图,其中这些表是连接的,这个视图已经在过程中使用过程中的问题是我想要当我填充所有参数时我将根据条件看到插入的表。

1 个答案:

答案 0 :(得分:2)

您需要考虑使用SCOPE_IDENTITY()

declare @id int
insert into test values(@marks)
SET @id = SCOPE_IDENTITY()
insert into st_test values(@st_id,@id)
insert into student_contact values(@mobile,@st_id)
select * from [join] where s_name =@name

我还建议在最后一个select语句中使用@id而不是s_name,因为你知道它应该是唯一的。