Monday, March 16, 2015

Stored Procedure

ALTER PROCEDURE [dbo].[spSaveStudent]
    -- Add the parameters for the stored procedure here
    @StudentId int output,
    @StudentFirstName varchar(100),
    @StudentLastName varchar(100),
    @MaxQualificationId int,
    @Age int,
    @Location varchar(100),
    @UserId 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
    IF NOT EXists(SELECT [StudentId] FROM [dbo].[tbl_Student] WHERE [StudentId]=@StudentId)
    BEGIN
    INSERT INTO [dbo].[tbl_Student]
           ([StudentFirstName]
           ,[StudentLastName]
           ,[MaxQualificationId]
           ,[Age]
           ,[Location]
           ,[CreateUserId]
           ,[CreateDate])
     VALUES
           (@StudentFirstName,
           @StudentLastName,
           @MaxQualificationId,
           @Age,
           @Location,
           @UserId,
           Getdate()
           )
         SET  @StudentId=@@Identity;
    END
    ELSE
    BEGIN
    UPDATE [dbo].[tbl_Student]
    SET [StudentFirstName] =@StudentFirstName
      ,[StudentLastName] = @StudentLastName
      ,[MaxQualificationId] =@MaxQualificationId
      ,[Age] = @Age
      ,[Location] =@Location
      ,[UpdateUserId] = @UserId
      ,[UpdateDate] = Getdate()
     WHERE [StudentId]=@StudentId
      SET  @StudentId = @StudentId;
    END

END

No comments:

Post a Comment