0

here is the dummy programhow can I save data from different databases into one database (MainDatabase)?

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
5
  • 1
    I think Database1 and Database2 should be Linked with MainDB to access! Commented Dec 22, 2018 at 9:22
  • Nope.. the project is intended to be dynamic, like calling a multiple counter and gets is total sales and put it in one database (MainDB).. something like that.. Commented Dec 22, 2018 at 9:25
  • 1
    what do you mean by dynamic? You can link databases and write Procedure on Database side for merging data. And you can execute your procedure on c# Commented Dec 22, 2018 at 10:15
  • I already did that it went well but in other cases like . cant put a string to an object @DATABASENAME varchar(50) AS insert into UserControlTenant.dbo.tenantData (tenant_name, receipt_id , amount, date, time) select * from [@DATABASENAME].dbo.transactiondata Commented Dec 22, 2018 at 11:26
  • btw Thanks... I got some ideas from now.. Thanks Commented Dec 22, 2018 at 11:30

1 Answer 1

2

In your case, you need database link in order to be able to access one database from another database.

INSERT INTO UserControlTenant.dbo.tenantData (tenant_name, receipt_id, amount, date, time)
    SELECT * from OPENQUERY(MyLinkedServer,'SELECT * FROM xxx.dbo.transactiondata');

or if it is mssql DB

INSERT INTO UserControlTenant.dbo.tenantData (tenant_name, receipt_id, amount, date, time)
    SELECT * from [SRVR002\ACCTG].dbo.transactiondata');

for creating database link check this reference

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

1 Comment

wooahh!! thats new for me... Thanks for helping a rookie here.. is that T-SQL? btw thanks

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.