|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, April 23, 2013 1:43 AM
Points: 71,
Visits: 59
|
|
CREATE TABLE [dbo].[LKP_PARAM_TYPE]( [PARAM_TYPE_ID] [int] NOT NULL, [PARAM_DESC] [varchar](50) NOT NULL, CONSTRAINT [XPKLKP_PARAM_TYPE] PRIMARY KEY CLUSTERED ( [PARAM_TYPE_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
CREATE TABLE [dbo].[SRC_DOM_RETAIL_AIRB_PARAM]( [RISK_PARAMETERS] [varchar](20) NOT NULL, [A_WEIGHT_IN_WITHOUT_FACTOR] [numeric](18, 2) NOT NULL ) ON [PRIMARY]
INSERT SRC_DOM_RETAIL_AIRB_PARAM(RISK_PARAMETERS,A_WEIGHT_IN_WITHOUT_FACTOR) VALUES('pd_ec','0.00') INSERT SRC_DOM_RETAIL_AIRB_PARAM(RISK_PARAMETERS,A_WEIGHT_IN_WITHOUT_FACTOR) VALUES('pd_rc','0.00') INSERT SRC_DOM_RETAIL_AIRB_PARAM(RISK_PARAMETERS,A_WEIGHT_IN_WITHOUT_FACTOR) VALUES('eadf_ec','0.00') INSERT SRC_DOM_RETAIL_AIRB_PARAM(RISK_PARAMETERS,A_WEIGHT_IN_WITHOUT_FACTOR) VALUES('eadf_rc','0.00') INSERT SRC_DOM_RETAIL_AIRB_PARAM(RISK_PARAMETERS,A_WEIGHT_IN_WITHOUT_FACTOR) VALUES('lgd_ec','1.00') INSERT SRC_DOM_RETAIL_AIRB_PARAM(RISK_PARAMETERS,A_WEIGHT_IN_WITHOUT_FACTOR) VALUES('lgd_rc','0.00')
DECLARE @MAX_ID INT SELECT @MAX_ID = MAX(PARAM_TYPE_ID) FROM LKP_PARAM_TYPE IF @MAX_ID IS NULL SET @MAX_ID=0
MERGE INTO LKP_PARAM_TYPE AS C USING ( SELECT DISTINCT RISK_PARAMETERS FROM SRC_DOM_RETAIL_AIRB_PARAM )X ) AS CT ON C.PARAM_DESC = CT.RISK_PARAMETERS WHEN NOT MATCHED THEN INSERT(PARAM_TYPE_ID,PARAM_DESC) VALUES(@MAX_ID+1,CT.RISK_PARAMETERS);
Hi .. I have a Primary key column[PARAM_TYPE_ID] which is not identity.. i wanna set it to increment automatically using merge...Please help me in this regard..Its throwing up an error
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 4:40 AM
Points: 2,198,
Visits: 4,148
|
|
greeshma.patla (9/11/2012)
MERGE INTO LKP_PARAM_TYPE AS C USING ( SELECT DISTINCT RISK_PARAMETERS FROM SRC_DOM_RETAIL_AIRB_PARAM )X -- I think this bracket is the problem, remove it ) AS CT ON C.PARAM_DESC = CT.RISK_PARAMETERS WHEN NOT MATCHED THEN INSERT(PARAM_TYPE_ID,PARAM_DESC) VALUES(@MAX_ID+1,CT.RISK_PARAMETERS);
This looks like a problem with closing brackets, I have put a comment on the code above. If this does not solve your problem, please post the error that you are receiving
I would also like to say that IDENTITY is the best way to achieve your requirement Can you tell us why you are avoiding the IDENTITY property?
Kingston Dhasian
How to post data/code on a forum to get the best help - Jeff Moden http://www.sqlservercentral.com/articles/Best+Practices/61537/
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 12:42 AM
Points: 2,338,
Visits: 3,158
|
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, April 23, 2013 1:43 AM
Points: 71,
Visits: 59
|
|
Msg 2627, Level 14, State 1, Line 10 Violation of PRIMARY KEY constraint 'XPKLKP_PARAM_TYPE'. Cannot insert duplicate key in object 'dbo.LKP_PARAM_TYPE'. The statement has been terminated.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, April 23, 2013 1:43 AM
Points: 71,
Visits: 59
|
|
DBA team defined the structure and we are not supposed to change it as it is in production..
SELECT @MAX_ID = MAX(PARAM_TYPE_ID) FROM LKP_PARAM_TYPE IF @MAX_ID IS NULL SET @MAX_ID=0 MERGE INTO LKP_PARAM_TYPE AS C USING ( SELECT RISK_PARAMETERS ,ROW_NUMBER()OVER(ORDER BY RISK_PARAMETERS)+@MAX_ID AS ID FROM ( SELECT DISTINCT RISK_PARAMETERS FROM SRC_DOM_RETAIL_AIRB_PARAM )X ) AS CT ON C.PARAM_DESC = CT.RISK_PARAMETERS WHEN NOT MATCHED THEN INSERT(PARAM_TYPE_ID,PARAM_DESC) VALUES(ID,CT.RISK_PARAMETERS); I have gt this idea suggest me if i can do it in a better way .........
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 10:59 AM
Points: 2,525,
Visits: 4,324
|
|
I have gt this idea suggest me if i can do it in a better way .........
The much better way would be to contact DBA (if you cannot change it yourself) and advise that you need identity there. Dwain is absolutely right. Instead of implementing the square-weels bicycle you better use in-build feature of SQL Server designed exactly for the purpose.
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 4:40 AM
Points: 2,198,
Visits: 4,148
|
|
greeshma.patla (9/11/2012) Msg 2627, Level 14, State 1, Line 10 Violation of PRIMARY KEY constraint 'XPKLKP_PARAM_TYPE'. Cannot insert duplicate key in object 'dbo.LKP_PARAM_TYPE'. The statement has been terminated.
With this approach, I am sure you will get this error again and again Just imagine what would happen if somebody failed to update the PARAM_TYPE_ID column in LKP_PARAM_TYPE table with the latest value( you will get the error again )
greeshma.patla (9/11/2012) I have gt this idea suggest me if i can do it in a better way .........
Yes. As others have also said "Use IDENTITY".
Kingston Dhasian
How to post data/code on a forum to get the best help - Jeff Moden http://www.sqlservercentral.com/articles/Best+Practices/61537/
|
|
|
|