0

I am fairly new to SQL. I am using SQL Server 2014. I want to run a query on a database which returns a column of ID's. I am wondering if it is possible to loop over the column of ID's from the first database and pass them into another database to collect additional info.

Attempted to Google the answer but I'm not able to find a helpful scenario that mimics what I am looking for.

SELECT * 
FROM dbo.MYDB1
WHERE CreatedLoc = 123

The above example spits out data but I only care about the ID column

I than want to loop over the ID column and for each run them on another database.

SELECT * 
FROM dbo.MYDB2
WHERE ID IN (array of ids here, not hardcoded but dynamic)

1 Answer 1

1

Assuming appropriate permissions, you can access a different database than the one you're currently connected to using a fully qualified databasename.schemaname.tablename (or view, etc.)

If your databases are MyDB1 and MyDB2, you can run a query that looks something like this:

SELECT * from MyDB2.dbo.Table2
where ID IN (
SELECT ID from MyDB1.dbo.Table1 where CreatedLoc = 123
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @jefftrotman that seemed to do the trick. Appreciate your fast response and assistance!

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.