

/****** Object:  StoredProcedure [dbo].[usp_Delete_Company]    Script Date: 11/25/2015 12:35:28 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[usp_Delete_Company]
	-- Add the parameters for the stored procedure here
	@Id as int = null,
	@ErrorCode int output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DELETE FROM [dbo].[Company]
	WHERE Company.Id = @Id

SET @ErrorCode = @@ERROR
END
GO

/****** Object:  StoredProcedure [dbo].[usp_GetAllCompanies]    Script Date: 11/25/2015 12:35:28 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[usp_GetAllCompanies]
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT 
		[Id]
		,[Name]
		,[Description]
		,[CreatedBy]
		,[CreatedDate]
		,[UpdatedDate]
		,[Status]
	FROM
		Company
END

GO

/****** Object:  StoredProcedure [dbo].[usp_GetCompanyById]    Script Date: 11/25/2015 12:35:28 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create PROCEDURE [dbo].[usp_GetCompanyById]
	@CompanyId 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
	SELECT 
		[Id]
		,[Name]
		,[Description]
		,[CreatedBy]
		,[CreatedDate]
		,[UpdatedDate]
		,[Status]
	FROM
		Company
	WHERE Id = @CompanyId
END

GO

/****** Object:  StoredProcedure [dbo].[usp_Insert_Company]    Script Date: 11/25/2015 12:35:28 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[usp_Insert_Company]
	-- Add the parameters for the stored procedure here
	@Name as varchar(100),
	@Description as nvarchar(500),
    @CreatedBy as int,
    @CreatedDate as datetime,
	@UpdatedDate as datetime,
	@Status as int,
	@ReturnValue int output,
	@ErrorCode int output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    INSERT INTO [dbo].[Company]
           ([Name]
           ,[Description]
           ,[CreatedBy]
           ,[CreatedDate]
           ,[UpdatedDate]
           ,[Status])
     VALUES
           (@Name
           ,@Description
           ,@CreatedBy
           ,@CreatedDate
           ,@UpdatedDate
           ,@Status)

Set @ReturnValue = @@IDENTITY
SET @ErrorCode = @@ERROR
END

GO

/****** Object:  StoredProcedure [dbo].[usp_Update_Company]    Script Date: 11/25/2015 12:35:28 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[usp_Update_Company]
	-- Add the parameters for the stored procedure here
	@Id as int = null,
	@Name as varchar(100),
	@Description as nvarchar(500),
    @CreatedBy as int,
    @CreatedDate as datetime,
	@UpdatedDate as datetime,
	@Status as int,
	@ReturnValue int output,
	@ErrorCode int output
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	if @Id is null 
	begin
		INSERT INTO [dbo].[Company]
           ([Name]
           ,[Description]
           ,[CreatedBy]
           ,[CreatedDate]
           ,[UpdatedDate]
           ,[Status])
		VALUES
           (@Name
           ,@Description
           ,@CreatedBy
           ,@CreatedDate
           ,@UpdatedDate
           ,@Status)
		Set @ReturnValue = @@IDENTITY
	end
	else
	begin
		UPDATE [dbo].[Company]
		SET [Name] = @Name
			,[Description] = @Description
			,[CreatedBy] = @CreatedBy
			,[CreatedDate] = @CreatedDate
			,[UpdatedDate] = @UpdatedDate
			,[Status] = @Status
		WHERE [Id] = @Id
		Set @ReturnValue = @Id
	end
SET @ErrorCode = @@ERROR
END
GO


