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

我是菜鸟,exec中,关于order by出错的提问!
exec( 'select   *   from   '+   @tablename1   +   'order   by   日期   desc ')

提示:在关键字   'by '   附近有语法错误。

------解决方案--------------------
表变量@tablename1传不进exec,只能用临时表替代
select * into # from @tablename1
exec( 'select * from # order by 日期 desc ')
drop table #
------解决方案--------------------
declare @sql vchar(1000)
set @sql= 'select * from '+ @tablename1 + 'order by 日期 desc '
exec(@sql)
------解决方案--------------------
原来是空格的问题:)
exec( 'select * from '+ @tablename1 + ' order by 日期 desc ') order前加空格
--------------------------------------
declare @tablename1 varchar(10)
set @tablename1 = 't '
create table t (conumber int,日期 datetime)
insert into t (conumber,日期)values(10, '2007-11-02 ')
insert into t (conumber,日期)values(2, '2007-1-03 ')
insert into t (conumber,日期)values(3, '2007-02-02 ')
insert into t (conumber,日期)values(4, '2007-07-02 ')
insert into t (conumber,日期)values(5, '2007-03-02 ')

exec( 'select * from '+ @tablename1 + ' order by 日期 desc ')
drop table t