Create FUNCTION split ( @StrAll varchar(8000), @StrSeprate varchar(10) ) RETURNS @temp TABLE(F1 VARCHAR(100)) AS BEGIN DECLARE @i INT SET @StrAll =rtrim(ltrim(@StrAll )) SET @i=charindex(@StrSeprate,@StrAll ) WHILE @i>=1 BEGIN INSERT @temp VALUES(left(@StrAll ,@i-1)) SET @StrAll =substring(@StrAll ,@i+1,len(@StrAll )-@i) SET @i=charindex(@StrSeprate,@StrAll ) END IF @StrAll <>'' INSERT @temp VALUES(@StrAll ) RETURN END
6.取出数据库中所有的表名
1
select name as tablename from sysobjects where type='U' and name<>'dtproperties'
7.sqlserver中取随机数的两种方法
a.创建一个表Rand,字段是:RandomNum ,存储0到9的数据。
使用下面SQL语句可产生随机数:
1
select top 1 RandomNum from Rand order by NewID()
b.使用sqlserver提供的Rand()函数
1 2 3 4
select cast( floor(rand()*N) as int) --产生到N-1之间的随机数 select cast(ceiling(rand() * N) as int) --产生到N之间的随机数
create table T2 ( id int ) insert T1 select 1 union all select 2 union all select 3 union all select 4
insert T2 select 3 union all select 4 运算代码:
--交集 ---------------------------------------------- --方法 select * from T1 intersect select * from T2 --方法 select distinct * from T1 where T1.id in (select id from T2) --方法 select distinct * from T1 where exists(select id from T2 where T2.id=T1.id)
--MsSql随机取数据 select top 10 * from tablename order by newid() --Access 随机取数据 select top 10 * FROM tablename order by rnd(id) --mySql 随机取数据 SELECT * FROM tablename order by rand() limit 10