Showing posts with label Download. Show all posts
Showing posts with label Download. Show all posts

Simple Store Procedure Code Example

USE [DataBaseNameUsing]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sp_TEST] -- Stored Procedure name
AS
BEGIN
set nocount on

-- ================ Declare parameter for query data from table source
declare @MonthforDelete decimal(18, 0)
 declare @YearforDelete nvarchar(10)
declare @ID nvarchar(10)
declare table_tmp cursor for

-- ================ Select data from table souce by unique year,month and ID
SELECT DISTINCT field_month,field_year,ID

FROM DataBaseNameSource..Table_Source

open table_tmp

-- ================ Save all select data as condition into table temp
fetch next from table_tmp
into @MonthforDelete,@YearforDelete,@ID

-- ================ Delete data from destinatin table
while @@fetch_status = 0
begin
DELETE FROM DataBaseNameDestination..Table_DataFact
WHERE ID = @ID
AND field_month = @MonthforDelete
AND field_year = @YearforDelete
fetch next from table_tmp
into @MonthforDelete,@YearforDelete,@ID
end

close table_tmp

deallocate table_tmp
END