Example of SQL Server inserting data into a temporary table

Source: Internet
Author: User

Copy codeThe Code is as follows:
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 ))

In this way, you can succeed.
Copy codeThe Code is as follows:
However, 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" is displayed"

Why?

[Note]: Do not use cursors. Replacing cursors with temporary tables and table variables will greatly improve performance. Today, it takes two hours to process 700 rows of data with cursors, it takes less than 1 second to use a temporary table.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.