开发者

Creating MSSQL Stored Procedure to check TableA record count. Then insert TableA records to TableB

Hoping someone can give me a general example of how to do the following in MSSQL 2008/2005

开发者_如何学JAVA

I need to do the following in 1 stored procedure.

I need it to verify TableA has more than 1 record. If TableA has more than one record then:

Delete all records from TableB AND copy the records from TableA to TableB

For the sake of argument and/or simplicity TableA and TableB Schemes are the same

This task wouldn't be that hard if I was performing the tasks in VB but I am trying to offload this work to the SQL server and I am not familiar on how to accomplish this.


Try something like this:

 CREATE PROC DoStuff
 AS 

    IF (SELECT COUNT(*) FROM TableA) > 1
    BEGIN

         DELETE TableB;
         INSERT INTO TableB (ID, CustomerName) 
             SELECT ID, CustomerName
             FROM TableA;
    END


I would recommend looking at SQL Server integration services. It is designed to perform exactly the types of tasks you are trying to do.

Here are a couple of links to get you started:

  • http://sql-bi-dev.blogspot.com/2010/11/incremental-load-using-ssis-package.html
  • http://vsteamsystemcentral.com/cs21/blogs/applied_business_intelligence/archive/2007/05/21/ssis-design-pattern-incremental-loads.aspx

These go over using an Incremental Load which seems to be what you want to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜