创建过程以将值从两个表插入到第三个

时间:2015-06-22 12:39:21

标签: sql-server stored-procedures

我有3张表如下:

表费用计划

CREATE TABLE [dbo].[FeePlan](
[FeePlanID] [int] IDENTITY(1,1) NOT NULL,
[Course] [nvarchar](50) NOT NULL,
[Semester] [nvarchar](50) NOT NULL,
[TotalFee] [int] NOT NULL,
[Remarks] [nvarchar](100) NOT NULL,
 CONSTRAINT [PK_FeePlan] PRIMARY KEY CLUSTERED 
(
[FeePlanID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

表StudentRegister

CREATE TABLE [dbo].[StudentRegister](
[Student_ID] [varchar](50) NOT NULL,
[Student_Name] [varchar](50) NULL,
[Course] [varchar](50) NULL,
[Roll_Number] [varchar](30) NULL,
[Semester] [varchar](10) NULL,
[Shift] [varchar](30) NULL,
[Address] [varchar](50) NULL,
[Phone_Number] [varchar](10) NULL,
[Gender] [varchar](10) NULL,
[Email_ID] [varchar](50) NULL,
[Fees] [varchar](20) NULL,
[Transportaion] [varchar](10) NULL,
PRIMARY KEY CLUSTERED 
(
[Student_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

表费用

CREATE TABLE [dbo].[FeePayment](
[FeePaymentID] [int] IDENTITY(1,1) NOT NULL,
[StudentID] [varchar](50) NOT NULL,
[StudentName] [varchar](50) NULL,
[Course] [varchar](50) NULL,
[Semester] [varchar](10) NULL,
[TotalFee] [int] NULL,
[Schlorship] [int] NULL,
[AmountPaid] [int] NULL,
[DueAmoount] [int] NULL,
[Remarks] [varchar](100) NULL,
 CONSTRAINT [PK_FeePayment] PRIMARY KEY CLUSTERED 
(
[FeePaymentID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

我需要创建的是一个过程,当学生详细信息被插入表StudentRegister时,它应该比较表FeePlan中的值并将表StudentRegister和FeePlan中的值插入表FeePayment。

要插入FeePayment的StudentRegister表中的值为:Student_ID,Student_Name,Course,Semester,并将这些值与表FeePlan值TotalFee进行比较,DueAmount将被输入FeePayment表。

我创建的程序如下,但它没有用。

Create Procedure [dbo].[SP_AddFeePayment]
(

@StudentID varchar(50),
@StudentName varchar(50),
@Course varchar(50),
@Semester varchar(50),
@TotalFee int,
@Schlorship int,
@AmountPaid int,
@DueAmount int,
@Remarks varchar(100)

)

AS

BEGIN

INSERT INTO
dbo.FeePayment(StudentID,StudentName,Course,Semester)
SELECT
      StudentRegister.Student_ID,StudentRegister.Student_Name,StudentRegister.Course,StudentRegister.Semester
From 
dbo.StudentRegister
Where dbo.StudentRegister.Student_Id=@StudentID
END

0 个答案:

没有答案