-1

I have two tables:

  • Table 1 with columns name_markaz nvarchar(100), code_markaz nchar(20);
  • Table 2 with columns name_markaz nvarchar(100), code_markaz nchar(20);

I want implement this plan, read all data from table 1 and insert into table 2 with this condition:

if table1.code_markaz not found in table 2.code_markaz then

   insert the table1.code_markaz into the table2.code_markaz
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
behzad razzaqi
  • 1,503
  • 2
  • 18
  • 33

1 Answers1

1

I think this query can do that (I prefer using EXISTS):

INSERT INTO table2 (name_markaz, code_markaz)
   SELECT name_markaz, code_markaz 
   FROM table1
   WHERE NOT EXISTS (SELECT 1
                     FROM table2 ti
                     WHERE ti.code_markaz = table1.code_markaz);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shA.t
  • 16,580
  • 5
  • 54
  • 111