不知道如何将这个特定的插入到sql表中

时间:2012-09-19 07:15:36

标签: sql-server-2008-r2

我有这个脚本:

 SELECT 'pro' as descript, COUNT(*) as cnt FROM Trade.TradesMen where TradesManAccountType_Value = 2 AND HasTradeListing = 1
 UNION ALL

 SELECT 'std' as descript, COUNT(*) as cnt FROM Trade.TradesMen tm
 INNER JOIN Membership.Members m ON m.MemberId = tm.MemberId
 INNER JOIN aspnet_Membership am ON am.UserId = m.AspNetUserId
 WHERE tm.TradesManAccountType_Value = 1 AND tm.HasTradeListing = 1 AND am.IsApproved = 1

 UNION ALL

 SELECT 'listed' as descript, COUNT(*) as cnt FROM Trade.TradesMen where HasTradeListing = 1

 UNION ALL

 SELECT 'all' as descript, COUNT(*) as cnt FROM Trade.TradesMen
 insert into Admin.VersionHistory values(4,cnt,CURRENT_TIMESTAMP) //is NOT correct

这会产生:

 1     pro          32549
 2     std          13096
 3     listed   230547
 4     all          231638

我想将上面的行添加到我的表中:Admin.VersionHistory,其列有VersionHistory类型int auto-increment,是ID,版本,类型为varchar(50),数据时间戳为

感谢

2 个答案:

答案 0 :(得分:1)

(使用OP的新信息更新)
从头顶看,它看起来像这样。

INSERT INTO Admin.VersionHistory (Version, NumberOf, DateAndTime)
SELECT descript, CAST(cnt AS VARCHAR), SYSDATE
FROM
(
SELECT 'pro' as descript, COUNT(*) as cnt FROM Trade.TradesMen where TradesManAccountType_Value = 2 AND HasTradeListing = 1

UNION ALL

SELECT 'std' as descript, COUNT(*) as cnt FROM Trade.TradesMen tm
INNER JOIN Membership.Members m ON m.MemberId = tm.MemberId
INNER JOIN aspnet_Membership am ON am.UserId = m.AspNetUserId
WHERE tm.TradesManAccountType_Value = 1 AND tm.HasTradeListing = 1 AND am.IsApproved = 1

UNION ALL

SELECT 'listed' as descript, COUNT(*) as cnt FROM Trade.TradesMen where HasTradeListing = 1

UNION ALL

SELECT 'all' as descript, COUNT(*) as cnt FROM Trade.TradesMen
) ;

这假设数据库自动播种VersionHistoryId列。每次插入时,都会自动插入ID号。

虽然不确定您希望通过CURRENT_TIMESTAMP列实现什么目标。我把SYSDATE作为时间戳。

NumberOf列包含计数数据。根据您的需要命名。

答案 1 :(得分:1)

insert into Admin.VersionHistory 
SELECT 'all', COUNT(*),current_timestamp FROM Trade.TradesMen