復制代碼 代碼如下:
INSERT INTO #DirtyOldWIPBOM SELECT TOP (100) PERCENT dbo.WIP_BOM.Model, dbo.WIP_BOM.PartNumber,WIP_BOM.WIP
FROM dbo.WIP_BOM left OUTER JOIN dbo.BOM_CHINA ON LTRIM(dbo.WIP_BOM.Model) = LTRIM(dbo.BOM_CHINA.Model) AND LTRIM(dbo.WIP_BOM.PartNumber) = LTRIM(dbo.BOM_CHINA.PartNumber) AND dbo.WIP_BOM.Qty = dbo.BOM_CHINA.Qty AND BOM_CHINA.WIP=
WIP_BOM.WIP WHERE (dbo.BOM_CHINA.Model IS NULL) AND EXISTS(SELECT * FROM dbo.BOM_CHINA WHERE WIP = WIP_BOM.WIP AND LTRIM(Model) = LTRIM(WIP_BOM.Model))
這樣就可以成功。
復制代碼 代碼如下:
但SELECT TOP (100) PERCENT dbo.WIP_BOM.Model, dbo.WIP_BOM.PartNumber,WIP_BOM.WIP into #DirtyOldWIPBOM
FROM dbo.WIP_BOM left OUTER JOIN dbo.BOM_CHINA ON LTRIM(dbo.WIP_BOM.Model) = LTRIM(dbo.BOM_CHINA.Model) AND LTRIM(dbo.WIP_BOM.PartNumber) = LTRIM(dbo.BOM_CHINA.PartNumber) AND dbo.WIP_BOM.Qty = dbo.BOM_CHINA.Qty AND BOM_CHINA.WIP=
WIP_BOM.WIP WHERE (dbo.BOM_CHINA.Model IS NULL) AND EXISTS(SELECT * FROM dbo.BOM_CHINA WHERE WIP = WIP_BOM.WIP AND LTRIM(Model) = LTRIM(WIP_BOM.Model))
會提示error "There is already an object named '#DirtyOldWIPBOM' in the database"
為什麼?
[筆記]:千萬不要用游標,用臨時表和表變量代替游標會極大的提高性能,今天用游標處理700行數據用了2小時,用臨時表不到1秒就搞定。