search
HomeDatabaseMysql Tutorial向SqlServer数据库插入数据

向SqlServer数据库插入数据

Jun 07, 2016 pm 03:24 PM
insertsqlservervalinsertdatadatabase

Insert Values Insert Select Insert Exec Select Into Bulk Insert Insert Values是最常用的一种插入数据的方式,基本语法如下,表名之后可以指定目标列的名称,这种显式的指定目标列的名称是可选的,但是这种做法的好处是对语句的列值列名之间的关联关系进

  1. Insert Values
  2. Insert Select
  3. Insert Exec
  4. Select Into
  5. Bulk Insert

Insert Values是最常用的一种插入数据的方式,基本语法如下,表名之后可以指定目标列的名称,这种显式的指定目标列的名称是可选的,但是这种做法的好处是对语句的列值列名之间的关联关系进行控制,而不必单纯的依赖定义表时

各个列出现的顺序


 

INSERT INTO dbo.DimDate
        ( DataKey ,
          Year ,
          Qu ,
          QuCN ,
          Month ,
          MonthCN ,
          Ten ,
          TenCN ,
          Week ,
          WeekDay ,
          Day ,
          Date
        )
VALUES  ( 0 , -- DataKey - int
          0 , -- Year - int
          0 , -- Qu - int
          '' , -- QuCN - varchar(20)
          0 , -- Month - int
          '' , -- MonthCN - varchar(20)
          0 , -- Ten - int
          '' , -- TenCN - varchar(20)
          0 , -- Week - int
          '' , -- WeekDay - varchar(20)
          0 , -- Day - int
          '2014-09-23 13:34:16'  -- Date - datetime
        )

 确定列值的优先级顺序:给定的值》默认值》Null

Sql2008增强了Values功能,允许在一条语句中指定由逗号隔开的多条记录

INSERT INTO dbo.Table_1  ( ID ) VALUES  ( 0 ),( 1 ),( 2 )

 使用Values构建虚拟表

SELECT * FROM (VALUES  ( 0 ),( 1 ),( 2 )) AS a(id)

 Insert Select


 Insert Select 将一组由Select查询返回的结果行插入到目标表中(目标表是已经存在的表,这一点和Select Into有区别

INSERT INTO dbo.Table_1 SELECT * FROM dbo.Table_2

 Insert Exec


  Insert Exec 语句可以把存储过程或动态Sql批处理返回的结果集插入到目标表

Select Into


 Select Into的作用是创建一个目标表,并用返回的结果集填充它,不能用这个语句向已经存在的表中插入数据,

当指定的目标表是临时表时自动创建一个临时表,指定为用户表时创建一个用户表

Select Into 会复制来源表的基本结构(包括列名、数据类型、是否允许NUll以及IDentity属性),不会复制的三样东西(约束、索引、触发器)

Select into的另一个优点:如果数据库的属性没有设置成完成恢复模式,Select into 就会按照最小日志记录模式来执行操作

Bulk Insert


 这种方式用的比较少,用于将文件中的数据导入一个已经存在的表

 示例本例从指定的数据文件中导入订单详细信息,该文件使用竖杠 (|) 字符作为字段终止符,使用 |/n 作为行终止符。

BULK INSERT Northwind.dbo.[Order Details]   FROM 'f:/orders/lineitem.tbl'     
WITH (   
     FIELDTERMINATOR = '|',            
     ROWTERMINATOR = '|/n'         
) 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.