Click here to Skip to main content
15,890,438 members
Home / Discussions / Database
   

Database

 
AnswerRe: check the key value before inserting data Pin
Eddy Vluggen29-Jun-11 2:50
professionalEddy Vluggen29-Jun-11 2:50 
GeneralRe: check the key value before inserting data Pin
PIEBALDconsult29-Jun-11 14:32
mvePIEBALDconsult29-Jun-11 14:32 
AnswerRe: check the key value before inserting data Pin
Eddy Vluggen29-Jun-11 23:11
professionalEddy Vluggen29-Jun-11 23:11 
GeneralRe: check the key value before inserting data Pin
PIEBALDconsult30-Jun-11 3:01
mvePIEBALDconsult30-Jun-11 3:01 
QuestionRe: check the key value before inserting data Pin
Eddy Vluggen30-Jun-11 9:24
professionalEddy Vluggen30-Jun-11 9:24 
AnswerRe: check the key value before inserting data Pin
PIEBALDconsult1-Jul-11 14:16
mvePIEBALDconsult1-Jul-11 14:16 
GeneralRe: check the key value before inserting data Pin
Eddy Vluggen2-Jul-11 0:10
professionalEddy Vluggen2-Jul-11 0:10 
AnswerRe: check the key value before inserting data [modified]---ALREADY SOLVED--- Pin
Niladri_Biswas3-Jul-11 23:49
Niladri_Biswas3-Jul-11 23:49 
Hello SayamiSuchi,
This problem can be solved in many ways.

Method 1: As has already been answered by UNCRushFan

IF NOT EXISTS (SELECT DISTINCT keyname FROM <table_name>)
  BEGIN 
    INSERT INTO.....
  END


Method 2: Another traditional way [ Count(ColumnName) approach ]
Declare @tblSource table([ID] int identity,[Name] varchar(20),[Key] varchar(10))
insert into @tblSource Select 'Sam', 'Sam1' Union all Select 'Joy','Joy2'
--Select * from @tblSource

Declare @key as varchar(10)
set @key  = 'Joy2'

if(( Select count([KEY]) from @tblSource where [key] = @key) = 1)
begin
insert into @tblSource Select 'NewName', 'NewKey'
end 

Select * from @tblSource


Method 3: Merge statement of Sql Server 2008

Consider the below statement first

Declare @tblSource table([ID] int identity,[Name] varchar(20),[Key] varchar(10))
insert into @tblSource Select 'Sam', 'Sam1' Union all Select 'Joy','Joy2' 

Declare @tblDestination table([ID] int identity,[Name] varchar(20),[Key] varchar(10))

MERGE INTO @tblDestination AS Target
USING (SELECT [Name],[Key] FROM @tblSource) AS Source
ON Target.[Key] = Source.[Key]
WHEN NOT MATCHED BY TARGET THEN
	INSERT (Name, [Key]) VALUES (Source.Name, Source.[Key]);	

Select * from @tblDestination


I have a source table and a destination table. I am inserting the record in the destination table by checking whether the Key of the source table exists in the destination or not. In this case no key will be found in the destination and hence the output will be

ID	Name	Key
1	Sam	Sam1
2	Joy	Joy2


Now let us insert a record (with an already existing key) in the source table as

insert into @tblSource Select 'Joy','Joy2' 


At this time the records in the source table will be

ID	Name	Key
1	Sam	Sam1
2	Joy	Joy2
3	Joy	Joy2


Now if we run the above Merge statement query

MERGE INTO @tblDestination AS Target
USING (SELECT [Name],[Key] FROM @tblSource) AS Source
ON Target.[Key] = Source.[Key]
WHEN NOT MATCHED BY TARGET THEN
	INSERT (Name, [Key]) VALUES (Source.Name, Source.[Key]);
Select * from @tblDestination


The output in the destination table will be

ID	Name	Key
1	Sam	Sam1
2	Joy	Joy2


because the key is already present in the destination table and hence it has been ignored.

But if we have the source table as

insert into @tblSource Select 'NewName','NewKey' 


i.e.

ID	Name	Key
1	Sam	Sam1
2	Joy	Joy2
3	NewName	NewKey


and then execute the above query, the output will be

ID	Name	Key
1	Sam	Sam1
2	Joy	Joy2
3	NewName	NewKey


Because this time we are inserting a new key altogether.

So the query will be

MERGE INTO @tblDestination AS Target
USING (SELECT [Name],[Key] FROM @tblSource) AS Source
ON Target.[Key] = Source.[Key]
WHEN NOT MATCHED BY TARGET THEN
	INSERT (Name, [Key]) VALUES (Source.Name, Source.[Key]);


N.B.~ This will work in Sql Server 2008 and above

Hope this helps

Thanks
Niladri Biswas

QuestionSSRS Help Pin
Aptiva Dave28-Jun-11 3:31
Aptiva Dave28-Jun-11 3:31 
AnswerRe: SSRS Help Pin
Mycroft Holmes28-Jun-11 18:09
professionalMycroft Holmes28-Jun-11 18:09 
GeneralRe: SSRS Help Pin
Aptiva Dave29-Jun-11 8:25
Aptiva Dave29-Jun-11 8:25 
QuestionRun Package Parameters Pin
Groulien27-Jun-11 3:36
Groulien27-Jun-11 3:36 
QuestionAssist to solve this problem? [modified] Pin
current199924-Jun-11 22:41
current199924-Jun-11 22:41 
AnswerRe: Assist to solve this problem? Pin
Blue_Boy25-Jun-11 1:10
Blue_Boy25-Jun-11 1:10 
AnswerRe: Assist to solve this problem? Pin
current199925-Jun-11 1:22
current199925-Jun-11 1:22 
AnswerRe: Assist to solve this problem? Pin
Mycroft Holmes25-Jun-11 2:35
professionalMycroft Holmes25-Jun-11 2:35 
GeneralRe: Assist to solve this problem? Pin
current199925-Jun-11 3:22
current199925-Jun-11 3:22 
GeneralRe: Assist to solve this problem? Pin
Mycroft Holmes25-Jun-11 3:34
professionalMycroft Holmes25-Jun-11 3:34 
GeneralRe: Assist to solve this problem? Pin
current199925-Jun-11 6:19
current199925-Jun-11 6:19 
GeneralRe: Assist to solve this problem? PinPopular
Mycroft Holmes25-Jun-11 13:06
professionalMycroft Holmes25-Jun-11 13:06 
GeneralRe: Assist to solve this problem? Pin
Peter_in_278026-Jun-11 14:50
professionalPeter_in_278026-Jun-11 14:50 
GeneralRe: Assist to solve this problem? Pin
Mycroft Holmes26-Jun-11 14:56
professionalMycroft Holmes26-Jun-11 14:56 
GeneralRe: Assist to solve this problem? Pin
David Skelly26-Jun-11 22:29
David Skelly26-Jun-11 22:29 
AnswerRe: Assist to solve this problem? Pin
Blue_Boy25-Jun-11 21:42
Blue_Boy25-Jun-11 21:42 
GeneralRe: Assist to solve this problem? [modified] Pin
current199917-Jul-11 21:48
current199917-Jul-11 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.