?
Solved

How do you do delete and update stored procedures?

Posted on 2009-07-14
6
Medium Priority
?
148 Views
Last Modified: 2012-05-07
I am trying to do a delete and update stored procedure based on a certain value.  How would you delete and update with stored procedures if this is the insert stored procedure?
USE [EmployeeScreening]
GO
/****** Object:  StoredProcedure [dbo].[InsertMemberFamilyInfo]    Script Date: 07/14/2009 15:50:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Brett Smith>
-- Create date: <07/13/09>
-- Description:	<Insert Employee Family Information>
-- =============================================
ALTER PROCEDURE [dbo].[InsertMemberFamilyInfo]
	-- Add the parameters for the stored procedure here	
	@MemberFamilyID				int = NULL OUTPUT,
	@EmployeeID					int,
	@MemberFamilyNumber			int,
	@Allergies					nvarchar(50),
	@Weight						int,
	@Age						int,
	@DateOfBirth				datetime,
	@MemberFamilyCreatedDate	datetime,
	@MemberFamilyCreatedBy		nvarchar(50)
AS
BEGIN
 
	    -- Insert statements for procedure here
	INSERT INTO MemberFamilyInfo (EmployeeID, MemberFamilyNumber, Allergies,
	Weight, Age, DateOfBirth, MemberFamilyCreatedDate, MemberFamilyCreatedBy) VALUES
(@EmployeeID, @MemberFamilyNumber, @Allergies, @Weight, @Age, @DateOfBirth,
@MemberFamilyCreatedDate, @MemberFamilyCreatedBy);
 
SET @MemberFamilyID = SCOPE_IDENTITY();
		
	END;
 
DECLARE @ID INT;
EXEC dbo.InsertMemberFamilyInfo @MemberFamilyID = @ID OUT,
@EmployeeID = 1,
@MemberFamilyNumber = 1,
@Allergies='',
@Weight=1,
@Age = 99,
@DateOfBirth='19000101',
@MemberFamilyCreatedDate = '20090714',
@MemberFamilyCreatedBy = 'Me'
	 
	 
 
SELECT @ID ID;

Open in new window

0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 24853580
update:

USE [EmployeeScreening]
GO
/****** Object:  StoredProcedure [dbo].[InsertMemberFamilyInfo]    Script Date: 07/14/2009 15:50:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            <Brett Smith>
-- Create date: <07/13/09>
-- Description:      <Insert Employee Family Information>
-- =============================================
CREATE PROCEDURE [dbo].[UpdateMemberFamilyInfo]
      -- Add the parameters for the stored procedure here      
      @MemberFamilyID                        int ,
      @EmployeeID                              int,
      @MemberFamilyNumber                  int,
      @Allergies                              nvarchar(50),
      @Weight                                    int,
      @Age                                    int,
      @DateOfBirth                        datetime,
      @MemberFamilyCreatedDate      datetime,
      @MemberFamilyCreatedBy            nvarchar(50)
AS
BEGIN
 
          -- Insert statements for procedure here
UPDATE MemberFamilyInfo
SET
      EmployeeID =@EmployeeID ,
      MemberFamilyNumber = @MemberFamilyNumber,
      Allergies = @Allergies,
      Weight = @Weight,  
      Age = @Age,
      DateOfBirth = @DateOfBirth,
      MemberFamilyCreatedDate = @MemberFamilyCreatedDate,  
      MemberFamilyCreatedBy = @MemberFamilyCreatedBy,  
WHERE MemberFamilyID = @MemberFamilyID

END;
 

EXEC dbo.InsertMemberFamilyInfo @MemberFamilyID = 33 ,
@EmployeeID = 1,
@MemberFamilyNumber = 1,
@Allergies='',
@Weight=1,
@Age = 99,
@DateOfBirth='19000101',
@MemberFamilyCreatedDate = '20090714',
@MemberFamilyCreatedBy = 'Me'
      
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24853583
you run the ALTER procedure ONCE,
and then run the other code.

what you posted looks like you run all in 1, making the second part actually being part of the stored proc itself...
in the query window (sql management studio), pout a line GO between the 2 parts (before DECLARE @ID INT;)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24853590
delete:

USE [EmployeeScreening]
GO
/****** Object:  StoredProcedure [dbo].[InsertMemberFamilyInfo]    Script Date: 07/14/2009 15:50:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            <Brett Smith>
-- Create date: <07/13/09>
-- Description:      <Insert Employee Family Information>
-- =============================================
CREATE PROCEDURE [dbo].[DeleteMemberFamilyInfo]
      -- Add the parameters for the stored procedure here      
      @MemberFamilyID                        int
AS
BEGIN
 
          -- Insert statements for procedure here
DELETE MemberFamilyInfo
WHERE MemberFamilyID = @MemberFamilyID

END;
 

EXEC dbo.DeleteMemberFamilyInfo @MemberFamilyID = 33 ,
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24853591
CREATE PROCEDURE [dbo].[UpdateMemberFamilyInfo]
      -- Add the parameters for the stored procedure here      
    @EmployeeID int
   ,@MemberFamilyNumber int
   ,@Allergies nvarchar(50)
   ,@Weight int
   ,@Age int
   ,@DateOfBirth datetime
   ,@MemberFamilyCreatedDate datetime
   ,@MemberFamilyCreatedBy nvarchar(50)
AS
BEGIN
 
    UPDATE  MemberFamilyInfo
    SET     EmployeeID = @EmployeeID
           ,MemberFamilyNumber = @MemberFamilyNumber
           ,Allergies = @Allergies
           ,Weight = @Weight
           ,Age = @Age
           ,DateOfBirth = @DateOfBirth
           ,MemberFamilyCreatedDate = @MemberFamilyCreatedDate
           ,MemberFamilyCreatedBy = @MemberFamilyCreatedBy
    WHERE   MemberFamilyID = @MemberFamilyID
END ;

GO

CREATE PROCEDURE [dbo].[DeleteMemberFamilyInfo]
      -- Add the parameters for the stored procedure here      
    @MemberFamilyID int
AS
BEGIN
 
          -- Insert statements for procedure here
    delete  from MemberFamilyInfo
    where   MemberFamilyID = @MemberFamilyID
           
END ;
0
 
LVL 2

Expert Comment

by:matty1stop
ID: 24853596
Its a little hard to tell what you are asking but is this it? (delete)

Matt
USE [EmployeeScreening]
GO
/****** Object:  StoredProcedure [dbo].[InsertMemberFamilyInfo]    Script Date: 07/14/2009 15:50:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Brett Smith>
-- Create date: <07/13/09>
-- Description:	<Insert Employee Family Information>
-- =============================================
Create PROCEDURE [dbo].[DeleteMemberFamilyInfo]
	-- Add the parameters for the stored procedure here	
	@MemberFamilyID				int = NULL OUTPUT,
AS
BEGIN
 
	    -- delete statements for procedure here
		delete MemberFamilyInfo 
		where MemberFamilyNumber = @MemberFamilyNumber
		
	END;

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24853603
this is how you run the update...i forgot to swap out the name

EXEC dbo.UPdateMemberFamilyInfo @MemberFamilyID = 33 ,
@EmployeeID = 1,
@MemberFamilyNumber = 1,
@Allergies='',
@Weight=1,
@Age = 99,
@DateOfBirth='19000101',
@MemberFamilyCreatedDate = '20090714',
@MemberFamilyCreatedBy = 'Me'

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question