View Single Post
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: SQL Insert statement syntax

 
0
  #5
Dec 4th, 2008
I'm trying to re-produce your case.

Here's my simplified tables.
Temp-table:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. USE [EditorTest]
  2. GO
  3. /****** Object: Table [dbo].[CASE_MILESTONE_TEMP] Script Date: 12/04/2008 10:35:36 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[CASE_MILESTONE_TEMP](
  11. [CASE_IDENTIFIER] [int] NULL,
  12. [MILESTONE_DATE] [datetime] NULL,
  13. [TEXT_FIELD] [varchar](50) NULL,
  14. [DROP_THIS_FIELD] [varchar](50) NULL
  15. ) ON [PRIMARY]
  16.  
  17. GO
  18. SET ANSI_PADDING OFF
and Clean-table:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. USE [EditorTest]
  2. GO
  3. /****** Object: Table [dbo].[CLEAN_MILE_DUPES] Script Date: 12/04/2008 10:35:05 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[CLEAN_MILE_DUPES](
  11. [CASE_IDENTIFIER] [int] NULL,
  12. [MILESTONE_DATE] [datetime] NULL,
  13. [TEXT_FIELD] [varchar](50) NULL
  14. ) ON [PRIMARY]
  15.  
  16. GO
  17. SET ANSI_PADDING OFF
with CASE_IDENTIFIER as a duplicate key:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. USE [EditorTest]
  2. GO
  3. CREATE UNIQUE NONCLUSTERED INDEX [test] ON [dbo].[CLEAN_MILE_DUPES]
  4. (
  5. [CASE_IDENTIFIER] ASC
  6. )
  7. WITH (IGNORE_DUP_KEY = ON)
and I tested with this code:
  1. Option Explicit
  2.  
  3. Private oConn As ADODB.Connection
  4. Private oCmd As ADODB.Command
  5.  
  6. Private Sub Command1_Click()
  7. '
  8. Dim strSQL As String
  9. Dim ConStr As String
  10.  
  11. On Error GoTo ErrorHandler
  12.  
  13. ConStr = "PROVIDER=SQLOLEDB;Data Source=JASMIN\SQLEXPRESS; INITIAL CATALOG=EditorTest; User ID=XXXXXXX; Password=XXXXXXX;"
  14. Set oConn = New ADODB.Connection
  15. oConn.Open (ConStr)
  16.  
  17. strSQL = "INSERT CLEAN_MILE_DUPES (" & vbCrLf _
  18. & " CASE_IDENTIFIER" & vbCrLf _
  19. & " ,MILESTONE_DATE" & vbCrLf _
  20. & " ,TEXT_FIELD) " & vbCrLf _
  21. & "(SELECT CASE_IDENTIFIER" & vbCrLf _
  22. & " ,MILESTONE_DATE" & vbCrLf _
  23. & " ,TEXT_FIELD " & vbCrLf _
  24. & " FROM CASE_MILESTONE_TEMP)"
  25.  
  26. Set oCmd = New ADODB.Command
  27. oCmd.CommandText = strSQL
  28. oCmd.CommandType = adCmdText
  29. Set oCmd.ActiveConnection = oConn
  30. oCmd.Execute
  31.  
  32. Sub_Exit:
  33. oConn.Close
  34. Exit Sub
  35. ErrorHandler:
  36. ' Breakpoint here
  37. Resume Sub_Exit
  38.  
  39. End Sub
The error I get in the Breakpoint-line is:
? oConn.Errors(0).Description
Duplicate key was ignored.
and that comes from a duplicate CASE_IDENTIFIER-field value from the Temp-table. And now the Case-table has all the rows from the Temp-table, except one dropped duplicate, and the column DROP_THIS_FIELD is missing of course.

Is this anything similar to your code?

(I tested with SQL Server 2005 EE, but that shouldn't make any difference)
Last edited by Teme64; Dec 4th, 2008 at 4:54 am. Reason: Added DB info
Reply With Quote