Web888.vn
  • Shop
  • Blog
Đăng nhập
  • Đăng nhập / Đăng ký

Please enter key search to display results.

Home
  • Micrsoft SQL Server
  • Technology News
INSERT command in SQL Server

INSERT command in SQL Server

  • 24-07-2022
  • Toanngo92
  • 0 Comments

Mục lục

  • Introducing the INSERT command in SQL Server
  • Some situations should be noted with the INSERT . command
  • INSERT get return information

Introducing the INSERT command in SQL Server

The insert statement is used to add data to a table in SQL Server

Syntax:

 INSERT INTO table_name (column_list) VALUES (value_list)

Explain:

  • table_name: the name of the table you want to add new data
  • column_list : list of columns, separated by ","
  • value_list: is a list of values corresponding to the column, separated by ","
  • Note: The value pairs of column and value correspond and match in order and quantity with each other. Need to follow the correct syntax

For example:

 INSERT INTO Person.PersonPhone (BusinessEntityID, PhoneNumber, PhoneNumberTypeID, ModifiedDate) VALUES (299,'699-511-0142,1','2020-10-12')

Short syntax (no need to define columns, just specify the correct order of values):

 INSERT INTO Person.PersonPhone VALUES (20777,'699-511-0143',1,'2020-10-12')

The values of the corresponding columns are as follows:

  • BusinessEntityID: 299
  • PhoneNumber: 699-511-0142
  • PhoneNumberTypeID: 1
  • ModifiedDate: 2020-10-12

Some situations should be noted with the INSERT . command

Test this insert statement in AdventureWorks2019:

 USE [AdventureWorks2019] GO INSERT INTO [Production].[Product] ([Name] ,[ProductNumber] ,[MakeFlag] ,[FinishedGoodsFlag] ,[Color] ,[SafetyStockLevel] ,[ReorderPoint] ,[StandardCost] ,[ListPrice] ,[Size] ,[SizeUnitMeasureCode] ,[WeightUnitMeasureCode] ,[Weight] ,[DaysToManufacture] ,[ProductLine] ,[Class] ,[Style] ,[ProductSubcategoryID] ,[ProductModelID] ,[SellStartDate] ,[SellEndDate] ,[DiscontinuedDate] ,[rowguid] ,[ModifiedDate]) VALUES ('Laptop Thinkpad L470' ,'SKU-001' ,0 ,0 ,null ,1000 ,750 ,0 ,0 ,null ,null ,null ,null ,0 ,null ,null ,null ,null ,null ,'2008-05-01' ,null ,null ,NEWID() ,'2022-05-01' ) GO

This command gives an error:

 USE [AdventureWorks2019] GO INSERT INTO [Production].[Product] ( [ProductID], [Name] ,[ProductNumber] ,[MakeFlag] ,[FinishedGoodsFlag] ,[Color] ,[SafetyStockLevel] ,[ReorderPoint] ,[StandardCost] ,[ListPrice] ,[Size] ,[SizeUnitMeasureCode] ,[WeightUnitMeasureCode] ,[Weight] ,[DaysToManufacture] ,[ProductLine] ,[Class] ,[Style] ,[ProductSubcategoryID] ,[ProductModelID] ,[SellStartDate] ,[SellEndDate] ,[DiscontinuedDate] ,[rowguid] ,[ModifiedDate]) VALUES ( 1001, 'Laptop Thinkpad L470' ,'SKU-001' ,0 ,0 ,null ,1000 ,750 ,0 ,0 ,null ,null ,null ,null ,0 ,null ,null ,null ,null ,null ,'2008-05-01' ,null ,null ,NEWID() ,'2022-05-01' ) GO
Error occurs when running below command

Reason: in Product table, column ProductID is primary key and is assigned self-incrementing attribute, in SQL Server is not assigned a value for column with self-incrementing value to ensure data integrity, so, we just don't insert data into the ProductID column and that's ok, that column will be automatically generated by SQL Server according to the self-incrementing mechanism.

INSERT get return information

All newly inserted data will be stored in the inserted variable, and to get the inserted data, we use the OUTPUT command. If you want to output more columns, separate the columns by commas ",":

For example, get the ID just inserted

 USE [AdventureWorks2019] GO INSERT INTO [Production].[Product] ([Name] ,[ProductNumber] ,[MakeFlag] ,[FinishedGoodsFlag] ,[Color] ,[SafetyStockLevel] ,[ReorderPoint] ,[StandardCost] ,[ListPrice] ,[Size] ,[SizeUnitMeasureCode] ,[WeightUnitMeasureCode] ,[Weight] ,[DaysToManufacture] ,[ProductLine] ,[Class] ,[Style] ,[ProductSubcategoryID] ,[ProductModelID] ,[SellStartDate] ,[SellEndDate] ,[DiscontinuedDate] ,[rowguid] ,[ModifiedDate]) OUTPUT inserted.ProductID, inserted.Name VALUES ('Dell Inspiron 5570' ,'SKU-002' ,0 ,0 ,null ,1000 ,750 ,0 ,0 ,null ,null ,null ,null ,0 ,null ,null ,null ,null ,null ,'2008-05-01' ,null ,null ,NEWID() ,'2022-05-01' ) GO
Output after data is inserted

Bài viết liên quan:

Introduction to Transact-SQL (T-SQL)
Introduction to Microsoft SQL Server
Create and administer the database, create users in SQL Server
Common Table Expression (CTE) – Common Table Expression in SQL Server
Combine Data Using (SET OPERATORS) UNION, INTERSECT, and EXCEPT in SQL Server
SQL Server Pivot and Grouping Set Operators
Programming and control of flow in Transact SQL
Transaction in SQL Server
Subqueries (subqueries) in SQL Server
SQL Server GROUP BY clause
Index in SQL Server
Entity-relational model (ER model) and data normalization (normalization)

THÊM BÌNH LUẬN Cancel reply

Dịch vụ thiết kế Wesbite

NỘI DUNG MỚI CẬP NHẬT

Step-by-step guide to installing Microsoft SQL Server and connecting to the database for beginners

Reasons Why People Are Increasingly Inexperienced in Life

PHP Data Types

Table (table) in SQL Server

Basic data types in C

Giới thiệu

web888.vn là chuyên trang chia sẻ và cập nhật tin tức công nghệ, chia sẻ kiến thức, kỹ năng. Chúng tôi rất cảm ơn và mong muốn nhận được nhiều phản hồi để có thể phục vụ quý bạn đọc tốt hơn !

Liên hệ quảng cáo: [email protected]

Kết nối với web888

© web888.vn - Tech888 Co .Ltd since 2019

Đăng nhập

Trở thành một phần của cộng đồng của chúng tôi!
Registration complete. Please check your email.
Đăng nhập bằng google
Đăng kýBạn quên mật khẩu?

Create an account

Welcome! Register for an account
The user name or email address is not correct.

Your personal data will be used to support your experience throughout this website, to manage access to your account, and for other purposes described in our privacy policy.

Registration confirmation will be emailed to you.
Log in Lost your password?

Reset password

Recover your password
Password reset email has been sent.
The email could not be sent. Possible reason: your host may have disabled the mail function.
A password will be e-mailed to you.
Log in Register
×