Avatar of fsuedu
fsuedu

asked on 

SQL INSERT SELECT Error

With the statment below--I get a error message:

INSERT INTO [SPIResConv5].[dbo].[Transactions]
           ([TRXNO],
            [RESORT_ID]
           ,[TRANSTYPE_ID]
           ,[BILLCODE_ID]
           ,[MAINTENANCE_ID]
           ,[CONTACT_ID]
           ,[POSTED]
           ,[DATE]
           ,[USER_ID]
           ,[BATCH]
           ,[TYPE]
           ,[AMOUNT]
           ,[PAYMENTCODE_ID]
           ,[BANKCODE_ID]
           ,[DOCNO]
           ,[Shift4Trx]
           )
Select
  c.cnt + row_number() over (partition by t.resort_id order by t.resort_id, t.contact_id) as trxno,
       t.Resort_ID,
       TRANSTYPE_ID,      
       BILLCODE_ID,
       MAINTENANCE_ID,
       Contact_ID,
       Posted,
       Date,
       [USER_ID],
       BATCH,
       [Type],
       Amount,
       PAYMENTCODE_ID,
       BANKCODE_ID,
       DOCNO,
       [Shift4Trx]
from   TransactionsTemp t
  left join
  (select Resort_ID, count(*) as cnt from TransactionsTemp group by Resort_ID) c
  on t.Resort_ID = c.Resort_ID


Error Message:

Violation of PRIMARY KEY constraint 'PK_Transactions'. Cannot insert duplicate key in object 'dbo.Transactions'.
The statement has been terminated.


I'm trying to insert into table Transaction,
That has to Primary keys:
[TRXNO], [RESORT_ID]

The TRXNO is the first column in the table--but it has a wierd setup.  
For example

TRXNO  RESORT_ID
3                  ELL2
2                  FAC
3                  CSI
3                  ATR
4                  CSI
3                  FAC
4                  ELL2

It Creates a transaction code- based on the last resort it -transaction -instead just adding a number for the next transaction.

Thanks JK
DatabasesMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
appari
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft SQL Server 2005
Microsoft SQL Server 2005

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.

72K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo