ALTER procedure [dbo].[proc_GetAllBooks]
 @order varchar(20)
as 
    declare @sqlstr varchar(max)
    set @sqlstr='select Books.*,Categories.Name as CategoryName,Publishers.Name as PublisherName from Books inner join Categories on Books.CategoryId=Categories.Id inner join Publishers on Books.PublisherId=Publishers.Id order by '+@order+' desc'
    
go我调用存储过程 但是显示不了数据 请各位大侠帮我看看

解决方案 »

  1.   

    --把@sqlstr的类型长度改成个具体的书
    declare @sqlstr varchar(4000) 
      

  2.   

    exec [proc_GetAllBooks] @order=PublishDate
    我是这样调的
      

  3.   

    ALTER procedure [dbo].[proc_GetAllBooks]
     @order varchar(20)
    as 
    declare @sqlstr varchar(max)
        set @sqlstr='select Books.*,Categories.Name as CategoryName,Publishers.Name as PublisherName from Books inner join Categories on Books.CategoryId=Categories.Id inner join Publishers on Books.PublisherId=Publishers.Id order by '+@order+' desc'
        exec(@sqlstr)
    goexec [proc_GetAllBooks] @order=PublishDate 加了一个exec(@sqlstr)语句就有数据了
      

  4.   

    ALTER procedure [dbo].[proc_GetAllBooks] 
    修改
    create  procedure [dbo].[proc_GetAllBooks] 调用使用exec ()你是在数据库执行吗?
      

  5.   

    嗯,还有一个问题就是我一开始不排序,当我为这个变量赋值的时候再排序ALTER procedure [dbo].[proc_GetAllBooks]
     @order varchar(20)
    as 
        declare @sqlstr varchar(max)
        set @sqlstr='select Books.*,Categories.Name as CategoryName,Publishers.Name as PublisherName from Books inner join     Categories on Books.CategoryId=Categories.Id inner join Publishers on Books.PublisherId=Publishers.Id'
        if(@order<>null)
          begin
        set @sqlstr=@sqlstr+' order by '+@order
          end
        exec(@sqlstr)
    这样好像不行
      

  6.   

    直接执行下面的语句有值吗?
    select *
    from Books inner join Categories on Books.CategoryId=Categories.Id 
         inner join Publishers on Books.PublisherId=Publishers.Id
      

  7.   

    if(@order IS NOT null) 
          begin 
        set @sqlstr=@sqlstr+' order by '+@order 
          end 
        exec(@sqlstr)
    你打印一下SQL看下句子正确吗?
      

  8.   

    create procedure [dbo].[proc_GetAllBooks] 
    @order varchar(20) --@order不能为空
    as 
        declare @sqlstr varchar(max) 
        set @sqlstr='
    select 
    Books.*,
    Categories.Name as CategoryName,
    Publishers.Name as PublisherName 
    from Books inner join Categories 
    on Books.CategoryId=Categories.Id 
    inner join Publishers 
    on Books.PublisherId=Publishers.Id 
    order by '+@order+' desc
    '    
    exec(@sqlstr)
    go 
      

  9.   

    报什么错?如果不报错且0行受影响,那是你的数据或者程序逻辑的问题。
    create procedure [dbo].[proc_GetAllBooks] 
    @order varchar(20)=null 
    as 
        declare @sqlstr varchar(max) 
        set @sqlstr='
    select 
    Books.*,
    Categories.Name as CategoryName,
    Publishers.Name as PublisherName 
    from Books inner join Categories 
    on Books.CategoryId=Categories.Id 
    inner join Publishers 
    on Books.PublisherId=Publishers.Id'
    +isnull(' order by '+@order,'')--可以利用isnull简化语句
        exec(@sqlstr) 
      

  10.   

    if(@order <>null) -------------if(@order IS NOT null) 
    NULL不是用<>来比较,是用IS NULL OR IS NOT NULL
      

  11.   

    老大, 就差那一点点了, 只定义了一个动态执行的SQL语句, 而没有去执行它, 加个exec( @sqlstr )就行了ALTER procedure [dbo].[proc_GetAllBooks] @order varchar(20) 
    as 
    declare @sqlstr varchar(max) 
    set @sqlstr = 'select Books.*,Categories.Name as CategoryName,Publishers.Name as PublisherName from Books inner join Categories on Books.CategoryId=Categories.Id inner join Publishers on Books.PublisherId=Publishers.Id order by '+@order+' desc'
    exec( @sqlstr ) 
    go