sql 游标的使用
一个例子:从tszl表中查出数据,根据cs字段的值决定往BOOK_SERIAL表中插入几行数据。
declare @num int
declare @id varchar(60)
declare @classbm varchar(60)
set @classBm='101'
declare id cursor for select id,cs from [tszl]
open id
fetch next from id into @id,@num
while @@fetch_status!=-1
begin
while(@num>0)
begin
set @num = @num-1
INSERT INTO [BOOK_SERIAL]
([id]
,[classBm]
,[state])
values(@id,@classBm,'0')
end
fetch next from id into @id,@num
end
close id
相关文档:
-- 一: 处理未查到的空值
select
case when exists(select EndPrice from T_stock where BOMSN='0109')
then (select EndPrice from T_stock where BOMSN='0109')
else 0 end
--二:更新字段值
--strNum 数量
--strId 序号
update T_buy_Pro set Co ......
要得到某一天是星期几,需要用到 SQL Server 中的日期函数:datename()。 今天是星期几,例子 1: 1: set language N'English'
2: select datename(weekday, getdate())
3:
4: Wednesday
今天是星期几,例子 2:
1: set language N'Simplified Chinese'
......
1.多where,少having
where用来过滤行,having用来过滤组
2.多union all,少union
union删除了重复的行,因此花费了一些时间
3.多Exists,少in
Exists只检查存在性,性能比in强很多,有些朋友不会用Exists,就举个例子
例,想要得到有电话号码的人的基本信息,table2有冗余信息
select * from table1;--(id,n ......
放在这里备忘,老是忘记怎么写。
mysql> create database book;
mysql> use book;
Database changed
mysql> create table email_message(key_mail_messages INTEGER,
-> date_created VARCHAR(19),
-> date_updated varchar(19),
-> date_email varchar(19),
-> addr_from va ......