将多个查询结果导出到单个csv

时间:2014-01-21 19:22:31

标签: sql sql-server export-to-csv

我正在尝试使用一个结果创建一个查询,并将其导出到csv而不是像我一直在做的多个。看起来很简单,但我无法弄清楚。

SELECT 
      database_name = DB_NAME(5)
    , log_size_mb = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
    , row_size_mb = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
    , total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2))
FROM sys.master_files WITH(NOWAIT)
WHERE database_id =(5) 
GROUP BY database_id

SELECT * FROM emp select @@ROWCOUNT 

SELECT * FROM BADGE WHERE STATUS=1 select @@ROWCOUNT
activebadges 

SELECT * FROM dbo.EVENTS select @@ROWCOUNT 
eventstotal

SELECT * FROM FAILEDRPC select @@ROWCOUNT

1 个答案:

答案 0 :(得分:0)

SELECT 
      database_name = DB_NAME(5)
    , log_size_mb = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
    , row_size_mb = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
    , total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2))
    , emp_count = (SELECT count(*) FROM emp)
    , activebadges = (SELECT count(*) FROM BADGE WHERE STATUS=1)
    , eventstotal = (SELECT count(*) FROM dbo.EVENTS)
    , failedrpc = (SELECT COUNT(*) FROM FAILEDRPC)
FROM sys.master_files WITH(NOWAIT)
WHERE database_id =(5) 
GROUP BY database_id