在网上看到一个有关公交线路查询的例子,表结构如下:
    ID           Stataion              Orders
    1路            鼓楼                  1
    1路            新街口                2
    1路            汽车站                3
    1路            火车站                4
    2路            新街口                1
    2路            飞机场                2
    2路            天安门                3
    3路            天安门                1
    3路            石门坎                2
    3路            驾校                  3存储过程如下:
CREATE PROC p_qry1
@id varchar(200)
as
begin
declare @e  varchar(8000)
set @e=''
select @e=@e+'->'+Station  from T_line where ID=@id order by Orders
set @e=stuff(@e,1,2,'') 
return (@e)
end
GO
执行exec p_qry1 '1路'
出现如下错误:
服务器: 消息 245,级别 16,状态 1,过程 p_qry1,行 9
将 varchar 值 '鼓楼->新街口->汽车站->火车站' 转换为数据类型为 int 的列时发生语法错误。

解决方案 »

  1.   

    存储过程不能用return返回值
    CREATE PROC p_qry1 (
    @id varchar(200),
    @e  varchar(8000)='' output )
    as
    beginselect @e=@e+'->'+Station  from T_line where ID=@id order by Orders
    set @e=stuff(@e,1,2,'') end
    GO
    调用
    declare @result as varchar(8000)
    exec p_qry1 '1',@result output
      

  2.   

    create table T_line(ID nvarchar(100),Station nvarchar(100),Orders int)
    insert into T_line 
    select '1路','鼓楼',1 union all
    select '1路','新街口',2 union all
    select '1路','汽车站',3 union all
    select '1路','火车站',4 union all
    select '2路','新街口',1 union all
    select '2路','飞机场',2 union all
    select '2路','天安门',3 union all
    select '3路','天安门',1 union all
    select '3路','石门坎',2 union all
    select '3路','驾校',3
    --drop proc p_qry1
    CREATE PROC p_qry1
    @id nvarchar(200),
    @line nvarchar(4000) output
    as
    begin
    declare @e  nvarchar(4000)
    set @e=''
    select @e=@e+'->'+Station  from T_line where ID=@id order by Orders
    set @e=stuff(@e,1,2,'') 
    set @line = @e
    end
    GOdeclare @id nvarchar(200),@line nvarchar(4000)
    select @id = '1路',@line = ''
    exec dbo.p_qry1 @id,@line output
    select @line
      

  3.   

    将 varchar 值 '鼓楼->新街口->汽车站->火车站' 转换为数据类型为 int 的列时发生语法错误。
    从这个看来,是你你连接字符串时出错的,就是一个int型与一个varchar型联接
    只要把int型的换成varchar( cast(int型 as varchar) ) 就可以了~但看你上面的,看不到那个是int型的
      

  4.   

    set @e=stuff(@e,1,2,'') ,这句话有问题,改为:select stuff(@e,1,2,''),应该就可以了
      

  5.   

    CREATE PROC p_qry1 (
    @id varchar(2000))
    as
    begin
    declare @e varchar(100)
    set @e=''
    select @e=@e+'->'+ltrim(Station)  from T_line where ID=@id order by Orders
    set @e=stuff(@e,1,2,'') 
    select @e as a
    end
    GO