将数据插入表变量

时间:2015-04-01 15:36:59

标签: sql sql-server

我想知道是否可以从select查询中将数据插入表变量中。在插入值之前使用变量表并自己键入它们。

我提出了以下选择查询,该查询显示修复订单的最新评论。它返回37条记录,我想知道是否可以将它们插入变量表中。

SELECT 
    a.CH_REPREF, a.CH_DATE, a.CH_CRTIME,
    b.[Latest Customer Comment], a.CH_CRUSER, a.CH_CCOMMNT
FROM 
   (SELECT 
        CH_REPREF, 
        MAX(CH_REPREF1) As [Latest Customer Comment]
    FROM 
        dbo.V_CSRPCH
    WHERE 
        CH_CCOMMNT IS NOT NULL 
        AND CH_CCOMMNT NOT LIKE 'X'
    GROUP BY 
        CH_REPREF) b,
   (SELECT 
        CH_REPREF, CH_DATE, CH_CRTIME, CH_REPREF1, 
        CH_CRUSER, CH_CCOMMNT, CH_ACCOUNT
    FROM 
        dbo.V_CSRPCH) a
WHERE 
    CH_REPREF1 = [Latest Customer Comment] 
    AND CH_ACCOUNT = 'DDCHC' 

我已经使用以下内容启动了表变量,但是我是否可以使用select查询返回的数据填充它:

Declare @tbl_last_customer_comment TABLE
(CH_REPREF nvarchar(10),
CH_DATE smalldatetime,
CH_CRTIME nvarchar(8),
Latest int,
CH_CRUSER nvarchar(8),
CH_CCOMMNT text)

1 个答案:

答案 0 :(得分:0)

只是按照“常数”这样做。表 -

INSERT INTO @tbl_last_customer_comment
SELECT a.CH_REPREF, a.CH_DATE, a.CH_CRTIME,b.[Latest Customer Comment],a.CH_CRUSER,a.CH_CCOMMNT
FROM (SELECT CH_REPREF, MAX(CH_REPREF1) As [Latest Customer Comment]
FROM dbo.V_CSRPCH
WHERE CH_CCOMMNT IS Not Null AND CH_CCOMMNT Not Like 'X'
GROUP BY CH_REPREF) b,
(SELECT CH_REPREF, CH_DATE, CH_CRTIME,CH_REPREF1, CH_CRUSER, CH_CCOMMNT, CH_ACCOUNT
FROM dbo.V_CSRPCH) a
WHERE CH_REPREF1 = [Latest Customer Comment] AND CH_ACCOUNT = 'DDCHC'