您的位置:首页 > 数据库

sql server 笔记

2007-11-16 16:25 218 查看
declare mycursor cursor for select * from aa2
declare @a varchar(5),@b varchar(20),@c int,@e
open mycursor
fetch next from mycursor into @a,@b,@c,@d
create table aa(bianhao varchar(10),pici varchar(10),
kucun int,buliang int)
set @e=@a
select @d=4 from aa1 where bianhao=@a
while @@fetch_status=0
begin

if @d>=@c
insert into aa values(@a,@b,@c,@c)
@d=@d-@c
fetch next from mycursor into @a,@b,@c
if(@e<>@a)
set @e=@a
select @d=4 from aa1 where bianhao=@a
else insert into aa values(@,@b,@c,@d)
while @@fetch_status=0 and @e=@a
fetch next from mycursor into @a,@b,@c
set @e=@a
select @d=4 from aa1 where bianhao=@a

end

select min(id),oid,date from biao group by oid,date

create trigger mytri on biao
for insert
as
begin
if exists(select biao.id from biao inner join insert on
biao.oid=insert.oid and biao.date=insert.date)
delete insert
end

select a.id,a.type,a.number from a inner join(select type,
max(number) from biao group by type)b on a.type=b.type and
a.number=b.number

select a.* from a as b where (select count(*) from a
where a.type=b.type and b.number<a.number)<1
select distinct a.* from a as b where id in(select top 1 id from a where a.type=b.type order by number desc) group by id,type,number
select * from a as b where not existe(select 1 from a
where b.type=type and number>b.number)

update #aa
set fend=(select fend from #aa where fid=1)+(select
fin-out from #aa where fid=2)
from #aa
where fid=2

if object_id('pubs..#temp') is not null
drop table #temp
update #temp
set 结存=(select sum(收入-发出) from #temp,#temp as a
where a.id<=#temp.id)+(select 月存数 from #temp)

declare mycursore cursore for select fid,fin,fout,fend from #aa
open mycursore
declare @a int,@b int,@c int,@d int
fetch next from mycursore
fetch next from mycursore into @a,@b,@c,@d
while @@fetch_status=0
begin
update #aa
set fend=(select fend from #aa where fid=@a-1)+@b-@c
fetch next from mycursore into @a,@b,@c,@d
end

update #temp
set fend=(select fend from #temp where fid=1)+fin-fout
from #temp
where fid>1

declare @fend int
select @fend=fend from biao where fid=1
update biao
set fend=@fend+fout-fin,@fend=fend from biao where fid>1

create proc myprocduce @bbb varchar(200) output
as
begin
declare @i int,@m,@id int
set @i=0,@bbb=''
select @m=max(id) from ab
while @i<5
begin
set @id=ceiling(@m*rand())
select @bbb=@bbb+','+aa from ab where id=@id
@i=@i+1

end
end

create proc myproc
as
declare @a varchar(200)
select @a=isnull(@a+',','')+aa from (select top 5 aa from a order by newid())b
update ab2
set aa=@a
from
where

select top 100000 id=identity(int,0,1) into #a from syscolumns a,syscolumns b
select right('000000'+id,6),newid() from #a

select a.1,a.2-isnull(b.2,0) from a left join b on a.name=b.name

create table #temp(id varchar(9),pwd defalut newid())
create prco myprco
as
begin
declare @id int,@i int
set @id=0
while @id<100000
begin
set @i=len(@id)
while @i<len(@id)+5
insert into #temp values('0000'+right(cast(@id as varchar(9),@i)
set @i=len(@id)+1
end
end

select top 100 作者,sum(的票数) from biao group by 作者

declare @sql varcahr(8000)
select @sql=coalesce(@sql+',','')+'['+subject+']=sum(case subject when '''+subject+''' then result end)' from table
group by subject
set @sql='select name,'+@sql+' from #test group by name'
create table a(name varchar(10),subject varchar(20),result int)
select 姓名 as name,'语文' as subject,语文 as result
union all
select 姓名 as name,'数学' as subject,数学 as result
union all
select 姓名 as name,'英语' as subject,英语 as result
declare @sql varchar(8000)
select @sql=coalesce(@sql+',','')+'['''+月份+'''金额]=sum(
case 月份 when '''+月份''' then 工资金额 end),['''+月份+'''基金]=sum(case 月份 when '''+月份+'''then 工资基金 end)' from biao
group by 月份
set @sql='select 类型,'+@sql+' from (select 类型=''工资'',
月份,工资金额,工资基金 from biao union all select 类型=''扣除'',月份,扣除金额,扣除基金) b group by 类型

select distinct * from biao a where not exists(select 1 from biao where biao.设备号=a.设备号 and biao.time>a.time)

create function myfun(@id int)
returns varchar(8000)
as
begin
declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+name from biao where id=@id
return @sql
end
select distinct id,name=myfun(id) from biao

select @sql='select'+col_name(object_id('tb'),2)+'from table'

create function myfunction(@component varchar(20))
returns varchar(10)
as
begin

while exists(select 1 from bom where component=@component)
begin
select @component=stock from bom where component=@component
end
return @component
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: