Reputation: 121
What i wana do is actually process some data then insert the processed data into a new table.. but first i need to check the target table ;if empty then delete everything in the table then only insert the fresh processed data..
i'm using sql server 2008...
anyone can give me the sample sql code to create the stored procedure??
Upvotes: 2
Views: 4539
Reputation: 65391
Syntax for create stored procedure is here: http://msdn.microsoft.com/en-us/library/ms187926.aspx
Then you need to do a select, syntax is here: http://msdn.microsoft.com/en-us/library/ms189499.aspx
Next is an if, see: http://msdn.microsoft.com/en-us/library/ms182717.aspx
And finally an insert http://msdn.microsoft.com/en-us/library/ms174335.aspx
Upvotes: 2
Reputation: 2428
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
Sorry code not tested ;)
Upvotes: 2