友恒通有限公司
方式一次删除一行,并在事务日志中记录每个删除的行。TRUNCATE TABLE 则释放所有与表关联的页。因此,TRUNCATE TABLE 比 DELETE 快且需要的事务日志空间更少。
注意drop table 与delete 的区别
2、 在行集上使用delete delete from #temp where …
3、在游标的当前行上使用 DELETE
下例显示在名为 cs_dxmdm 的游标上所做的删除。它只影响当前从游标提取的单行。
DELETE FROM #dxmdm
WHERE CURRENT OF cs_dxmdm
4、据与关联表、子查询上使用delete delete ZY_BRSYK
from ZY_BRSYK a,ZYB_BRYJK b where a.syxh=b.syxh and a.brzt=9
三、update 命令
简单格式:
update table_name set 列=表达式 [FROM table_sources] [WHERE search_condition]
1、 使用简单的update
UPDATE YF_YFZKC SET djsl = 0
2、 把 WHERE 子句和 UPDATE 语句一起使用
UPDATE YF_YFZKC SET jxje=0
Where abs(jxje)>1000000
3、通过 UPDATE 语句使用来自另一个表的信息 update #temp
set ksmc =b.name
from #temp a,YY_KSBMK b where a.ksdm=b.id
第 6 页 共 53 页
友恒通有限公司
4、 在游标的当前行上使用update
update #dxmdm set zje=@zje
WHERE CURRENT OF cs_dxmdm
四、insert命令(表必须存在)
所提供的数据值必须与列的列表匹配。数据值的数目必须与列数相同,每个数据值的数据类型、精度和小数位数也必须与相应的列匹配。 1、使用 INSERT...SELECT 插入多行
insert #mzybtemp (hzxm,pzh,jzks,jzrq,jzcs,zje,ybdm,sjh,zzbz,sfzh,zddm,bjqk) select hzxm,convert(varchar(17),substring(cardno,1,10)),ksdm,
substring(sfrq,1,8),1,zje-zfyje-yhje,ybdm,sjh,substring(zhbz,1,1), substring(sfzh,1,18),zddm,substring(zhbz,2,1) from VW_MZBRJSK a (nolock)
where sfrq between @ksrq and @jssj and ybjszt=2 and ghsfbz in (0,1)
and substring(zhbz,4,1)='0' and substring(zhbz,12,1)='0' and exists(select 1 from YY_YBFLK b (nolock)
where b.ybdm=a.ybdm and b.pzlx=10)
2、 使用 INSERT...Values 插入一行。
如果没有指定列的列表,指定值的顺序必须与表或视图中的列顺序一致。 insert into #temp(syxh,jsxh) values (@syxh,@jsxh)
3、SET IDENTITY_INSERT 表 ON|OFF 允许将显式值插入表的标识列中
如果插入值大于表的当前标识值,则 SQL Server 自动将新插入值作为当前标识值使用。
CREATE TABLE products (id int IDENTITY PRIMARY KEY, product varchar(40)) GO
-- Inserting values into products table.
INSERT INTO products (product) VALUES ('screwdriver') INSERT INTO products (product) VALUES ('hammer') INSERT INTO products (product) VALUES ('saw') INSERT INTO products (product) VALUES ('shovel') GO
--删除第三行 DELETE products
第 7 页 共 53 页
友恒通有限公司
WHERE product = 'saw' GO
-- 试图插入id=3的记录,将报错
INSERT INTO products (id, product) VALUES(3, 'garden shovel') GO
-- SET IDENTITY_INSERT to ON时,能插入id=3的记录. SET IDENTITY_INSERT products ON GO
INSERT INTO products (id, product) VALUES(3, 'garden shovel'). GO
SET IDENTITY_INSERT products OFF GO
第二节 函数 一、聚合函数
SUM、AVG、COUNT、MAX 和 MIN 忽略空值,而 COUNT(*) 不忽略。
1、 count()
COUNT(*) 返回组中项目的数量。它对每行分别进行计数,包括含有空值null的行。
COUNT(ALL expression)=count(expression) 对组中的每一行都计算 expression 并返回非空值的数量。
COUNT(DISTINCT expression) 对组中的每一行都计算 expression 并返回唯一非空值的数量。
select count(*) 总行数,
count(zlf_pt) 有值的行数, count(all zlf_pt) 有值的行数,
count(distinct zlf_pt) 不重复的行数 from YY_KSBMK
总行数 有值的行数 有值的行数 不重复的行数 ----------- ----------- ----------- ----------- 205 61 61 3
2、 sum()
sum(ALL expression)=sum(expression) 对所有的非空的值求和 sum(DISTINCT expression) 返回唯一非空值的和 例:
create table #temp (aa int,bb money) insert #temp values(1,null)
第 8 页 共 53 页
友恒通有限公司
insert #temp values(2,2) insert #temp values(3,2) insert #temp values(4,3)
select sum(bb),sum(all bb),sum(distinct bb) from #temp
7.0000 7.0000 5.0000
3、 avg()
avg(ALL expression)=avg(expression) 对所有的非空的值求平均值 avg(DISTINCT expression) 返回唯一非空值的平均值
create table #temp(aa int) insert #temp values (null) insert #temp values (20) insert #temp values (30) insert #temp values (30)
select avg(aa) from #temp --26 select avg(distinct aa) from #temp --25 4、 max()、min()
二、系统函数
1、@@IDENTITY
@@IDENTITY 中包含此语句产生的最后的标识值。若此语句没有影响任何有标识列的表,则 @@IDENTITY 返回 NULL。若插入了多个行,则会产生多个标识值,@@IDENTITY 返回最后产生的标识值。
insert into SF_MZCFK(jssjh, hjxh, czyh, lrrq, patid, hzxm, ybdm, zje,zfyje,yhje,zfje)
select @sjh, @hjxh, @czyh, @now, patid, hzxm, @ybdm, @zjecf,@zfyjecf,@yhjecf,@zfje from #brxxk
if @@error<>0 or @@rowcount=0 begin
select \保存收费处方出错!\ return end
select @xhtemp=@@identity
insert into SF_CFMXK(cfxh, cd_idm, gg_idm, dxmdm, ypmc, ypdm, ypdw, dwxs, ykxs, ypfj, ylsj, ypsl, ts, cfts, zfdj, yhdj)
select @xhtemp, idm, gg_idm, dxmdm, ypmc, xxmdm, ypdw, dwxs, ykxs, ypfj, ylsj, fysl, 1, cfts, zfdj, yhdj
from #sfmx where cfxh=@cfxh
第 9 页 共 53 页
友恒通有限公司
if @@error<>0 begin
select \保存收费处方明细出错!\ return end
2、 @@ERROR
Transact-SQL 语句的执行时,如果语句执行成功,则 @@ERROR 设置为 0。若出现一个错误,则返回一条错误信息。@@ERROR 返回此错误信息代码,直到另一条 Transact-SQL 语句被执行。 update SF_MZCFK set jlzt=0, lrrq=(case when @jsrq='' then @now else @jsrq end), czyh=@czyh where jssjh=@sjh if @@error<>0 begin select \更新门诊处方信息出错!\ return end
8、 @@ROWCOUNT
返回受上一语句影响的行数, 直到另一条 Transact-SQL 语句被执行。 任何不返回行的语句将这一变量设置为 0 . Select @pzlx=pzlx
from YY_YBFLK (nolock) where ybdm=@ybdm
if @@rowcount=0 or @@error<>0 begin
select \患者费用类别不正确!\ return end
9、 CAST 和 CONVERT
CAST ( expression AS data_type )
CONVERT (data_type[(length)], expression [, style])
将某种数据类型的表达式显式转换为另一种数据类型。CAST 和 CONVERT 提供相似的功能。
第 10 页 共 53 页
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库SqlServer 常用命令说明(2)在线全文阅读。
相关推荐: