第一种最简单的
  into 和目标列的列表是可选的
  intsert [into] tableA [(col1,col2)] values(val1,val2)

第二种
  insert tableA values(val1,default)--cal2的在创建表时需要有默认值约束
  insert tableA default values --tableA表中所有的列都时有默认值约束才可以

第三种
  insert [into] tableA(col1,col2)
  select val1,val2 from Stu --可以使用select查出的值作为insert值

第四种,使用存储过程返回的结果集
  insert [into] tableA(col1,col2)
  exec sourceprocname

insert和错误

  insert的一个有趣的特性就是批命令错误的密封性--同时插入10条数据其中一条错误,但其它9条不受那一条的影响

  如果希望一个insert插入失败时整个批命令都失败如何操作呢?--利用@@ERROR

 1 create table #famousjaycees
 2 (
 3     jc varchar(15) unique,
 4     occupation varchar(25),
 5     becamefamous int default 0,
 6     notes text null
 7 )
 8 
 9 insert #famousjaycees values('julius','miliday',0045,'took the raman')
10 if(@@ERROR <>0) GOTO LIST
11 insert #famousjaycees values('julius','miliday',0045,'took the raman')
12 if(@@ERROR <>0) GOTO LIST
13 insert #famousjaycees values('JESUS','founded',0001,'birth featured')
14 if(@@ERROR <>0) GOTO LIST
15 insert #famousjaycees values('John','founded',0001,'birth featured')
16 if(@@ERROR <>0) GOTO LIST
17 insert #famousjaycees values('Joan','founded',0001,'birth featured')
18 if(@@ERROR <>0) GOTO LIST
19 insert #famousjaycees values('Joe','founded',0001,'birth featured')
20 if(@@ERROR <>0) GOTO LIST
21 
22 list:select * from #famousjaycees
执行语句

相关文章:

  • 2021-12-27
  • 2021-12-21
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2021-09-13
  • 2022-02-16
相关资源
相似解决方案