通过学习,我们知道在Transact-SQL DDL中,我们通过Create Table语句来创建新表的。但创建好新表之后,如果表中记录集合采用数据库中已经存在的一张或多张表的记录来进行填充时,我们就需要用到Insert into的方式,实现内容的复制。
我们来看一下,基本的Insert语句的用法。例如:以Northwind数据库为例,我们需要往Customers表中插入一条新记录。
Code 通过以上操作,我们既实现了将Customers表中的记录复制到了新表NewCustomers之中。当然,我们也可以从几张表中复制内容到一张表中。例如:
Code 此外,Transact-SQL中还允许嵌套的Execute语句,它调用一个存储过程,为多行Insert语句生成结果表。例如:
我们来看一下,基本的Insert语句的用法。例如:以Northwind数据库为例,我们需要往Customers表中插入一条新记录。
Insert into Customers(CustomerId,CompanyName,ContactName) values('ALFKI','Alfreds Futterkiste','Maria Anders')
Insert into语句的一般用法,多是用于为某张表插入新记录。而很多时候,我们需要将某一张或几张表中的记录复制到一张新表之中,那么就需要用到多行Insert语句的用法。现在我们来看看多行Insert的用法。Create Table CustCreditRate
(CustId Int Not Null,
CreditRate Int)
Insert into CustCreditRate
(CustId,
CreditRate)
Execute CalcCustCreditRating
(CustId Int Not Null,
CreditRate Int)
Insert into CustCreditRate
(CustId,
CreditRate)
Execute CalcCustCreditRating