SQL2005新手开发常见问题

来源:互联网 发布:老虎证券 是人工智能 编辑:程序博客网 时间:2024/05/17 07:25

1.

insert  into aaaaa   select cdutytime1   from t_attendConfig  --查询并

插入已有表aaaaa中

2.

select cdutytime1 into aaaaa from t_attendConfig ----是查询并自动

创建新表aaaaa,并插入

3.

在表中修改查询的列的值,然后作为where条件筛选,出现错误是

因为,where和select的执行顺序

4.

删除日志表的方法, trancate table [table_name]

5.

distinct 去除重复项,如果列中有无重复的,那么没有任何效果

6.

游标的时候:创建游标declare [cursor_name] cursor  for

[exporsion]
 打开游标 open [cursor_name]
 获取游标的值赋给变量 fetch   [cursor_name] into

[variliable]

7.

使用游标时,以@@fetch_status=0作为while的条件,同时循环中的

fetch next应该作为最有一句,因为当@@fetch_status不等于0的时候

才跳出循环

8.

except 比较两个结果集,返回两个表不重复的值,并且是distinct的

结果

9.

在sql中对只声明未赋值的变量进行字符串累加或运算前,必须给变

量赋初始值,否则变量一直为null,无法运算


10.

数据库中的charindex索引的查找匹配,是从1开始的

11.

解决值为'1,2,3,4,5,6,'的主键存在性检查
(1)在数据库中可以使用exec(),将用sql拼接的查询字符串传入,就可以

解决,sql中列存在多主键的问题了如:列userid值 1,2,3,4,5,相当于在

dal中拼串的
(2)在where 条件中给字段前面添加',' ,然后用like 或者用charindex进

行匹配检查

12、

  xp_cmdshell   ‘DOS命令’
--操作文件的时候,路径为单反斜杠/
 
exec master..sp_dropextendedproc N'xp_cmdshell'
go
——————关闭sql外围管理器

exec master..sp_addextendedproc N'xp_cmdshell',N'xplog70.dll'
go
——————开启sql外围管理器的

13、

使用select @@identity获取最后一次插入成功的标识列

14、

@@identity————返回插入数据的最后一张表的最大标识列
 ident_current('table_name')——返回指定表的最大标识列

(table_name区分大小写)

15. 

 ident_seed(‘table_name’)————返回该表的标识列的种子


 ident_Incr('table_name')————返回该表标识列的增长值

16、

数据库作业————定期执行的操作(详见数据库作业)

 17.

还原数据库是,对于不同版本的数据库要从选型中设置兼容性,新

建数据库并替换掉

18.

在插入datetime数据时,将' '插入,系统会自动转成1900-1-1

00:00:00

19、

多表链接时, (table inner join    [table2]),table2可以是select返

回的结果集

20、

Union All 转换将多个输入组合到一个输出中。Union All 转换将

多个输入组合到一个输出中。
select * from ((select aid as id from ai )   union all (select  bid as id from

bi)) a

21、

substring(a,b,c)函数的截取,是在字符串a中截取从b开始c个长度

的,a的下标是从1 开始

22、

想要查出一张有三条记录的表中,某列的百分比 ,可以使用表连接

结果集的方法完成,如:表:VoteItem 

PKid    FKid     name    count

1         1         淡淡的       1  

2         1        蓝蓝的        2

3         1        嘿嘿的        3

要添加查询FKid=1,每条记录在总数量中的百分比

 select *,Convert(float,v.count)/n.total as parent

from VoteItem  v   inner join
(
select FKid,sum(count) as total
from VoteItem 
where FKid=1
group by FKid
) n on v.FKid=n.FKid