Solved

How do you do delete and update stored procedures?

Posted on 2009-07-14
6
141 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

21 Experts available now in Live!

Get 1:1 Help Now