-1

I was thinking is there a query that can select many table at once?. Not using a JOINS. I tried to research about it but all I got is

SELECT * FROM Table WHERE ID IN (parameter1,parameter2);

I tried this kind of query

select * from Table Table1,Table2

but the result is getting from the Table1 only.

All tables are not it relation with each other so it means they are different table to each. What I am trying to do here is I will check the tables if there is a data on it or not. What I was using is query it all one-by-one using

select * from Table

again and again. Hope this will explain it.

KiRa
  • 924
  • 3
  • 17
  • 42
  • try this : select * from Table1,table2,table3... – chirag satapara Aug 30 '17 at 08:56
  • @chiragsatapara that query. I try it. – KiRa Aug 30 '17 at 08:57
  • Please clarify what you are trying to achieve. Why are JOINs not suited for your task? Are the tables identical in structure/column setup? Why can you not type multiple queries (one for each table)? – SchmitzIT Aug 30 '17 at 08:59
  • @KiRa: Really you are looking for a combined table as output, or separate tables? If combined you can use chirag satapara' s solution. else write each select statement separately, and you can retrieve them to DataSet. – gjijo Aug 30 '17 at 09:03
  • @gjijo seperately. – KiRa Aug 30 '17 at 09:05
  • 1
    Then you can write it SELECT * FROM table1; SELECT * FROM table2; In c# code you can use DataSet to hold them and can access each table separately using index as .Table[index] – gjijo Aug 30 '17 at 09:09
  • https://stackoverflow.com/questions/7239450/returning-multiple-tables-from-a-stored-procedure may be what you are looking for. – gjijo Aug 30 '17 at 09:21

2 Answers2

1

For combined Tables as an output:

UNION

To allow duplicate values, use:

UNION ALL

SELECT * FROM table1 

UNION 

SELECT * FROM table2

For separated Tables as an output:

DataSet

DataSet dataSet = new DataSet();
DataTable table1 = new DataTable("table1");
DataTable table2 = new DataTable("table2");
dataSet.Tables.Add(table1);
dataSet.Tables.Add(table2);
using(SqlCommand cmd = new SqlCommand("SELECT * FROM table1;SELECT * from table2", con))
{
    using(SqlDataReader dr = cmd.ExecuteReader())
    {
        dataSet.Load(dr, LoadOption.OverwriteChanges, table1, table2);
    }
}

Now you can work with the DataSet's Tables like this (msdn documentation):

private void PrintRows(DataSet dataSet)
{
    // For each table in the DataSet, print the row values.
    foreach(DataTable table in dataSet.Tables)
    {
        foreach(DataRow row in table.Rows)
        {
            foreach (DataColumn column in table.Columns)
            {
                Console.WriteLine(row[column]);
            }
        }
    }
}
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
0

Take a look at SQL command UNION. Then you can make something like

SELECT * FROM table1
UNION
SELECT * FROM table2;
campovski
  • 2,979
  • 19
  • 38