0

I am coding with Visual Studio 2013 and SQL Server 2012. Sometimes we have to use the data in result tables for our code. As an example, if we compare two tables using SELECT, EXCEPT method, we can see the differentiated entries between those two tables in result window as a another table.

So, I asked, if I need to add that result data into a table in my database, how can I do it? Is it possible or not and are there any method to do it?

Sample

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anj Desaan
  • 11
  • 2
  • Can you also post what have you tried to do to get your desired result? – Mr.J Jun 11 '19 at 03:19
  • What I am doing here is, comparing two tables and take the difference entries of those tables in result box in Visual Studio 2013. so i need to use those data in effective way inside my program. – Anj Desaan Jun 12 '19 at 04:54

1 Answers1

0

If you are asking for SQL, you can do something like this for table that already created:

INSERT INTO NEW_TABLE (col1,col2,...)
SELECT col1,col2,... 
FROM YOUR_TABLE 
EXCEPT SELECT ...

Or if you want to create a new table using that query without specifying each columns (copy the column specs from the source table), you can do something like this:

SELECT ... 
INTO NEW_TABLE 
FROM YOUR_TABLE
EXCEPT 
SELECT ...
FROM ANOTHER_TABLE

If your new table want to drop after you have processed with your data, consider #temptable or @tablevariable.

But if you just want to do some processing in the resulting table but do not want to store it in database, I recommend using DataTable in .NET by doing something like this:

using System;
using System.Data;
//...
DataTable dttbl = new DataTable();
dttbl.TableName = "YOUR_TABLE";
dttbl.Load(new SqlCommand("SELECT ...",sqlConnection).ExecuteReader());
//process your datatable using indexing like this 
dttbl.Rows[(row_number (int))][(column_name (string), column_position (int))] = "Processed";
dttbl.Rows[0]["col1"] = "Processed"; //This line will change the value in cell of the first record in column named "col1" to "Processed". 

EDIT*: More about dataTable

To print out a whole DataTable

//Print out the columns name
for(int i=0;i<dttbl.Columns.Count;i++)
    Console.Write(dttbl.Columns[i].ColumnName + " ");
Console.Write("\n");
//Print out the data
for(int i=0;i<dttbl.Rows.Count;i++)
{
    for(int k=0;k<dttbl.Columns.Count;k++)
    {
        Console.Write(dttbl.Rows[i][k] + " ");
    }
    Console.Write("\n");
}

Note: SQL is database query language while C#,VB or other .NET language is programming language. In short, database query language is a way for programming language to communicate with database.

LEE Hau Chon
  • 435
  • 3
  • 12
  • Thank you for your responding. I tried first two methods and it was worked. But in last method, I think I did not figure out really well. Because this is first time I am working with SQL, Visual Studio, SQL server kind of things. I have no such a idea about what is the .NET. But I realized that third method is more fit to my work, because all I need is to print that difference between two tables. – Anj Desaan Jun 12 '19 at 05:00
  • I've added some codes to printout the whole dataTable. Hope it helps. Actually, I only start using .NET 3 months ago. Based on my understanding, .NET is a framework created by Microsoft to make programmers' work easier. Languages that utilize the .NET are C#, Visual Basic etc. The codes I typed in the answer is in C#. If you are not familiar with C /C++ or OO programming, I recommend using Visual Basic is better since it is more human readable (it need no semicolon). There is online converter for both language. Lastly, please mark my answer as true if it helped you. It mean a lot to me.Thanks. – LEE Hau Chon Jun 12 '19 at 05:49