如何使用Declare @Lookup表获取查询

时间:2016-02-07 22:52:59

标签: sql sql-server

我需要使用以下代码,但是,它在上一个Select语句中给出了一个错误。我收到的错误是:

  

关键字“select”附近的语法不正确。

有人可以协助解决这个问题吗?谢谢。

declare @Lookup table(
    Id int identity(1, 1)
  , SongTitle nvarchar(512)
)

insert into @Lookup(SongTitle)
select *
from (
    values('Deuce')
         , ('Strutter')
         , ('Black_Diamond')
         , ('Parasite')
         , ('Strange_Ways')
         , ('Rock_Bottom')
         , ('God_of_Thunder')
         , ('Love_Gun')
         , ('She')
         , ('I_Stole_Your_Love')
)

select Albums.AlbumName
     , Songs.SongTitle
      , Songs.Writers
      , Songs.Vocals
      , Songs.SID
      , Songs.TheTime
from Albums A
inner join Songs S 
    on A.AID = S.AID
inner join @Lookup L
    on L.SongTitle = S.SongTitle
order by L.Id

1 个答案:

答案 0 :(得分:1)

您需要为派生表提供别名

<script>

如果为表分配别名,请使用别名而不是表名。

insert into @Lookup(SongTitle)
select *
from (
    values('Deuce')
         , ('Strutter')
         , ('Black_Diamond')
         , ('Parasite')
         , ('Strange_Ways')
         , ('Rock_Bottom')
         , ('God_of_Thunder')
         , ('Love_Gun')
         , ('She')
         , ('I_Stole_Your_Love')
) AS v; -- <<< provide alias for derived tables