日期:2014-05-18  浏览次数:20695 次

求助!这个存储过程出错:
表结构:
SQL code
id    int
FName    nvarchar(50)
FSalary    decimal(18, 0)


SQL code
--创建有[输出参数] [输入参数]的存储过程;
use AdventureWorks;
go
create proc selectEmployeeNameById
    @id int =1 ,
    @N nvarchar(50)= null  
as
    select @N =t.FName from dbo.tee as t where (t.id=@id);
    return @N;



如下执行:
SQL code
 --运行 存储过程 ;
declare @Name nvarchar(50);
exec @Name= selectEmployeeNameById 2;
print @Name;


出错:
SQL code

消息 245,级别 16,状态 1,过程 selectEmployeeNameById,第 6 行
在将 nvarchar 值 '小李' 转换成数据类型 int 时失败。



------解决方案--------------------
SQL code

--创建有[输出参数] [输入参数]的存储过程;
use AdventureWorks;
go
create proc selectEmployeeNameById
    @id int =1 ,
    @N nvarchar(50)= null  output
as
    select @N =t.FName from dbo.tee as t where (t.id=@id);
    return 0;

------解决方案--------------------
return 返回的是int,改用ouput吧

------解决方案--------------------
测试数据
SQL code

CREATE  TABLE tee
(
    [id] INT NOT NULL PRIMARY KEY,
    FName    nvarchar(50),
    FSalary    decimal(18, 0)
)
INSERT INTO tee
SELECT 1,'A',500.00 UNION ALL
SELECT 2,'B',600.00 UNION ALL
SELECT 3,'C',700.00
SELECT * FROM tee
Alter proc selectEmployeeNameById
    @id int ,
    @N nvarchar(50) OUTPUT  
AS
    begin
     SET @N=(select FName from tee where [id]=@id)
    END
return 0
 --运行 存储过程 ;
declare @Name nvarchar(50)
exec selectEmployeeNameById 2 ,@Name OUTPUT
print @Name
/*------------------------
declare @Name nvarchar(50)
exec selectEmployeeNameById 2 ,@Name OUTPUT
print @Name
------------------------*/
B

------解决方案--------------------
探讨
表结构:
SQL code
id int
FName nvarchar(50)
FSalary decimal(18, 0)


SQL code
--创建有[输出参数] [输入参数]的存储过程;
use AdventureWorks;
go
create proc selectEmployeeNameById
@id int =1 ,
@N nvarc……