Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have two tables I use.

1.TABLE => temptablestudent(ıd,name,surname,class)
2.TABLE => student(ıd,name,surname,class)
Explain question
if you have different int you want to translate how many there is and want to add different ones from the temp table to the student table.
and if the tables are the same, when I press the add button, I want to compare and press add button, but not add.

ı use sqlcommand.
var x=select ıd,name,surname from temptablestudent except  ıd,name,surname from student

//how do I find the result value from type int.

if the result returns I say add larger than 0


INSERT INTO student (ıd,name,surname,class)(select select ıd,name,surname from temptablestudent except ıd,name,surname from student)

//If there is the same in the table, I want to not add the add button.in this case how do I write query

What I have tried:

I want to

I'm missing a query.

1. I cannot convert sql command x to int. //how do I find the result value from type int.

2. If the same table tempstudent with the student add button I want to add even bass.
If there is the same in the table, I want to not add the add button.in this case how do I write query

How can I do it.
Posted
Updated 1-Aug-19 5:10am
Comments
MadMyche 31-Jul-19 21:36pm
   
Add some sample data and what you expect from that data
EngineerStudentSC 1-Aug-19 0:24am
   
Sample data:
1 john alex


Expect data:

1 john alex

Sample data2:

1 john alex
2 mary alex

Expect data:(ınsert data)

2 mary alex

So finally student data:

1 john alex
2 mary alex
Wendelius 1-Aug-19 3:13am
   
Few questions.
1. Is the insert query working as expected and the only problem is to know how many rows were inserted?
2. Are you using plain SQL or using SQL inside some other programming language, like C#?
3. What database you're using, SQL Server, MySql, Oracle, ...?
EngineerStudentSC 1-Aug-19 15:10pm
   
I use SQL.And sql language.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I am going to rephrase your question and add in your comments so that others can follow along

I have 2 tables Student and TempStudent with identical schemas
CREATE TABLE dbo.Student (
  ID      int,
  Name    nvarchar(32),
  Surname nvarchar(32)
) ON [PRIMARY]
How do I transfer the rows from the second table (TempStudent) into the first (Student) when the record does not exist?

Sample Data #1
Student table is empty, and TempStudent table has one row:
TRUNCATE TABLE Student;
INSERT TempStudent(ID, [Name], Surname)
VALUES  (1, 'John', 'Alex)
I need the query to transfer this row

Sample Data #2
Student table should now have row#1 in it, and we have added a row to TempStudent
INSERT TempStudent(ID, [Name], Surname)
VALUES  (2, 'Mary', 'Alex)
Again, I need the query to transfer this new row to Student, but only the new row


The solution is quite simple:
INSERT Student(ID, [Name], Surname)
SELECT ID, [Name], Surname
FROM   TempStudent
WHERE  ID NOT IN (SELECT ID FROM Student)
   
Comments
EngineerStudentSC 1-Aug-19 15:11pm
   
Hello.I want to
If you have the same id value in the table.
So do not add the same record over and over again from the same id.
If not, I don't want to add.

For example:

temptable:

1 John Alex
2 Mary Alex
if again same 2 two ıd insert (1,John,Alex and 2,Mary,Alex),
if the same records will be added again if the query is the same I need to say.
I need to look at the id of you saying.

But (3,Harry,Alex) add record to add.
MadMyche 1-Aug-19 16:32pm
   
This should take care of this- the subquery in the WHERE clause
WHERE  ID NOT IN (SELECT ID FROM Student)
says to grab everything from the Temp table that does not have the corresponding ID in the Student table

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100