使用过程SQL SERVER在表中插入值

时间:2018-05-01 12:41:37

标签: sql-server

我正在尝试在Schedule表中插入看起来像这样的值:

ID          int         Unchecked
TeacherID   int         Unchecked
SubjectID   int         Unchecked
DayID       int         Unchecked
Time        varchar(5)  Unchecked
StatusID    int         Unchecked

还有教师,主题和日期表,我存储有关姓名,姓氏等教师的信息......看起来像:

ID          int             Unchecked
FirstName   nvarchar(50)    Unchecked
LastName    nvarchar(50)    Unchecked
Mobile      int             Unchecked
IDNumber    varchar(11)     Unchecked
StatusID    int             Unchecked

这是主题表

ID          int             Unchecked
Name        nvarchar(50)    Unchecked
StatusID    int             Unchecked

这里是Days table

ID      int             Unchecked
Name    nvarchar(15)    Unchecked

想要制作一个带参数的程序:

@FirstName NVARCHAR(50),
@LastName NVARCHAR(50),
@Subject NVARCHAR(50),
@Day NVARCHAR(15),
@Time VARCHAR(5),
@StatusID INT

因此,该程序将从教师的表,主题表,天数表中获取教师ID,并将它们与其他参数一起插入。

我不知道是否有可能与程序有关。建议其他方式。

ID列是增量类型。

2 个答案:

答案 0 :(得分:1)

create index iSubj on Subjects (Name) include (ID);
create index iDays on Days (Name) include (ID)
create index iSched on Schedules (TeacherID, SubjectID, DayID, Time, StatusID) include (ID)


create proc InsertSchedule @FirstName varchar(50), @LastName varchar(50), @Subject varchar(50), @Day varchar(15), @Time varchar(5), @StatusID int as begin

insert Schedule (TeacherID, SubjectID, DayID, Time, StatusID)
select distinct t.ID, s.ID, d.ID, @Time, @StatusID
from Teachers t
    inner join Subjects s on s.Name = @Subject
    inner join Days d on d.Name = @Day
    left join Schedule sch (nolock) on sch.TeacherID = t.ID
        and sch.SubjectID = s.ID
        and sch.DayID = d.ID
        and sch.Time = @Time
        and sch.StatusID = @StatusID
where t.FirstName = @FirstName
    and t.LastName = @LastName
    and sch.ID is null

if @@rowcount > 1 print 'Warning: Duplicate Teachers, Subjects, or Days detected'

end

我不知道您的数据是什么样的,但我也建议您进行研究:

  • 日期,时间和日期时间类型
  • DateName和DatePart函数
  • 重命名@FirstName和@LastName以反映它是老师的名字
  • 删除Days表,除非您有很好的理由不只是将Date和/或DateTime列放在Schedule中。
  • 从用户界面的下拉菜单中选择教师,然后将TeacherID传入。主题同上;和Days,如果您不能将Days设为“适当”类型。

答案 1 :(得分:-1)

USE [Corporation]
GO
/****** Object:  StoredProcedure [dbo].[spAddSchedule]    Script Date: 
5/1/2018 17:28:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spAddSchedule] 
-- Add the parameters for the stored procedure here
    @FirstName NVARCHAR(50),
    @LastName NVARCHAR(50),
    @Subject NVARCHAR(50),
    @Day NVARCHAR(15),
    @Time VARCHAR(5),
    @StatusID INT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

    INSERT  dbo.Schedule
        ( TeacherID ,
          SubjectID ,
          DayID ,
          Time ,
          StatusID
        )
    VALUES  ( (SELECT dbo.Teacher.ID FROM dbo.Teacher WHERE FirstName=@FirstName AND LastName=@LastName) , -- TeacherID - int
          (SELECT dbo.Subject.ID FROM dbo.Subject WHERE Name=@Subject) , -- SubjectID - int
          (SELECT dbo.Days.ID FROM dbo.Days WHERE Name=@Day) , -- DayID - int
          @Time , -- Time - varchar(5)
          @StatusID  -- StatusID - int
        )

END