友恒通有限公司
6、 sp_password 原密码,新密码,’sa’ 修改sa的密码。如果密码为空,则输入null。
第六节 表、存储过程的创建
1、 create table
create table YK_YPCGZD (
xh ut_xh12 identity(1,1) not null, czyh ut_czyh not null, cjrq ut_rq16 not null, djh ut_sjh null , ksdm ut_ksdm not null, shry ut_czyh null , shrq ut_rq16 null , jlzt ut_bz not null, jzbz ut_bz not null,
memo ut_memo null , constraint PK_YK_YPCGZD primary key (xh) ) go
create index idx_djh on YK_YPCGZD(djh) create index idx_cjrq on YK_YPCGZD(cjrq)
--序号 --操作员 --创建日期 --单据号 --科室代码 --审核员 --审核日期
--记录状态 0录入1审核2作废 --记账标志 0:录入 1:记账 --memo
①IDENTITY
表示新列是标识列。当向表中添加新行时,自动将为该标识列提供一个唯一的、递增的值。标识列通常与 PRIMARY KEY 约束一起用作表的唯一行标识符。可以将 IDENTITY 属性指派给 tinyint、smallint、int、bigint、decimal(p,0) 或 numeric(p,0) 列。对于每个表只能创建一个标识列。不能对标识列使用绑定默认值和 DEFAULT 约束。必须同时指定种子和增量,或者二者都不指定。如果二者都未指定,则取默认值 (1,1)。
②关于自定义类型的说明:
execute sp_addtype ut_bz, 'smallint', null go
create default D_ut_bz as 0 go
execute sp_bindefault D_ut_bz, ut_bz go
execute sp_addtype ut_xh12, 'numeric(12,0)','NOT NULL' go
③CONSTRAINT
第 16 页 共 53 页
友恒通有限公司
是可选关键字,表示 PRIMARY KEY、NOT NULL、UNIQUE、FOREIGN KEY 或 CHECK 约束定义的开始。约束是特殊属性,用于强制数据完整性并可以为表及其列创建索引。
④PRIMARY KEY
是通过唯一索引对给定的一列或多列强制实体完整性的约束。对于每个表只能创建一个 PRIMARY KEY 约束。
⑤CLUSTERED | NONCLUSTERED
是表示为 PRIMARY KEY 或 UNIQUE 约束创建聚集或非聚集索引的关键字。PRIMARY KEY 约束默认为 CLUSTERED,UNIQUE 约束默认为 NONCLUSTERED。 在 CREATE TABLE 语句中只能为一个约束指定 CLUSTERED。
2、 drop table
3、 创建存储过程
CREATE PROC [ EDURE ] procedure_name
[ { @parameter data_type } [ = default ] [ OUTPUT ] ] [ ,...... ] AS
sql_statement
--创建临时存储过程 create proc #temp as
select * from YY_KSBMK
SQL Server 允许创建的存储过程引用尚不存在的对象。
4、 alter proc
更改已存在的存储过程
5、 drop proc
第 17 页 共 53 页
友恒通有限公司
第二篇 如何制作外挂报表
外挂报表的制作一般分三步进行:
第一节 编写相关的存储过程 一、存储过程的标准格式
例:挂号科室统计报表
if exists(select * from sysobjects where name='usp_gh_tybb_ghkstj') drop proc usp_gh_tybb_ghkstj go
create procedure usp_gh_tybb_ghkstj( @ksrq ut_rq8, @jsrq ut_rq8, @sflx ut_dm2 ) as
/**********
[版本号]4.0.0.0.0 [创建时间]2005.02.02 [作者]黄克华
[版权] Copyright ? 1998-2001上海金仕达-卫宁医疗信息技术有限公司 [描述] 挂号科室统计报表 [功能说明]
适合所有医院
增加参数类型“医保代码集”:select id \代码\名称\医保代码集\ 参数Z001:外挂报表金额部分是否包含优惠金额
注意修改时,必须同步修改usp_gh_tybb_ghfbtj(和usp_gh_tybb_ghystj),两者的区别仅在于一个是ksdm/ksmc(和ysdm/ysmc),一个是ybdm/ybsm
[参数说明]
@ksrq 开始日期 @jsrq 结束日期
@sflx 收费类型(-1全部,其他:取结帐模板) [返回值]
第 18 页 共 53 页
友恒通有限公司
[结果集、排序] 成功:结果集
错误:\错误信息\
[调用的sp]
exec usp_gh_tybb_ghkstj '20031201','20031210','-1' [调用实例]
**********/ set nocount on …………. return go
二、报表书写的常见问题
1)存在年表的情况,应该用视图不应该用表 错误写法: select *
from SF_BRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2)
正确写法: select *
from VW_MZBRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2)
2)使用聚合函数时,未包含在聚合函数中的列,必须包含再在 GROUP BY 子句中。 错误写法:
select ksdm,ysdm,sum(zje) zje from VW_MZBRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2) group by ksdm
正确写法:
select ksdm,ysdm,sum(zje) zje from VW_MZBRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2) group by ksdm,ysdm
第 19 页 共 53 页
友恒通有限公司
3)在分支中不能多次生成临时表 错误写法: If 取本地表
Select xh,zje Into #temp From SF_BRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2) else
Select xh,zje Into #temp From SF_NBRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2)
正确写法:
create table #temp ( xh int,
zje ut_je14)
if 取本地表
Insert into #temp Select *
From SF_BRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2) else
insert into #temp Select *
From SF_NBRJSK
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2)
4)生成的临时表,应注意数据类型 错误写法:
Select substring(sfrq,1,8) rq,sum(zje) mz_zje,0 zy_zje Into #temp
From VW_MZBRJSK (nolock)
where sfrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2) group by substring(sfrq,1,8)
insert into #temp
Select substring(sfrq,1,8),0 mz_zje,sum(zje) zy_zje from ZY_BRJSK (nolock)
where jsrq between @ksrq and @jsrq+’24’ and ybjszt=2 and jlzt in (0,1,2) group by substring(jsrq,1,8)
第 20 页 共 53 页