0

Here is the actual dummy program

enter image description here

Can I ask a question about this, how to save data from different databases into one database (MainDatabase). It is possible right?

I just want to have an idea making a simple program that can retrieve data from multiple databases with the SAME table name e.g transactionTBL, and then save it to one database with the same columns and value. btw I did tried using a stored procedure - but it have to be an object not varchar or string.

@DATABASE varchar(50)

INSERT INTO UserControlTenant.dbo.tenantData (tenant_name, receipt_id, amount, date, time)
    SELECT * 
    FROM ___.dbo.transactiondata 

Example:

Database1

~transactiontbl~

ID
receiptID
amount
date time

Database2

~transactiontbl~

ID
receiptID
amount
date time

- MainDB

~transactiontbl~

ID
receiptID
amount
date time
2
  • It is as simple as providing a connection string per database. You can switch them at runtime as long as the schemas are the same. Commented Dec 22, 2018 at 11:02
  • Is there any difference other than the posted author hmm Commented Dec 22, 2018 at 12:28

3 Answers 3

1

if the databases are on the same server you could use an insert select based on union

INSERT INTO UserControlTenant.dbo.tenantData (tenant_name, receipt_id, amount, date, time)
select  'db1_tenant_name', receiptID, amount,date, time
from db1.dbo.tenantData 
UNION ALL 
select  'db2_tenant_name', receiptID, amount,date, time
from db2.dbo.tenantData 
UNION ALL
....
select  'dbn_tenant_name', receiptID, amount,date, time
from dbn.dbo.tenantData 

use UNION ALL if you want all the rows values use UNION if you want only distinct result

Sign up to request clarification or add additional context in comments.

3 Comments

I already did that.. it went well but my main problem is I want to pass the data drom db1,db2 to my MainDb.. UNION is not a problem but the problem is if I create a new database..
@DATABASENAME varchar(50) AS insert into UserControlTenant.dbo.tenantData (tenant_name, receipt_id , amount, date, time) select * from [@DATABASENAME].dbo.transactiondata
you're trying to specify target db in the variable name; it won't work, you must need to specify target db the way scaisEdge specify
1

You could use the below cursor to fetch all databases from where you want to get the records and store in the other database

DECLARE @Database NVARCHAR(500) ;
DECLARE @Query NVARCHAR(1000);

DECLARE looper CURSOR FOR 
SELECT [name] FROM sys.databases 
WHERE [name] IN ('test','test1') --you could edit your where clause for the 
                                   database 
                                 --you need to fetch data from 
OPEN looper    

FETCH NEXT FROM looper     
INTO @Database   

WHILE @@FETCH_STATUS = 0    
BEGIN    


    SET @Query = '
    INSERT INTO UserControlTenant.dbo.tenantData (tenant_name, receipt_id, 
    amount, date, time)
    SELECT '+@Database +',receiptID, amount,date, time FROM 
    '+@Database+'.dbo.transactiondata  
    '

    EXEC (@Query)

FETCH NEXT FROM looper     
INTO @Database    

END     
CLOSE looper;    
DEALLOCATE looper; 

Comments

0

It can be achieved in many ways. The performance and duration need to be considered on all the scenarios. Best approach is do it on the SQL side itself using linked server "open insert" query . Another can be done using dynamic query with all the databases values merged together in stored procedure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.