Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Add records from one table to another
Message
De
06/07/2017 15:06:17
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01652499
Message ID:
01652513
Vues:
67
>Hi,
>
>I would like to create a SQL Insert (for SQL Server DB) that would add records from one table to another and automatically set fields of certain values. Here is a simplified example:
>
>The table which now has records. Name TENANT
>Fields: TEN_PK, USER_ID, USER_PWRD
>
>The table where the records should be added: Name TEN_NAME
>Fields: TEN_PK, USER_ID, USER_PWRD (basically the same field names)
>
>Example:
>TENANT:
>TEN_PK, USER_ID, USER_PWRD
>1 "ABC" "123"
>2 "CBS" "444"
>3 "NBC" "9933"
>
>After this SQL Insert the table TEN_NAMES will have the fields with the same values.
>
>I am starting to write a SQL Insert but get lost quickly. I would appreciate any help.

Simply use a not exists query:
insert into TEN_NAMES (TEN_PK, USER_ID, USER_PWRD)
select TEN_PK, USER_ID, USER_PWRD
from TENANT t1 
where not exists (select * from TEN_NAMES t2 where t1.TEN_PK = t2.TEN_PK);
PS: If TEN_PK is IDENTITY then simply remove it from the insert list (and also select).
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform