Click here to Skip to main content
15,885,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,


I have a table named 'Map' with column as ids and shift. then my input values to the stored procedure are 'ids' and 'shift'.

example:
ids=106,101,200
shift=1


Then i want to check each id already exists in table Map. If it exist then delete else

add each id and shift value in a for loop. the table look as below.

SQL
ids       shift
---       -----
106         1
101         1
200         1



Thanks
Posted
Updated 21-Jul-13 23:51pm
v2

You can only add 1 row at a time so in the Loop you have to call the stored procedure with the neccessary parameters:
Read This[^] on how to create a Stored Procedure.
 
Share this answer
 
Comments
Herman<T>.Instance 22-Jul-13 5:58am    
We are not here to make code for you. You better share the code you already have so we can help you out.
Maciej Los 22-Jul-13 14:26pm    
"You can only add 1 row at a time" - It's not true. What about: INSERT ... SELECT...?
See my answer ;)
Herman<T>.Instance 23-Jul-13 10:00am    
parameterized you cannot because it is external tothe db INSERT...SELECT is internal to the db
If you turn your collection of shift and ID into an XML string then you can pass that to the stored proc and use select from xml to insert the data. I would use a temp table to get at the IDs so you can delete the existing ones.

Do a search for inserting multiple entries into SQL Server or inserting xml.
 
Share this answer
 
There is nothing hard to do. You should try first.

The skeleton is always the same ;)
SQL
CREATE PROCEDURE ProcedureName
    @ids VARCHAR(30),
    @shift INT
AS
BEGIN
--the body of the procedure
END


I was wondering why do you want to delete records rather than update existing ones... I would suggest you to re-think this part of your project.

Have a look at example:

SQL
--MAP table
DECLARE @map TABLE (ID INT, Shift INT)
--example data
INSERT INTO @map (ID, Shift)
SELECT 106, 1
UNION ALL SELECT 101, 1
UNION ALL SELECT 200, 1
UNION ALL SELECT 121, 1
UNION ALL SELECT 220, 1
UNION ALL SELECT 122, 1
UNION ALL SELECT 222, 1
UNION ALL SELECT 301, 1
UNION ALL SELECT 203, 1
UNION ALL SELECT 103, 1
UNION ALL SELECT 233, 1

--show example data; uncomment below lines to show result
--SELECT *
--FROM @map
--ORDER BY ID

--collection of id's separated by comma
--in your case it would be a parameter of SP
DECLARE @ids VARCHAR(30)

SET @ids = '106,101,200'

--temporary table
DECLARE @tmp TABLE (ID INT)
--get id's from varchar variable and insert into temporary table
;WITH IDCollection AS
(
	SELECT CONVERT(INT, LEFT(@ids, CHARINDEX(',', @ids)-1)) AS ID, RIGHT(@ids,LEN(@ids) - CHARINDEX(',', @ids)) AS Remainder
	WHERE CHARINDEX(',', @ids)>0
	UNION ALL
	SELECT CONVERT(INT,LEFT(Remainder, CHARINDEX(',', Remainder)-1)) AS ID, RIGHT(Remainder,LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
	FROM IDCollection
	WHERE CHARINDEX(',', Remainder)>0
	UNION ALL
	SELECT CONVERT(INT,Remainder) AS ID, NULL AS Remainder
	FROM IDCollection
	WHERE CHARINDEX(',', Remainder)=0
)
INSERT INTO @tmp (ID)
SELECT ID
FROM IDCollection

--delete data based on passed id's
DELETE 
FROM @map 
WHERE ID IN(SELECT ID FROM @tmp)

--insert new data
INSERT INTO @map (ID, Shift) 
SELECT ID, 1 AS Shift
FROM @tmp 

--show data 
SELECT *
FROM @map


As i mentioned above, better way is to update existing records instead deleting and adding new ones.
SQL
UPDATE @map SET Shift = @Shift
WHERE ID IN (SELECT ID FROM @tmp)
 
Share this answer
 
Comments
Mycroft Holmes 23-Jul-13 1:32am    
Delete and insert is faster than update under certain circumstances so the approach may not be invalid.
Maciej Los 23-Jul-13 2:36am    
I'm not sure that delete and insert is faster than update. It is always two commands instead one ;)
Herman<T>.Instance 24-Jul-13 6:20am    
update is in fact a delete and an insert.
You have no try catching mechanism in case of errors. Risky processing
Maciej Los 24-Jul-13 6:49am    
Dear @digimanus,
Does it make my answer completely unusable? No. Of course, i should write that OP need to use transaction, but... is this a place to write complete solutions? No. This is a place where we post Quick Answers. My example is based on temporary data, so why should i care for error handling? If i'm not agree with your answer, i do not down-vote it unless ...

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