首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于存储过程的有关问题?

2012-01-28 
关于存储过程的问题?????createprocedureAdd_Student_Info@student_namechar(8),@student_sexchar(2),@stu

关于存储过程的问题?????
create   procedure   Add_Student_Info
@student_name   char(8),
@student_sex   char(2),
@student_age   tinyint,
@student_dempt   char(20),
@student_major   char(20),
@student_year   smalldatetime,
@student_no   char(13)   output
as  
begin
    declare   @temp   char(13)--用来提取游标中的数据
    set   @student_no= 'XH '+convert(varchar(20),Year(@student_year))
    if   Month(@student_year) <10
        set   @student_no=@student_no+ '0 '+convert(varchar(20),Month(@student_year))
    else
        set   @student_no=@student_no+convert(varchar(20),Month(@student_year))
    if   Day(@student_year) <10
        set   @student_no=@student_no+ '0 '+convert(varchar(20),Day(@student_year))
    else
        set   @student_no=@student_no+convert(varchar(20),Day(@student_year))
   
    declare   fetch_max_xh   cursor   local   scroll  
    --声明一个局部游标用来提取当前日期的最大学生学号
    for
        select   max(student_no)   from   tb_student_temp   where   student_year> =
        floor(convert(float,@student_year))   and   student_year <
        floor(convert(float,@student_year))+1;
   
    if   CURSOR_STATUS( 'local ', 'fetch_max_xh ')=-1
    --如果游标没有打开
        Open   fetch_max_xh
    Fetch   first   from   fetch_max_xh   into   @temp
    CLOSE   fetch_max_xh
    Declare   @id   int
    if   @temp   is   null
        begin
            set   @student_no=@student_no+ '001 '
        end
    else
        begin
            set   @id=convert(int,substring(rtrim(@temp),11,10))
            if   @id <9
                set   @student_no=@student_no+ '00 '+convert(varchar(20),@id+1)
            else
                if   @id <99
                    set   @student_no=@student_no+ '0 '+convert(varchar(20),@id+1)
                else
                    set   @student_no=@student_no+convert(varchar(20),@id+1)
        end
    insert   into   tb_student_temp   values(@student_no,@student_name,@student_sex,
                                                                          @student_age,
                                                                          @student_dempt,


                                                                          @student_major,
                                                                          @student_year)    
end
go


declare   @student_no   char(13)
exec   add_student_info     '陈真 ', '男 ',24, '计算机 ',
                                              '计算机科学与技术 ', '2003-06-26 ',@student_no
select   *   from   tb_student_temp


  我执行后   记录是    
XH2003     陈真     男   24   计算机     计算机科学与术   2003-06-26

怎么学号不是XH20030626001的格式??????


[解决办法]
找到问题了
@student_no char(13) output
应该定义成可变长度的变量
应该是
@student_no varchar(13) output

如果用char ,你SET第一次之后 后面的就再也加不上了,后面的会被截掉

[解决办法]

declare @student_year smalldatetime
declare @student_no varchar(10)
set @student_year= '2005-2-1 '

select @student_no= 'XH '+replace(convert(varchar(10),@student_year,120), '- ', ' ')

print @student_no

[解决办法]
type = 'P ' 说明选出的对象是存储过程
可以是下列对象类型中的一种:
C = CHECK 约束
D = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
L = 日志
FN = 标量函数
IF = 内嵌表函数
P = 存储过程
PK = PRIMARY KEY 约束(类型是 K)
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = 用户表
UQ = UNIQUE 约束(类型是 K)
V = 视图
X = 扩展存储过程

[解决办法]
exec add_student_info '陈真 ', '男 ',24, '计算机 ', '计算机科学与技术 ', '2003-06-26 ',
@student_no OUTPUT
这样试一下

热点排行