比方說,我有10個表,我需要寫一個簡單的LOG條目,說明每個表有多少記錄。TSQL - 選擇計數(*)..到Varchar變量
喜歡的東西
Declare @msg Varchar(MAX)
Set @msg = 'Process Succeeded; Table1 has xx record, Table2 has zz records, Table3 has ww records ...'
Insert INTO LOG (msg) VALUES (@msg)
比方說,我有10個表,我需要寫一個簡單的LOG條目,說明每個表有多少記錄。TSQL - 選擇計數(*)..到Varchar變量
喜歡的東西
Declare @msg Varchar(MAX)
Set @msg = 'Process Succeeded; Table1 has xx record, Table2 has zz records, Table3 has ww records ...'
Insert INTO LOG (msg) VALUES (@msg)
或者把它捲成:
declare @Summary as VarChar(256)
select @Summary =
'Foo: ' + Cast((select Count(42) from Foo) as VarChar(10)) +
', Bar: ' + Cast((select Count(42) from Bar) as VarChar(10))
select @Summary
由於SQL Server當前沒有這樣的事情string.Format
任何支持,你要做的:
DECLARE @msg VARCHAR(MAX)
DECLARE @Table1Count INT
SELECT @Table1Count = COUNT(*) FROM dbo.Table1
DECLARE @Table2Count INT
SELECT @Table2Count = COUNT(*) FROM dbo.Table2
..... and so forth for each table you want to consider
SET @msg = 'Process Succeeded; ' +
'Table1 has ' + CAST(@Table1Count AS VARCHAR(10)) + ' rows, ' +
'Table2 has ' + CAST(@Table2Count AS VARCHAR(10)) + ' rows...' + ........
Insert INTO LOG (msg) VALUES (@msg)
眼下,在SQL Server中,這是一個痛苦 - 這就是爲什麼將這些計數返回到前端應用程序並在那裏進行格式化和日誌寫入可能更容易。
很高興聽到即將到來的功能....謝謝。 – 2012-02-27 21:59:08
嘗試
Declare @msg Varchar(2000)
,@table1count varchar (100)
,@table2count varchar (100)
Select @table1count= cast(count(*) as varchar (100)) from table1
Select @table2count= cast(count(*) as varchar (100)) from table2
Set @msg = 'Process Succeeded; Table1 has '[email protected] +' records, Table2 has '[email protected]+' records, ...'
Insert INTO LOG (msg) VALUES (@msg)
當然,我只給你的前兩個表,就可以使用同德爲techinique休息。
哇!如果你需要一個'varchar(100)'來保存它們的數量,那麼你的表格有多大**?!?! :-) – 2012-02-27 21:28:13
我只是在嘲笑。我對原始varchar(最大) – HLGEM 2012-02-27 21:36:19
的迴應,其實我仍然在笑(嘿嘿) – 2012-02-27 21:59:44
問題是什麼? – 2012-02-27 21:20:29