日期:2014-05-17  浏览次数:20551 次

求解:SQL语句无法绑定与列明无效
SELECT UserReg.username AS userid, id, tneed, adddate, hot, verify
FROM (SELECT ROW_NUMBER() OVER (ORDER BY T .adddate DESC) AS Row, T .*
FROM TrainNeed T INNER JOIN
  UserReg ON userid = UserReg.id) TT
WHERE TT.Row BETWEEN 0 AND 10

这样写提示:
---------------------------
Microsoft SQL Server Management Studio
---------------------------
SQL 执行错误。

已执行 SQL 语句: SELECT UserReg.username AS userid, id, tneed, adddate, hot, verify FROM (SELECT ROW_NUMBER() OVER (ORDER BY T .adddate DESC) AS Row, T .* FROM TrainNeed T INNER JOIN UserReg ON userid = UserReg.id) TT WHERE TT.Row BETWEEN 0 AND 10
错误源: .Net SqlClient Data Provider
错误信息: 无法绑定由多个部分组成的标识符 "UserReg.username"。
---------------------------
确定 帮助  
---------------------------
SELECT username AS userid, id, tneed, adddate, hot, verify
FROM (SELECT ROW_NUMBER() OVER (ORDER BY T .adddate DESC) AS Row, T .*
FROM TrainNeed T INNER JOIN
  UserReg ON userid = UserReg.id) TT
WHERE TT.Row BETWEEN 0 AND 10

这样写提示列明无效。愁人~希望各位高手们帮忙看看。

------解决方案--------------------
SELECT UserReg.username AS userid,

将UserRge去掉。
------解决方案--------------------
改成下面
SQL code
SELECT TT.username AS userid, id, tneed, adddate, hot, verify
FROM (SELECT ROW_NUMBER() OVER (ORDER BY T .adddate DESC) AS Row, T .*,UserReg.username 
FROM TrainNeed T INNER JOIN
  UserReg ON userid = UserReg.id) TT
WHERE TT.Row BETWEEN 0 AND 10

------解决方案--------------------
SQL code
--测试一下
SELECT 
    T.username AS userid, 
    id, 
    tneed, 
    adddate, 
    hot, 
    verify
FROM (SELECT ROW_NUMBER() OVER (ORDER BY T.adddate DESC) AS Row, T.*
FROM TrainNeed T INNER JOIN UserReg 
    ON T.userid = UserReg.id) TT
WHERE TT.Row BETWEEN 0 AND 10

------解决方案--------------------
SQL code
SELECT  username AS userid, id, tneed, adddate, hot, verify
FROM    (
          SELECT  ROW_NUMBER() OVER ( ORDER BY T.adddate DESC ) AS Row ,
                  T.*
          FROM    TrainNeed T
          INNER JOIN UserReg
          ON      userid = UserReg.id
        ) TT
WHERE   TT.Row BETWEEN 0 AND 10