Sql Tutorial Class


How to use select into in sql server

How to use select into in SQL Server

SELECT INTO: The statement create new table and insert the record(s) into new table.


SELECT INTO Syntax:

Select [*/COLUMN_NAME,..] into [NEW_TABLE_NAME] from [OLD_TABLE_NAME] where [COLUMN_NAME]=['VALUE']


Description:
             Select: SQL Keyword.
             Into: SQL statement.             
        COLUMN_NMME: Column name.
             From: SQL Keyword.
             NEW_TABLE_NAME: New table name.
             Where: SQL keyword.
            ['VALUE'] : User defined value.




SQL SELECT INTO Examples


Here is a Table ItemDetails

ItemCode ItemName ManufactureCompany ProductDate ExpiredDate ItemSerialNo ItemCategory ItemQuantity ItemPrice ProductCountry ReceivedDate
1 Samsung Mobile Samsung 12/10/2024 12/1/2050 S12345 Mobile 500 240 Korea 12/10/2025
2 Samsung Pro Samsung 12/10/2024 12/1/2050 SP1234 Mobile 100 300 Korea 12/10/2025
3 Samsung Headphone Samsung 12/10/2023 12/1/2053 SH2345 Headphone 50 50 Korea 12/10/2025
4 iPhone iPone 12/1/2024 12/1/2049 iP12345 Mobile 1000 500 United Stat 12/10/2025
5 Apple Mobile Apple 12/11/2024 12/1/2049 A12345 Mobile 200 300 United Stat 12/10/2025
6 Apple Computer Apple 12/2/2024 12/1/2050 A12343 Computer 200 400 United Stat 12/10/2025
7 Apple Macbook Apple 12/11/2024 12/1/2050 AM2345 Macbook 50 600 United Stat 12/10/2025
8 Dell Computer DELL 12/9/2024 12/1/2050 D02345 Computer 200 240 United Stat 12/10/2025
9 Dell Laptop DELL 12/9/2024 12/1/2050 DL2345 Laptop 30 240 United Stat 12/10/2025
10 Apple Laptop Apple 12/8/2024 12/1/2050 AL2345 Laptop 500 100 United Stat 12/10/2025
11 Apple Headphone Apple 12/12/2024 12/1/2050 AH2345 Headphone 50 20 United Stat 12/10/2025
12 Sony Camera Sony 12/11/2024 12/11/2050 SN2345 Camera 200 240 Japan 12/11/2025
13 Sony Headphone Sony 12/9/2024 12/1/2050 SNH345 Headphone 500 50 Japan 12/11/2025
14 Ascer Computer Ascer 12/9/2024 12/7/2050 AC2345 Computer 100 600 China 12/10/2025
16 Borother Printer Brother 12/9/2024 12/1/2050 B12345 Printer 200 200 China 12/10/2025
17 Ascer Printer Ascer 12/9/2024 12/7/2050 AP2345 Printer 200 150 China 12/10/2025
18 Brother Photocopy Brother 12/8/2024 12/7/2050 BP2345 Printer 20 150 China 12/10/2025
19 Brother Multi Printer Brother 2/5/2020 10/10/2050 BM243 Photocopy NULL NULL India 2/5/2020





Following SQL Statement copy all the records from ItemDetails table and Insert into ItemDetailsBack table. [Note: ItemDetailsBack table automatically created]

Select * into ItemDtailsBack from ItemDetails

Select * from ItemDtailsBack

Result:
ItemCode ItemName ManufactureCompany ProductDate ExpiredDate ItemSerialNo ItemCategory ItemQuantity ItemPrice ProductCountry ReceivedDate
1 Samsung Mobile Samsung 12/10/2024 12/1/2050 S12345 Mobile 500 240 Korea 12/10/2025
2 Samsung Pro Samsung 12/10/2024 12/1/2050 SP1234 Mobile 100 300 Korea 12/10/2025
3 Samsung Headphone Samsung 12/10/2023 12/1/2053 SH2345 Headphone 50 50 Korea 12/10/2025
4 iPhone iPone 12/1/2024 12/1/2049 iP12345 Mobile 1000 500 United Stat 12/10/2025
5 Apple Mobile Apple 12/11/2024 12/1/2049 A12345 Mobile 200 300 United Stat 12/10/2025
6 Apple Computer Apple 12/2/2024 12/1/2050 A12343 Computer 200 400 United Stat 12/10/2025
7 Apple Macbook Apple 12/11/2024 12/1/2050 AM2345 Macbook 50 600 United Stat 12/10/2025
8 Dell Computer DELL 12/9/2024 12/1/2050 D02345 Computer 200 240 United Stat 12/10/2025
9 Dell Laptop DELL 12/9/2024 12/1/2050 DL2345 Laptop 30 240 United Stat 12/10/2025
10 Apple Laptop Apple 12/8/2024 12/1/2050 AL2345 Laptop 500 100 United Stat 12/10/2025
11 Apple Headphone Apple 12/12/2024 12/1/2050 AH2345 Headphone 50 20 United Stat 12/10/2025
12 Sony Camera Sony 12/11/2024 12/11/2050 SN2345 Camera 200 240 Japan 12/11/2025
13 Sony Headphone Sony 12/9/2024 12/1/2050 SNH345 Headphone 500 50 Japan 12/11/2025
14 Ascer Computer Ascer 12/9/2024 12/7/2050 AC2345 Computer 100 600 China 12/10/2025
16 Borother Printer Brother 12/9/2024 12/1/2050 B12345 Printer 200 200 China 12/10/2025
17 Ascer Printer Ascer 12/9/2024 12/7/2050 AP2345 Printer 200 150 China 12/10/2025
18 Brother Photocopy Brother 12/8/2024 12/7/2050 BP2345 Printer 20 150 China 12/10/2025
19 Brother Multi Printer Brother 2/5/2020 10/10/2050 BM243 Photocopy NULL NULL India 2/5/2020







Select ItemName,ManufactureCompany,ItemCategory Into ICI from ItemDetails

Select * from ICI
Result:
ItemName ManufactureCompany ItemCategory
Samsung Mobile Samsung Mobile
Samsung Pro Samsung Mobile
Samsung Headphone Samsung Headphone
iPhone iPone Mobile
Apple Mobile Apple Mobile
Apple Computer Apple Computer
Apple Macbook Apple Macbook
Dell Computer DELL Computer
Dell Laptop DELL Laptop
Apple Laptop Apple Laptop
Apple Headphone Apple Headphone
Sony Camera Sony Camera
Sony Headphone Sony Headphone
Ascer Computer Ascer Computer
Borother Printer Brother Printer
Ascer Printer Ascer Printer
Brother Photocopy Brother Printer
Brother Multi Printer Brother Photocopy








Select ItemName,ManufactureCompany,ItemCategory Into USAProduct from ItemDetails Where ProductCountry='United Stat'

Select * from USAProduct
Result:
ItemName ManufactureCompany ItemCategory
iPhone iPone Mobile
Apple Mobile Apple Mobile
Apple Computer Apple Computer
Apple Macbook Apple Macbook
Dell Computer DELL Computer
Dell Laptop DELL Laptop
Apple Laptop Apple Laptop
Apple Headphone Apple Headphone





SQL SELECT INTO Statement in Video












No comments:

Post a Comment