How do we import the data from table using Stored Procedure

BeUnique 2,332 Reputation points
2022-12-05T06:00:20.743+00:00

I have two tables (Table-1 and Table-2)

Table-1 data is coming from the jobs which is another source of origin.

Some times the jobs getting failed due to n/w or someother server issues.

so, that time the complete data does not import from the source.

because of this failure, it will be impacting of our application and report.

so we decided to use another table called "Table-2". It will have the same structure of the first table (Table-1).

we don't want to disturb Table-1 always and planning to use Table-2 for app and report purpose.

Here we want to create one stored procedure and this we can make it in the schedule based on the below conitions.

How to create the SP using below conditions.

     1. Before importing data from Table-1, it should check the row count of Table-2.  
 If Row count of Table-2 is equal or greater than Table-1, then import data should be happening. other wise it should not do any import.  

How to create the stored procedure like above conditions..?

266996-image.png

The Final conclusion is, we can always refer Table-2 with data for apps and reports.

Developer technologies | Transact-SQL
{count} votes

2 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,576 Reputation points
    2022-12-05T06:28:54.897+00:00

    Hi @BeUnique
    Don't know what your table like, maybe you could try something like this:

    CREATE OR ALTER PROC sp_test  
    AS   
    DECLARE @RowCount1 INT  
    DECLARE @RowCount2 INT  
    SELECT @RowCount1=COUNT(*) FROM Table1;  
    SELECT @RowCount2=COUNT(*) FROM Table2;  
    IF @RowCount1 > @RowCount2  
      ......  
    ELSE  
      ......  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Olaf Helper 47,526 Reputation points
    2022-12-05T06:35:25.82+00:00

    No clear, what your final question is.
    Do as you described your process, import first into a stagging table and if everything is fine, exchange the data.
    If the table & index design is exact the same, then the easiest & most performant way is to do a partition switch.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.