Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define table names for results of multiple SQL selects in a single query

For example if I run the following query:

select * from table1
select * from table2

And run it with a DB adapter (C#) I get a dataset with two tables. How can I define the names for the result tables in SQL?

I can only do this inside the SQL. I don't have access to the c# code.

like image 538
LiorE Avatar asked Nov 01 '08 00:11

LiorE


People also ask

How can I get data from multiple tables in a single query?

In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.


1 Answers

@Timothy Khouri: It can be done! EDIT: but not at the SQL level!

You can use TableMappings on the DataAdapter.

If the SelectCommand of a DataAdapter returns multiple result sets, the DataAdapter uses table mappings to fill corresponding DataTables in a DataSet. By default, the first result set will be filled to a DataTable named "Table", and the second result set will be filled to a DataTable named "Table1" etc.

SqlDataAdapter sqlDa = new SqlDataAdapter();
SqlCommand selectCmd = new SqlCommand();
selectCmd.CommandText = "spReturnMultpileResultSets";
selectCmd.CommandType = CommandType.StoredProcedure;
selectCmd.Connection = this.sqlConnection1;
sqlDa.SelectCommand = selectCmd;

// Add table mappings to the SqlDataAdapter
sqlDa.TableMappings.Add("Table", "Customers");
sqlDa.TableMappings.Add("Table1", "Orders");

// DataSet1 is a strongly typed DataSet
DataSet1 ds = new DataSet1();

this.sqlConnection1.Open();

sqlDa.Fill(ds);

this.sqlConnection1.Close();

Refs:

http://blogs.msdn.com/vsdata/archive/2007/03/08/tableadapter-multiple-result-sets.aspx http://www.eggheadcafe.com/software/aspnet/32696845/strongly-typed-datasets.aspx

like image 178
Mitch Wheat Avatar answered Sep 17 '22 15:09

Mitch Wheat