一次插入多条信息(sql)
注释:只适合单表单列数据,
create database test
go
use test
go
create table users
(
:id int identity(1,1) primary key not null,
:name nvarchar(20)
)
go
create proc sp_Inserts
@Names nvarchar(4000)
as
declare @Name nvarchar(20),@ErrorSum int
:set @ErrorSum = 0
:begin tran
:while(len(@Names)>0)
:begin
::if(charindex(',',@Names)<>len(@Names))
::begin
:::set @Name = substring(@Names,0,charindex(',',@Names))
:::insert into users(name) values(@Name)
:::set @ErrorSum = @ErrorSum + @@error
:::set @Names = substring(@Names,(charindex(',',@Names)+1),(len(@Names)-(charindex(',',@Names))))
::end
::else
::begin
:::set @Name = substring(@Names,0,charindex(',',@Names))
:::insert into users(name) values(@Name)
:::set @ErrorSum = @ErrorSum + @@error
:::set @Names = ''
::end
:end:
:if(@ErrorSum<>0)
::rollback tran
:else
::commit tran
go
sp_Inserts 'Tom,Jack,Bob,'
相关文档:
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1 <>1
法二:select top 0 * into b from a
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
3、说明:跨数据库之间表的拷贝(具体数据使用 ......
1) 选择最有效率的表名顺序(只在基于规则的优化器中有效):
ORACLE的解析器按照从右到左的顺序处理from子句中的表名,from子句中写在最后的表(基础表 driving table)将被最先处理,在from子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。如果有3个以上的表连接查询, 那就需要选择交叉表(intersection ......
drop table father;
create table father(
id int identity(1,1) primary key,
name varchar(20) not null,
age int not null
)
drop table mother;
create table mother(
id int identity(1,1),
name varchar(20) not null,
age int not null,
husban ......
例 34 找出年龄超过平均年龄的学生姓名。
SELECT SNAME
from STUDENTS
WHERE AGE >
(SELECT AVG(AGE)
from STUDENTS)
例 35 找出各课程的平均成绩,按课程号分组,且只选择学生超过 3 人的课程的成 ......
1 避免无计划的全表扫描
如下情况进行全表扫描:
- 该表无索引
- 对返回的行无人和限制条件(无Where子句)
- 对于索引主列(索引的第一 ......