create procedure CreateStuUser
@StudentId varchar(50),
@StudentName varchar(50),
@StudentPwd varchar(50),
@TeacherId varchar(50),
@Message varchar(50) output,  
as
declare @newStuId varchar-- 声明变量
set @newStuId = "SD_" + @StudentId  -- 为学生ID添加标识if @StudentId is null
return "数据插入失败!学生ID不能为空"
if @StudentName is null
return "数据插入失败!学生姓名不能为空"
if @StudentPwd is null
return "数据插入失败!学生密码不能为空"
if @TeacherId is null
return "数据插入失败!老师ID不能为空"insert into Student values(@newStuId,@StudentName,@StudentPwd,@TeacherId)
return "数据插入成功!"
**************************************************************
1.这样写问题在declare @newStuId varchar报错?
2.请问这样写的存储过程规范吗?(那些判断是否为空是不是该到程序调用前在程序上检查是否为空会好点?)

解决方案 »

  1.   

    --  修改了一下,这样就应该可以了.create procedure CreateStuUser
    @StudentId varchar(50),
    @StudentName varchar(50),
    @StudentPwd varchar(50),
    @TeacherId varchar(50),
    @Message varchar(50) output 
    as
    declare @newStuId varchar           -- 声明变量
    set @newStuId = 'SD_' + @StudentId  -- 为学生ID添加标识if @StudentId is null
    set @Message='数据插入失败!学生ID不能为空'
    if @StudentName is null
    set @Message='数据插入失败!学生姓名不能为空'
    if @StudentPwd is null
    set @Message='数据插入失败!学生密码不能为空'
    if @TeacherId is null
    set @Message='数据插入失败!老师ID不能为空'if @Message is not null
            returninsert into Student values(@newStuId,@StudentName,@StudentPwd,@TeacherId)
    set @Message='数据插入成功!'
    Go
      

  2.   

    @newStuId varchar(50)最后建议加上
    GO
      

  3.   

    对,set @newStuId = 'SD_' + @StudentId  'sd_'应该为单引号