1

I have two query's. One from one database, and one from another.

The second query uses the first query's data as a table. (This is what I need to know how to do)

The query's more complicated then the example but i tried to make it simple. Example below:

DataTable dt = new DataTable();
OdbcConnection myConnection = salesConnection();
myConnection.Open();

OdbcCommand selectCMD = new OdbcCommand("SELECT CUSTOMER.CUSTOMER_NBR " +
"FROM CUSTOMER", myConnection);

OdbcDataAdapter cmd = new OdbcDataAdapter();
cmd.SelectCommand = selectCMD;

cmd.Fill(dt);
myConnection.Close();

DataTable dtM = new DataTable();
myConnection = mConnection();
myConnection.Open();

selectCMD = new OdbcCommand("SELECT '**DATA TABLE ABOVE**'.CUSTOMER_NBR " + 
                            "FROM '**HOW TO I REFERENCE THE DATA TABLE ABOVE**'",
                            myConnection);

cmd = new OdbcDataAdapter();
cmd.SelectCommand = selectCMD;

cmd.Fill(dtM);
myConnection.Close();
7
  • Does the first query return a single CUSTOMER_NBR or multiple? In general, use parameters in your second command which are set from the first table's row(s) (you could use a lop if it mcontains multiple rows). Commented Sep 3, 2013 at 13:45
  • If you need to use the data from first qurey, why don't you combine it to your second one? Commented Sep 3, 2013 at 13:47
  • It returns a big list of customer names and numbers (for this example a big list of numbers). Use a lop? Never heard of it. Commented Sep 3, 2013 at 13:50
  • huMpty duMpty, because their not from the same database, they require two different connections strings to get data Commented Sep 3, 2013 at 13:51
  • I thought I deleted my answer. lol. I dont think you can do what your asking because sql doesn't understand a .net datatable. You'd have to build your sql using the data in your datatable. If the answer to huMpty duMpty question is correct the solution be easier than you think and he can help Commented Sep 3, 2013 at 14:05

1 Answer 1

1

I don't think you can do what you are trying to do at the moment

As far as I know, you will need to combine both the queries in to one in the second stage.

You can have a detail look at sp_addlinkedserver (T-SQL)

For example

Select Columns
From   TableName TB1
       Inner Join [OtherServer].[DatabaseName].[dbo].[TabeleName] Tb 
                                        ON Tb.Column=TB1.Column

Update

If this is Oracle , please refer Accessing and Modifying Information in Multiple Databases (Oracle)

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

4 Comments

Are we sure this is a SQL server we're dealing with?
I'll test out your suggestions. Thanks
@Luke As an aside it is possible to send a DataTable to an Oracle procedure and then join to it. See stackoverflow.com/questions/5557318 but you can't use a Odbc Command, you'd have to use ODP.NET.
I marked this as the answer because it pushed me into the right direction. I changed the whole sql so it doesn't depend on the first query.

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.