在 sql server 中,表值构造函数(table value constructor, tvc)是一种用于在单个语句中插入多行数据到表中的语法。它允许你以行内表值表达式(row-valued expression)的形式指定多行数据,并将这些数据作为一个整体插入到表中。
1、本文内容
- 语法
- 自变量
- 限制和局限
- 数据类型
- 示例
- 另请参阅
适用于:
- sql server
- azure sql 数据库
- azure sql 托管实例
指定要构建到某一表中的一组行值表达式。 transact-sql 表值构造函数允许在单个 dml 语句中指定多行数据。 表值构造函数可以指定为 insert values 语句的 values 子句…或指定为 merge 语句 using 子句中的或 from 子句中的派生表。
2、语法
values ( <row value expression list> ) [ ,...n ] <row value expression list> ::= {<row value expression> } [ ,...n ] <row value expression> ::= { default | null | expression }
noteto view transact-sql syntax for sql server 2014 (12.x) and earlier versions, see previous versions documentation.
若要查看 sql server 2014 (12.x) 及更早版本的 transact-sql 语法,请参考如下官方地址
3、自变量
values
介绍行值表达式列表。 每个列表都必须用括号括起来并由逗号分隔。在每个列表中指定的值的数目必须相同,并且值必须采用与表中的列相同的顺序。 表中每个列的值必须指定,或者列列表必须显式为每个传入值指定列。
default
强制数据库引擎插入为列定义的默认值。 如果某列并不存在默认值,并且该列允许 null 值,则插入 null。 default 对标识列无效。 当在表值构造函数中指定时,只在 insert 语句中允许 default。expression
一个常量、变量或表达式。 表达式不能包含 execute 语句。
4、限制和局限
当指定为派生表时,行数没有限制。
当用作 insert values 语句的 values 子句时…限制为最多 1000行。 如果行数超过最大值,则返回错误 10738。 若要插入超过 1000 行的数据,请使用下列方法之一:
创建多个 insert 语句
使用派生表
通过使用 bcp 实用工具、.net sqlbulkcopy 类、openrowset (bulk …) 或 bulk insert语句批量导入数据
只允许单个标量值作为行值表达式。 涉及多列的子查询不允许作为行值表达式。 例如,以下代码导致语法错误,因为第三个行值表达式列表包含具有多列的子查询。
t_product 表信息如下
create table dbo.t_myproducts (nameinfo nvarchar(50), price decimal(18,2)); go -- this statement fails because the third values list contains multiple columns in the subquery. insert into dbo.t_myproducts (nameinfo, price) values (n'鼠标', 25.50), (n'键盘', 130.00) , (select title, price from t_product where id = 2); go
但是,可以通过单独在子查询中指定每一列,重新编写该语句。 下面的示例成功地将三行插入 t_myproducts 表中。
insert into dbo.t_myproducts (name, listprice) values (n'鼠标', 25.50), (n'键盘', 130.00) , ((select title from t_product where id = 2), (select price from t_product where id = 2)); go select * from dbo.t_myproducts go
5、数据类型
在多行 insert 语句中指定的值遵循 union all 语法的数据类型约定属性。 这会导致不匹配类型隐式转换到更高优先级的类型。 如果此转换不是所支持的隐式转换,则返回错误。 例如,以下语句将整数值和字符值插入到类型为 char 的列中。
create table dbo.t_datatype (col_a int, col_b char); go insert into dbo.t_datatype values (1,'a'), (2, 1); go
运行 insert 语句时,sql server 尝试将 ‘a’ 转换为整数,因为数据类型优先级指示整数类型的优先级高于字符。 转换失败,并且返回错误。 您可以根据需要显式转换值,从而避免发生此错误。 例如,前面的语句可以编写为:
insert into dbo.t_datatype values (1,'a'), (2, convert(char,1));
6、示例
6.1、插入多行数据
下面的示例创建表 dbo.t_myproducts,然后使用表值构造函数将3行数据插入到该表中。 由于提供了所有列的值并按表中各列的顺序列出这些值,因此不必在列列表中指定列名。
insert into t_myproducts values (n'华为手机',6888.88), (n'荣耀手机',6688.99), (n'小米手机',5999.88); go select * from dbo.t_myproducts
6.2、使用 default 和 null 值插入多行
下面的示例说明如何在使用表值构造函数向表中插入行时指定 default 和 null。
create table t_mysalesreason ( id int identity(1,1) not null, nameinfo nvarchar(32) null , reasontype nvarchar(32) not null default n'新能源汽车' ); go insert into t_mysalesreason values (n'问界m9',n'增程'), (n'比亚迪唐', default), (null, n'燃油车'); select * from t_mysalesreason;
6.3、在 from 子句中将多个值指定为派生表
下面的示例在 select 语句的 from 子句中使用表值构造函数指定多个值。
select col_a,col_b from (values (1, 2), (3, 4), (5, 6), (7, 8), (9, 10) ) as mytable(col_a, col_b); go -- used in an inner join to specify values to return. select nameinfo,a.reasontype,b.name from t_mysalesreason as a inner join (values (n'问界m9'), (n'比亚迪唐'), (n'宝马x3')) as b (name) on a.nameinfo = b.name; -- used in an cross join to specify values to return. select nameinfo,a.reasontype,b.name from t_mysalesreason as a cross join (values (n'问界m9'), (n'比亚迪唐'), (n'宝马x3')) as b (name);
6.4、在 merge 语句中将多个值指定为派生源表
下面的示例使用 merge 以更新或插入行的方式来修改 t_mysalesreason 表。 当源表中的 newname 值与目标表 nameinfo列中的值匹配时,就会更新此目标表中的 reasontype 列。 当 newname 的值不匹配时,就会将源行插入到目标表中。 此源表是一个派生表,它使用 transact-sql 表值构造函数指定源表的多个行。
-- create a temporary table variable to hold the output actions. declare @summaryofchanges table(change varchar(20)); merge into t_mysalesreason as target using (values (n'问界m9',n'绿牌'), (n'比亚迪唐', n'绿牌'), (n'宝马x3', n'蓝牌')) as source (newname, newreasontype) on target.nameinfo = source.newname when matched then update set reasontype = source.newreasontype when not matched by target then insert (nameinfo, reasontype) values (newname, newreasontype) output $action into @summaryofchanges; -- query the results of the table variable. select change, count(*) as countperchange from @summaryofchanges group by change;
查询表返回结果集如下,满足预期的结果
6.5、 插入超过 1000 行
以下示例演示如何将表值构造函数用作派生表。 此方式可从单个表值构造函数中插入超过 1000 行。
create table dbo.t_testvalue ([value] int); insert into dbo.t_testvalue ([value]) select [newval] from (values (0), (1), (2), (3), ..., (5000)) as temp01 ([newval]);
7、另请参阅
insert (transact-sql)
https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql?view=sql-server-ver16merge (transact-sql)
https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver16from (transact-sql)
https://learn.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql?view=sql-server-ver16
到此这篇关于sqlserver 表值构造函数(transact-sql)的使用的文章就介绍到这了,更多相关sql 表值构造函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论