SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Insert works in 2008 but not SQL 2000
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Vack
Constraint Violating Yak Guru

USA
464 Posts

Posted - 07/25/2012 :  09:50:34  Show Profile  Reply with Quote
What do I need to do to have this script work in SQL 2000. When I run it in SQL 2000 I get cannot insert duplicate key. I am running the script against the same data in both versions.

I would expect it to insert zero records if it finds existing records in the table.


INSERT INTO [001].[dbo].[ItemAccounts]
           ([ItemCode]
           ,[AccountCode]
           ,[MainAccount]
           ,[ItemCodeAccount]
           ,[PurchaseCurrency]
           ,[PurchasePrice]
           ,[PurchaseVATCode]
           ,[PurchaseVATPerc]
           ,[PurchaseVATIncl]
           ,[PurchaseUnitToInternalUnitFactor]
           ,[PurchaseUnitToPurchasePackageFactor]
           ,[PurchaseOrderSize]
           ,[DiscountMargin]
           ,[SalesPriceRecommended]
           ,[SlsPkgsPerPurPkg]
           ,[DeliveryTimeInDays]
           ,[DeliverableFromStock]
           ,[StatisticalFactor]
           ,[Warranty]
           ,[Division]
           ,[syscreated]
           ,[syscreator]
           ,[sysmodified]
           ,[sysmodifier]
           ,[sysguid])
  
     select
           oecusitm_sql.item_no,
           cicmpy.cmp_wwn,
          'False',
           oecusitm_sql.cus_item_no,
           'USD',
           OECUSITM_SQL.item_price,
          0,
           0,
           'False',
           1,
          1,
          1,
          0,
          0,
          1,
           0,
           'False',
           1,
         0,
          1,
           GETDATE(),
          1,
           getdate(),
           1,
           NEWID()
           from oecusitm_sql join cicmpy on oecusitm_sql.cus_no = cicmpy.debcode
           left join ItemAccounts on ItemAccounts.ItemCode = oecusitm_sql.item_no and ItemAccounts.ItemCodeAccount = oecusitm_sql.cus_item_no
   
           where not exists(select 1 from ItemAccounts t1 where t1.ItemCode = oecusitm_sql.item_no and t1.ItemCodeAccount = oecusitm_sql.cus_item_no)

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 07/25/2012 :  09:56:33  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Choose EITHER

from oecusitm_sql join cicmpy on oecusitm_sql.cus_no = cicmpy.debcode
left join ItemAccounts on ItemAccounts.ItemCode = oecusitm_sql.item_no and ItemAccounts.ItemCodeAccount = oecusitm_sql.cus_item_no

where ItemAccounts.ItemCode is null


or


from oecusitm_sql join cicmpy on oecusitm_sql.cus_no = cicmpy.debcode
where not exists(select 1 from ItemAccounts t1 where t1.ItemCode = oecusitm_sql.item_no and t1.ItemCodeAccount = oecusitm_sql.cus_item_no)



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000