在 sql server2000 中
实例如下:
有如下表,
create table tb(a int, b int)
insert into tb values(1,   1) 
insert into tb values(1,   3) 
insert into tb values(2,   2) 
insert into tb values(2,   3)
执行语句:
(select cast(a as nvarchar) from tb)
这句有没有最外层的括号都能执行。

(SELECT a, b FROM tb ORDER BY a FOR xml auto)
这句
没有最外层的括号可以执行,
加了括号就报错?
错误信息为:在关键字 'ORDER' 附近有语法错误。我想作个字段值转换:
select REPLACE
          ( (SELECT a, b
          FROM tb
          ORDER BY a 
FOR XML AUTO) , '<tb a="', ',')加了括号就报错,下面的字符串替换没法做了!还请各位高手指条明路!!

解决方案 »

  1.   

    两个错误.
    1, 当select ... 语句做为子查询时,使用了order必须使用top

    select * from (select top ... from .. where ... order by) x
    或,不加top也不加order by
    2,sql2000下 for xml auto 子句不支持嵌套

    select * from (select ... from ... from xml auto) x
    是不被允许的.sql2005下支持for xml的嵌套.