sqlcurious
asked on
Need to reporgam in such a way that I dont overwrite rest of the records
Hi experts in the below query, I am actually overwriting all the records if any one customer number exists, please help corrct....
CREATE PROCEDURE [dbo].[CustomerMagSubscrip tionSetFla g]
@p01_CustomerNo INT ,
@p02_OptInSel VARCHAR(5),
@p03_BirthMonthSel VARCHAR(9)
AS
SET NOCOUNT ON
BEGIN
-- Since all r new customers, insert the customer name, whether opted for a magazine or not --and the birth month selected on the website(this
-- serves more like personally identifying question and not necessarily the correct birth month , if the customer already exists overwrite with the entered optin and birthmonth
IF @p01_CustomerNo IN (SELECT Customer_No FROM G..CustMagSel)
BEGIN
UPDATE G..CustMagSel
SET Customer_No = @p01_CustomerNo,
OptInSel =
CASE
WHEN @p02_OptInSel like '%Y%' THEN 'YES'
ELSE 'NO'
END,
BirthMonthSel= @p03_BirthMonthSel
END
ELSE
INSERT INTO G..CustMagSel
SELECT Customer_No = @p01_CustomerNo,
OptInSel =
CASE
WHEN @p02_OptInSel like '%Y' THEN 'YES'
ELSE 'NO'
END,
BirthMonthSel= @p03_BirthMonthSel
END
CREATE PROCEDURE [dbo].[CustomerMagSubscrip
@p01_CustomerNo INT ,
@p02_OptInSel VARCHAR(5),
@p03_BirthMonthSel VARCHAR(9)
AS
SET NOCOUNT ON
BEGIN
-- Since all r new customers, insert the customer name, whether opted for a magazine or not --and the birth month selected on the website(this
-- serves more like personally identifying question and not necessarily the correct birth month , if the customer already exists overwrite with the entered optin and birthmonth
IF @p01_CustomerNo IN (SELECT Customer_No FROM G..CustMagSel)
BEGIN
UPDATE G..CustMagSel
SET Customer_No = @p01_CustomerNo,
OptInSel =
CASE
WHEN @p02_OptInSel like '%Y%' THEN 'YES'
ELSE 'NO'
END,
BirthMonthSel= @p03_BirthMonthSel
END
ELSE
INSERT INTO G..CustMagSel
SELECT Customer_No = @p01_CustomerNo,
OptInSel =
CASE
WHEN @p02_OptInSel like '%Y' THEN 'YES'
ELSE 'NO'
END,
BirthMonthSel= @p03_BirthMonthSel
END
ASKER
yes but some how getting syntax error
You'll have to copy-paste the T-SQL that is throwing the error into the question.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
Guessing you want to limit the rows that are UPDATED by including a WHERE clause?