假设我有10个表,我需要编写一个简单的LOG条目,说明每个表有多少条记录。
像
这样的东西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)
答案 0 :(得分:11)
或者将其汇总到:
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
答案 1 :(得分:2)
由于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中,这很痛苦 - 这就是为什么将这些计数返回到前端应用并在那里进行格式化和日志编写可能要容易得多。
答案 2 :(得分:1)
尝试
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 '+@table1count +' records, Table2 has '+@table2count+' records, ...'
Insert INTO LOG (msg) VALUES (@msg)
当然我只给你前两张桌子,你可以用其他技术。