Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insertion instead of updating

Author  Topic 

sparrow37
Posting Yak Master

148 Posts

Posted - 2009-09-29 : 01:58:11
Hi all,

I have following stored proceudre which has two problems

1) inserts duplicate values instead of unique
2) and when i want to update, it inserts instead of updating.

SQL is :

ALTER PROCEDURE [dbo].[MapPartTypeManufacturer]
(
@CustomerId INT,
@PartType VARCHAR(20),
@ManufacturerId INT,
@ManufacturerName VARCHAR(200)
)

AS
BEGIN

IF EXISTS (SELECT * FROM tblAutoManufacturers WHERE iCustomerId = @CustomerId
and iManufacturerId=@ManufacturerId and sPartType = @PartType
)
BEGIN
UPDATE
tblAutoManufacturers
SET
sPartType = @PartType,
iManufacturerId = @ManufacturerId,
sManufacturer = @ManufacturerName
WHERE
sPartType = @PartType and
iCustomerId = @CustomerId and
iManufacturerID=@ManufacturerId
END
ELSE
BEGIN
INSERT INTO
tblAutoManufacturers
(iCustomerId, sManufacturer, iManufacturerId,sPartType)
VALUES (@CustomerId, @ManufacturerName, @ManufacturerId, @PartType)
END

END

for example the problem i have let me give you one scenario for customer 1 for same parttype but for different manufacturer name,
it should add new record but when i want to edit this by changing the manufacturer name, it should update the existing record
but what it does is, it adds a new record.

Please help me on this urgently,

Regards,
Asif Hameed



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-29 : 05:55:52
quote:
Originally posted by sparrow37

Hi all,

I have following stored proceudre which has two problems

1) inserts duplicate values instead of unique
2) and when i want to update, it inserts instead of updating.

SQL is :

ALTER PROCEDURE [dbo].[MapPartTypeManufacturer]
(
@CustomerId INT,
@PartType VARCHAR(20),
@ManufacturerId INT,
@ManufacturerName VARCHAR(200)
)

AS
BEGIN

IF EXISTS (SELECT * FROM tblAutoManufacturers WHERE iCustomerId = @CustomerId
and sPartType = @PartType
)
BEGIN
UPDATE
tblAutoManufacturers
SET
sPartType = @PartType,
iManufacturerId = @ManufacturerId,
sManufacturer = @ManufacturerName
WHERE
sPartType = @PartType and
iCustomerId = @CustomerId
END
ELSE
BEGIN
INSERT INTO
tblAutoManufacturers
(iCustomerId, sManufacturer, iManufacturerId,sPartType)
VALUES (@CustomerId, @ManufacturerName, @ManufacturerId, @PartType)
END

END

for example the problem i have let me give you one scenario for customer 1 for same parttype but for different manufacturer name,
it should add new record but when i want to edit this by changing the manufacturer name, it should update the existing record
but what it does is, it adds a new record.

Please help me on this urgently,

Regards,
Asif Hameed






seeing your explanation i think what you need is above
Go to Top of Page
   

- Advertisement -