MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
writing a stored procedure -

in Search

writing a stored procedure

Last post 03-21-2008 10:11 AM by krtyknm_gmail. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 03-07-2008 5:33 AM

    writing a stored procedure

    Hi,

    I have created a stored procedure which will delete a particular record in a table based on the input we give as a parameter. It is working fine.  Here is the following code

    Code
    -------
    Create procedure [dbo].[emp_insert]
    @Name nvarchar(15),
    @Age int,
    @DOB datetime,
    @BGRP nchar(10)
    as
    begin
    insert into Employee values(@Name,@Age,@DOB,@BGRP)
    end

    Now what i need is a small modification in the stored procedure. Before deleting the record in the table, it should get copied in a new table which has the same schema. Only after inserting the record in the new table, the existing record in the old table should get deleted. Pls give me the modification in the above coding.

    It is very urgent

    Pls help

    Filed under:
  • 03-07-2008 7:11 AM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 191

    Re: writing a stored procedure

    Here's a stab..... this is untested so test it out.  I don't know your schema so I made some assumptions 

     create procedure dbo.emp_insert @Name nvarchar(15),
                                    @Age int,
                                    @DOB datetime,
                                    @BGRP nchar(10)
    as

    set nocount on
    set xact_abort on

    begin tran tran1

    insert into dbo.Employee_History ([Name],Age,DOB,BGRP) values (@Name,@Age,@DOB,@BGRP)
    delete from dbo.Employee where [Name] = @Name

    commit

    go
     

  • 03-21-2008 10:11 AM In reply to

    Re: writing a stored procedure

    Hi Rajan, Instead of writing code. Why cant you use triggers. when you delete the record trigger automatically insert the old value into another table. Thanx, Karthik
Page 1 of 1 (3 items)