Solved

How do you do delete and update stored procedures?

Posted on 2009-07-14
6
142 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
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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server; storing data in offline mode. 10 67
How to PARSE a text field that is delimited by '~' character? 3 53
sql help 5 52
Passing value to a stored procedure 8 89
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now