开发者

Stored Procedure (process data then insert into another table)

开发者_如何学JAVA

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??


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 ;)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜