Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I am stuck up with this query.
I want to insert 4 records in to the table for that am using the below query
SQL
IF NOT EXISTS(SELECT WS.ScheduleID FROM WaitingSchedules WS,@waitingSchedules_temp WST WHERE WST.ScheduleID = WS.ScheduleID)
            INSERT INTO WaitingSchedules SELECT ScheduleID,AppointmentStatus,InDt,OutDt,HasUpdated FROM @waitingSchedules_temp

Works fine for the scenario
If all the 4 records are not exist in the table then 4 records are inserted successfully.

Fails in the below scenario<br />
If first records is exist in table then remaining first and remaining 3 records are failed to insert in table.


Expected: I want to insert 4th record even if first/second/third id is exist in the table.Which means I want to insert a new record leaving previously existing records
I tried this
SQL
WHILE (@Count < @RowCount)
BEGIN

    IF NOT EXISTS (SELECT WS.ScheduleID FROM WaitingSchedules WS,@waitingSchedules_temp WST WHERE WST.ScheduleID = WS.ScheduleID)
    INSERT INTO WaitingSchedules SELECT ScheduleID,AppointmentStatus,InDt,OutDt,HasUpdated FROM @waitingSchedules_temp

    SET @Count = @Count + 1;
END

And also this
SQL
WHILE (NOT EXISTS(SELECT WS.ScheduleID FROM WaitingSchedules WS,@waitingSchedules_temp WST WHERE WST.ScheduleID = WS.ScheduleID))
            INSERT INTO WaitingSchedules SELECT ScheduleID,AppointmentStatus,InDt,OutDt,HasUpdated FROM @waitingSchedules_temp

But this also failed :(

Sample input:

I have 4 records like 6589, 6857, 1220, 4500
Now I want to insert into WaitingStatus Table by checking if the records are pre exist.
Working scenario:
If Waitingschedules table doesnt have all the 4 records then its inserting all successfully.

Failing scenario:

If Waitingschedules table has 6589(first record) then remaining records are not inserting even remaining records are not exist.


Please help me on this issue.

Thanks,
RK
Posted
Updated 19-Dec-13 2:34am
v5
Comments
Maciej Los 19-Dec-13 8:28am    
RK, please provide sample data and expected outtput. It would be easier to understand.
♥…ЯҠ…♥ 19-Dec-13 8:31am    
Sure Maciej

Hi,

I optimized the query further to insert not existed records,

Thanks Maciej Los for NOT IN concept(Wondering why I didnt think that ;-))
This is the query that I used

SQL
INSERT INTO WaitingSchedules SELECT ScheduleID,AppointmentStatus,InDt,OutDt,HasUpdated
            FROM @waitingSchedules_temp AS tmp
            WHERE (tmp.ScheduleID NOT IN (SELECT ScheduleID FROM WaitingSchedules))

Regards,
RK
 
Share this answer
 
Have a look at example:
SQL
DECLARE @ws TABLE(ID INT)

INSERT INTO @ws (ID)
VALUES(6589)

SELECT ID
FROM @ws

INSERT INTO @ws (ID)
SELECT ID
FROM (
    SELECT 6589 AS ID UNION ALL
    SELECT 6857 AS ID UNION ALL
    SELECT 1220 AS ID UNION ALL
    SELECT 4500 AS ID
) AS T
WHERE ID NOT IN(SELECT ID FROM @ws)

SELECT ID
FROM @ws



Results:
1.query - before insert
ID
6589


2.query - after insert
ID
6589
6857
1220
4500


Using CTE and list of ID's as a varchar variable:
SQL
DECLARE @ValuesToInsert VARCHAR(300) = '6589,6857,1220,4500'

--use CTE to split values
;WITH VTI AS 
(
	SELECT CONVERT(INT, LEFT(@ValuesToInsert, CHARINDEX(',', @ValuesToInsert)-1)) AS TID, RIGHT(@ValuesToInsert, LEN(@ValuesToInsert) - CHARINDEX(',', @ValuesToInsert)) AS Remainder
	WHERE CHARINDEX(',', @ValuesToInsert)>0
	UNION ALL
	SELECT CONVERT(INT, LEFT(Remainder, CHARINDEX(',', Remainder)-1)) AS TID, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
	FROM VTI
	WHERE CHARINDEX(',', Remainder)>0
	UNION ALL
	SELECT CONVERT(INT,Remainder) AS TID, NULL AS Remainder
	FROM VTI
	WHERE CHARINDEX(',', Remainder)=0
)
INSERT INTO @ws (ID)
SELECT ID 
FROM 
	(
	SELECT TID AS ID
	FROM VTI
	) AS T
WHERE ID NOT IN (SELECT ID FROM @ws)

SELECT ID
FROM @ws


Have a look at OriginalGriff's tip: Using comma separated value parameter strings in SQL IN clauses[^]
 
Share this answer
 
v3
Comments
♥…ЯҠ…♥ 19-Dec-13 9:22am    
Thanks Maciej, if I dont know the number of records then?
Maciej Los 19-Dec-13 9:23am    
Logic is the same ;)
♥…ЯҠ…♥ 19-Dec-13 9:27am    
I'll try and update you soon.... :)
♥…ЯҠ…♥ 19-Dec-13 9:59am    
5+ thanks for the logic Maciej ;-)
Maciej Los 19-Dec-13 10:16am    
It would be nice when you accept my answer as a solution - formally.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900