Solved

How do you do delete and update stored procedures?

Posted on 2009-07-14
6
146 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 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