Solved

How do you do delete and update stored procedures?

Posted on 2009-07-14
6
145 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 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

762 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