在SqlServer中複製表記錄,複製報告

來源:互聯網
上載者:User
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

--作者:Ran(Sunday_hl)
--建立時間:2005-12-14
--實現功能:複製報告,主要複製客戶基本資料,檢測項目,批次,費用等資訊
--參數說明:@old_accept_no被複製的編號,@new_accept_no新的受理

ALTER  Procedure copy_reports
    @old_accept_no  varchar(20),
    @new_accept_no  varchar(20)
AS
    --判斷傳入的老編號
    If @old_accept_no Is Null Or @old_accept_no = ''
    Begin
        RAISERROR ('被複製的編號不可為空。', 16, 1)
        Return -1;
    End
    --判斷傳入的新受理號
    If @new_accept_no Is Null Or @new_accept_no = ''
    Begin
        RAISERROR ('新的編號不可為空。', 16, 1)
        Return -2;
    End
    --判斷新舊兩個編號是否相同
    If @new_accept_no = @old_accept_no
    Begin
        RAISERROR ('新舊兩個編號不能相同。', 16, 1)
        Return -3;
    End   
    --判斷老的記錄是否存在,這裡只判斷tb_base中的記錄,其他的就不判斷了
    If Not Exists(Select * From tb_base Where accept_no = @old_accept_no)
    Begin
        RAISERROR ('沒有找到被複製的相關記錄。', 16, 1)
        Return -4;
    End
    --判斷新的記錄是否存在,這裡只判斷tb_base中的記錄,其他的就不判斷了
    If Exists(Select * From tb_base Where accept_no = @new_accept_no)
    Begin
        RAISERROR ('基本資料表中包含了新的編號的相關記錄了,請查實.', 16, 1)
        Return -5;
    End
--開始複製資料
Begin TransAction
    --複製基本資料tb_base
    If Exists(Select * From tb_base Where accept_no = @old_accept_no)
    Begin
        If object_id('tempdb.dbo.#tb_base_temp') Is not null
            Drop Table #tb_base_temp
        --把需要複製的相關記錄儲存到暫存資料表中
        Select * Into #tb_base_temp
            From tb_base
            Where accept_no = @old_accept_no
         --這個是刪除自動增加列,如果不刪除會出錯的
        Exec ('Alter Table #tb_base_temp Drop Column id ')
        --修改暫存資料表中相關資料,使其符合新的記錄要求,並要保證符合表中相關約束
        Update #tb_base_temp Set accept_no = @new_accept_no,flag = '0'
        --把修改好的暫存資料表的資料插入到 表中
        Insert Into tb_base Select * From #tb_base_temp
        --出現錯誤就 跳轉
        If (@@Error <> 0) GOTO err
    End
   ........................約..................................
--複製表1......
--複製表n......
   ........................約..................................

--提交事務,如果一個失敗就復原所有事務
    err:
    If @@Error = 0
        Commit TransAction
    Else
    Begin
        RollBack TransAction
        RAISERROR ('複製失敗', 16, 3)
    End

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

//複製當前報告PB中調用

String ls_old_accept_no,ls_new_accept_no

ls_old_accept_no = is_accept_no
IF  ls_old_accept_no = "" OR IsNull(ls_old_accept_no) THEN RETURN

IF MessageBox("提示資訊","您確認要複製‘" + ls_old_accept_no + "’此報告嗎?",Question!,yesno!,1) <> 1 THEN RETURN

DECLARE Emp_proc PROCEDURE FOR dbo.copy_reports
    @old_accept_no = :ls_old_accept_no,
    @new_accept_no = :ls_new_accept_no output
    USING Sqlca;
   
EXECUTE Emp_proc;
FETCH  Emp_proc  INTO :ls_new_accept_no;
CLOSE  Emp_proc;

IF Sqlca.SQLCode = 100 OR Sqlca.SQLCode = 0 THEN
    COMMIT USING Sqlca;
    MessageBox("複製報告資訊","複製成功!,新的編號為: '"+ls_new_accept_no+"'")
ELSE  
    MessageBox("複製報告錯誤資訊",Sqlca.SQLErrText )
 ROLLBACK USING Sqlca;
END IF

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.