加入一个sql视图和一个表

时间:2013-02-27 10:48:48

标签: sql sql-server join

我创建了一个视图,而不是在存储过程中我想用表连接它,但是我有错误:

  

对程序'vw_Security'的请求失败,因为'vw_Security'是一个视图对象。

这是我的sp:

BEGIN

  -- declare the tables we will use
  DECLARE @Temp_Order TABLE
  (
    order_id  decimal,
    TRADE_PLACE_ID  decimal,
  )

  DECLARE @Temp_Pos TABLE
  (
    Fund_Type varchar(max),
    Desk_Ticker varchar(max),
  )

  DECLARE @Temp_Tick TABLE
  (
    SyCode varchar(max),
    Group_Name varchar(max)
  )

  INSERT INTO @Temp_Order
  EXEC LNK_xxxx @fromDate = '02/26/2013' ,
          @ToDate  = null, 
          @Trader = '' , 
          @Accts = @fundname    

  INSERT INTO @Temp_Pos
  EXEC LNK_Txxxxxx

  INSERT INTO @Temp_Pos
  EXEC LNK_xxxxx

  ;WITH CTE AS 
  ( 
    SELECT DISTINCT Ticker,CREATE_DATE,COMMENTS,EXEC,POS.Weighted_Avg_Price 
    from @Temp_Order TOR
    LEFT JOIN @Temp_Pos POS ON TOR.TICKER = POS.Desk_Ticker
  )
  SELECT Group_Name 
  From LNK_xxxxxx.dbo.vw_Security 
END

1 个答案:

答案 0 :(得分:0)

您未在现有查询中加入自己的观点。您需要使用JOIN语法将视图加入CTE

;WITH CTE AS 
( 
  SELECT DISTINCT Ticker,CREATE_DATE,COMMENTS,EXEC,POS.Weighted_Avg_Price 
  from @Temp_Order TOR
  LEFT JOIN @Temp_Pos POS 
    ON TOR.TICKER = POS.Desk_Ticker
)
SELECT s.Group_Name 
From LNK_xxxxxx.dbo.vw_Security s
JOIN CTE c
  on s.someCol = c.someCol  -- replace the someCol with your columns for joining