ITPUB??ì3
ITPUB论坛 » MS SQL Server » 这题难?怎么做呀?sql server 2000

标题: 这题难?怎么做呀?sql server 2000
离线 guohaiwei163
初级会员



精华贴数 0
个人空间 0
技术积分 76 (20168)
社区积分 0 (1388364)
注册日期 2007-5-14
论坛徽章:0
      
      

发表于 2007-6-6 20:30 
这题难?怎么做呀?sql server 2000

1 有订单表,需要实现它的编号,格式如下:200211030001……200222039999等
2 如何向T1中的编号字段(code varchar(20))添加一万条记录,不充许重复,规则如下:编号的数据必须从小写的a-z之间取值
3如何统计数据库中所有用户表的数据,显示格式如下:
表名       记录数
  sales       23
    student   60
     testtable   20
4.请使用事务日志恢复数据库到一个时间点
5.请设计作业进行周期性的备份数据库


__________________
不可以做朋友﹎因为彼此伤害过 ★ 不可以做敌人﹎因为彼此深爱过 所以我们成了﹎最熟悉的陌生
只看该作者    顶部
在线/呼叫 棉花糖ONE


精华贴数 0
个人空间 0
技术积分 14587 (74)
社区积分 1241 (799)
注册日期 2007-2-21
论坛徽章:44
现任管理团队成员     
      

发表于 2007-6-6 20:43 
1.定义check约束,语法看帮助
2.不允许重复,建立unique约束就行,编号的数据必须从小写的a-z之间取值,貌似也是用check,具体不是很清楚
3.create table table_count(name varchar(20),c int)
insert into table_count exec sp_msforeachtable 'select "?" as name,count(*) as c from ?'
select * from table_count ,也可以用游标来做
4.恢复到时间点,前提是有一个时间点之前的完全备份,故障恢复模型不能是simple
5.建立维护计划,或者建立job调用备份语句


__________________
换一种思路来思考问题

qq群:47823366
只看该作者    顶部
离线 kissmoon
努力飞



精华贴数 0
个人空间 0
技术积分 5901 (221)
社区积分 30 (6135)
注册日期 2005-4-13
论坛徽章:3
会员2007贡献徽章ITPUB新首页上线纪念徽章数据库板块每日发贴之星   
      

发表于 2007-6-6 20:58 
2,建立一个数据与字母的对应关系,比如1对应aa 2对应ab 27对应ba 28对应bb,然后,用一个自增长字段取值,每次取到的值按每两位使用对应关系求值


__________________
个性签名:浪费网络流量、占用页面篇幅!让平淡在记忆中成为感动!!
只看该作者    顶部
离线 guohaiwei163
初级会员



精华贴数 0
个人空间 0
技术积分 76 (20168)
社区积分 0 (1388364)
注册日期 2007-5-14
论坛徽章:0
      
      

发表于 2007-6-8 11:29 
能详细点吗?


__________________
不可以做朋友﹎因为彼此伤害过 ★ 不可以做敌人﹎因为彼此深爱过 所以我们成了﹎最熟悉的陌生
只看该作者    顶部
离线 1986ccp
初级会员



精华贴数 0
个人空间 0
技术积分 4 (151583)
社区积分 0 (1522031)
注册日期 2007-8-23
论坛徽章:0
      
      

发表于 2007-8-23 13:46 
1 有订单表,需要实现它的编号,格式如下:200211030001……200222039999等
replace(substring(convert(varchar(20),getdate(),120),1,10),’-’,’ ‘)+max(订单号)+1
2 有表t1,t2,现有一事务,在向表t1添加数据时,同时也必须向t2也添加数据,如何实现该事务
begin transaciton
insert into table1,insert into table2
commit transaction
except rollback transaction
3 如何向t1中的编号字段(code varchar(20))添加一万条记录,不充许重复,规则如下:编号的数据必须从小写的a-z之间取值
将编号字段设为主键,并设置规则。
4 如何删除表中的重复数据,请使用光标与分组的办法
delete from aa where rowid in(
select rowid from aa where rowid not in(
select max(rowid) from aa group by a having count(a)>1) and in (
select a from aa group by a having count(a)>1))
5 如何求表中相邻的两条记录的某字段的值之差

6 如何统计数据库中所有用户表的数据,显示格式如下:
表名 记录数
sales 23

7 如何删除数据库中的所有用户表(表与表之间有外键关系)

8 表a editor_id       lb2_id
   123           000
   123           003
   123           003
   456           007
   456           006
表b  lb2_id         lb2_name
    000           a
    003           b
    006           c
    007           d
显示 a   共1条 (表a内lb2_id为000的条数)
   b   共2条(表a内lb2_id为003的条数)
select count(a.lb2_id) from a
join b
on a.lb2_id=b.lb2_id where (b.lb2_id=’000’)
9 人员情况表(employee):里面有一字段文化程度(wh):包括四种情况(本科以上,大专,高中,初中以下),现在我要根据年龄字段查询统计出:表中文化程度为本科以上,大专,高中,初中以下,各有多少人,占总人数多少。
select wh as 学历,age as 年龄, count(*) as 人数,
count(*) * 100 /(select count(*) from employee) as 百分比
from employee group by wh,age
学历 年龄 人数 百分比
本科以上 20 34 14
大专 20 33 13
高中 20 33 13
初中以下 20 100 40
本科以上 21 50 20

10 现在有三个表student:(fid 学生号,fname 姓名),
subject:(fsubid 课程号,fsubname 课程名),
score(fscoreid 成绩记录号,fsubid 课程号,fstdid 学生号,fscore 成绩)
怎么能实现这个表:
姓名 英语 数学 语文 历史
张萨 78 67 89 76
王强 89 67 84 96

select a.fname as 姓名,
英语 = sum(case b.fsubname when '英语' then c.fscore end),
数学 = sum(case b.fsubname when '数学' then c.fscore end),
语文 = sum(case b.fsubname when '语文' then c.fscore end),
历史 = sum(case b.fsubname when '历史' then c.fscore end)
from student a, subject b, score c
where a.fid = c.fstdid and b.fsubid = c.fsubid group by a.fname

11 原始表的资料如下:
pid ptime pno
111111 2003-01-28 04:30:09
111111 2003-01-28 18:30:00
222222 2003-01-28 04:31:09
333333 2003-01-28 04:32:09
111111 2003-02-09 03:35:25
222222 2003-02-09 03:36:25
333333 2003-02-09 03:37:25

查询生成表
pdate 111111 222222 333333 ......
2003-01-28 04:30:09 04:31:09 04:32:09 ......
2003-01-28 18:30:00
2003-02-09 03:35:25 03:36:25 03:37:25 ......
if exists(select * from sysobjects where name='crosstab' and type='p')
drop procedure crosstab
go
create procedure crosstab
--定义参数
@strtab varchar(50),
@strgroup varchar(50),
@strcase varchar(50),
@strthen varchar(50),
@strfir varchar(50)
as
declare @strsql varchar(1000),@strtmp varchar(1000)
set @strsql='select '+@strfir
--定义游标
declare cross_cursor cursor
for
select distinct subname from crosstable
--打开游标
open cross_cursor
while (0=0)
begin
fetch next from cross_cursor into @strtmp
if (@@fetch_status<>0) break
set @strsql=@strsql+',sum(case '+@strcase+' when '''+@strtmp+''' then '+@strthen+' else 0 end) as'''+@strtmp+''''
print @strtmp
end
set @strsql=@strsql+'from '+@strtab+' group by '+@strgroup
print @strsql
execute(@strsql)
close cross_cursor
deallocate cross_cursor

12 表一(aaa)
商品名称mc 商品总量sl
a 100
b 120
表二(bbb)
商品名称mc 出库数量sl
a 10
a 20
b 10
b 20
b 30

用一条sql语句算出商品a,b目前还剩多少?

declare @aaa table (商品名称 varchar(10), 商品总量 int)
insert into @aaa values('a',100)
insert into @aaa values('b',120)

declare @bbb table (商品名称 varchar(10), 出库数量 int)
insert into @bbb values('a', 10)
insert into @bbb values('a', 20)
insert into @bbb values('b', 10)
insert into @bbb values('b', 20)
insert into @bbb values('b', 30)

select ta.商品名称,a-b as 剩余数量 from
(select 商品名称,sum(商品总量) as a
from @aaa
group by 商品名称)ta,
(select 商品名称,sum(出库数量) as b
from @bbb
group by 商品名称)tb
where ta.商品名称=tb.商品名称

select 商品名称,sum(商品总量) 剩余数量 from (select * from @aaa union all select 商品名称,-出库数量 from @bbb) a group by 商品名称

13 优化这句sql语句
update tblexltempyear
set tblexltempyear.gdqc = tblexltempmonth.gdqc
from tblexltempyear,tblexltempmonth
where tblexltempmonth.gdxm=tblexltempyear.gdxm and tblexltempmonth.txdz=tblexltempyear.txdz

(1)、加索引:
tblexltempyear(gdxm,txdz)
tblexltempmonth (gdxm,txdz)
(2)、删除无用数据
(3)、转移过时数据
(4)、加服务器内存,升级服务器
(5)、升级网络系统

update tblexltempyear
set tblexltempyear.gdqc = tblexltempmonth.gdqc
from tblexltempyear (index indexy),tblexltempmonth (index indexm)
where tblexltempmonth.gdxm=tblexltempyear.gdxm and tblexltempmonth.txdz=tblexltempyear.txdz

14 品种 日期 数量
p0001 2002-1-10 10
p0001 2002-1-10 11
p0001 2002-1-10 50
p0001 2002-1-12 9
p0001 2002-1-12 8
p0001 2002-1-12 7
p0002 2002-10-10 5
p0002 2002-10-10 7
p0002 2002-10-12 0.5
p0003 2002-10-10 5
p0003 2002-10-12 7
p0003 2002-10-12 9

结果要先按照品种汇总,再按照日期汇总,结果如下:
p0001 2002-1-10 71
p0001 2002-1-12 24
p0002 2002-10-10 12
p0002 2002-10-12 0.5
p0003 2002-10-10 5
p0003 2002-10-12 16

sql server能做出这样的汇总吗…
select pid,pdate,sum(pcount) from table group by pid,pdate order by pid

15 在分组查循中with{cube|rollup}的区别是什么?
如:
use pangu
select firm_id,p_id,sum(o_price_quantity)as sum_values
from orders
group by firm_id,p_id
with cube

use pangu
select firm_id,p_id,sum(o_price_quantity)as sum_values
from orders
group by firm_id,p_id
with rollup
的区别是什么?

cube 和 rollup 之间的区别在于:
cube 生成的结果集显示了所选列中值的所有组合的聚合。
rollup 生成的结果集显示了所选列中值的某一层次结构的聚合。
例如,简单表 inventory 中包含:
item color quantity
-------------------- -------------------- --------------------------
table blue 124
table red 223
chair blue 101
chair red 210




下列查询将生成小计报表:
select case when (grouping(item) = 1) then 'all'
else isnull(item, 'unknown')
end as item,
case when (grouping(color) = 1) then 'all'
else isnull(color, 'unknown')
end as color,
sum(quantity) as qtysum
from inventory
group by item, color with rollup

item color qtysum
-------------------- -------------------- --------------------------
chair blue 101.00
chair red 210.00
chair all 311.00
table blue 124.00
table red 223.00
table all 347.00
all all 658.00
(7 row(s) affected)
如果查询中的 rollup 关键词更改为 cube,那么 cube 结果集与上述结果相同,只是在结果集的末尾还会返回下列两行:
all blue 225.00
all red 433.00
cube 操作为 item 和 color 中值的可能组合生成行。例如,cube 不仅报告与 item 值 chair 相组合的 color 值的所有可能组合(red、blue 和 red + blue),而且报告与 color 值 red 相组合的 item 值的所有可能组合(chair、table 和 chair + table)。对于 group by 子句中右边的列中的每个值,rollup 操作并不报告左边一列(或左边各列)中值的所有可能组合。例如,rollup 并不对每个 color 值报告 item 值的所有可能组合。rollup 操作的结果集具有类似于 compute by 所返回结果集的功能;然而,
rollup 具有下列优点: rollup 返回单个结果集;compute by 返回多个结果集,而多个结果集会增加应用程序代码的复杂性。rollup 可以在服务器光标中使用;compute by 不可以。有时,查询优化器为 rollup 生成的执行计划比为 compute by 生成的更为高效。

16 假如我有两个表
表1(电话号码,是否存在)
表2(电话号码,是否拨打)
想查找表1中的电话号码是否在表2中存在,如果存在就更新表1中的是否存在字段为1。

update 表1 set 是否存在=1
where exists(select * from 表2 where 表2.电话号码 = 表1.电话号码)
17 用存储过程调用外部程序.
做成com控件
用sp_oacreate存储过程)
declare @object int
declare @hr int
declare @src varchar(255), @desc varchar(255)
exec @hr = sp_oacreate 'sqldmo.sqlserver', @object out
if @hr <> 0
begin
exec sp_oageterrorinfo @object, @src out, @desc out
select hr=convert(varbinary(4),@hr), source=@src, description=@desc
return
end


只看该作者    顶部
离线 1986ccp
初级会员



精华贴数 0
个人空间 0
技术积分 4 (151583)
社区积分 0 (1522031)
注册日期 2007-8-23
论坛徽章:0
      
      

发表于 2007-8-23 13:48 
上面是仅供参考哈```````


只看该作者    顶部
离线 aiatop
为爱执着


来自 Beijing,China
精华贴数 0
个人空间 0
技术积分 295 (6555)
社区积分 13 (9268)
注册日期 2006-5-25
论坛徽章:3
生肖徽章2007版:鸡生肖徽章2007版:鼠生肖徽章2007版:猴   
      

发表于 2007-10-29 13:16 
不错,顶一下


__________________
坚定目标,持之以恒---->Google
E-mail/MSN:cn.man.cn@hotmail.com
http://hi.baidu.com/cocis
http://aiatop.itpub.net
只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问