我做的是实际处理一些数据,然后将处理后的数据插入到新表中。 但首先我需要检查目标表;如果为空,则删除表中的所有内容,然后只插入新处理的数据..
我正在使用sql server 2008 ...
任何人都可以给我提供示例sql代码来创建存储过程吗?
答案 0 :(得分:2)
create procedure SprocName
AS
BEGIN
DECLARE @ProcessedData AS TABLE (IntColumn int, CharColumn varchar(MAX))
-- get processed data
INSERT INTO @ProcessedData (IntColumn, CharColumn)
SELECT IntValue, CharValue FROM SourceTable -- WHERE your condition
-- check target and delete
IF EXISTS (SELECT * FROM TargetTable)
BEGIN
DELETE FROM TargetTable -- WHERE your condition
END
-- insert fresh
INSERT INTO TargetTable (IntColumn, CharColumn)
SELECT IntColumn, CharColumn FROM @ProcessedData
END
抱歉代码未经测试;)
答案 1 :(得分:2)