-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArhivirajKase.txt
113 lines (99 loc) · 3.69 KB
/
ArhivirajKase.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/****** Object: StoredProcedure [dbo].[ArhivirajKasu] Script Date: 06/11/2013 10:54:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ArhivirajKasu](
@mID as int
)
AS
/*
DECLARE @mID as int
SET @mID = 1
*/
DECLARE @month as int
SELECT @month = DATEPART(month, GETDATE()) --select @month
--DECLARE @days as int
--SET @days = 11
DECLARE crsHost CURSOR
READ_ONLY
FOR SELECT [ID],[Name] FROM Host --WHERE name in (SELECT DISTINCT Klijent FROM KasaG)
DECLARE @name varchar(40)
DECLARE @id int
OPEN crsHost
FETCH NEXT FROM crsHost INTO @id, @name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
CREATE TABLE #tmpErrors (Error int)
BEGIN TRANSACTION
if NOT exists (select * from dbo.sysobjects where id = object_id(N'[KasaGArhiva]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
SELECT [vID], mID, [RAS], [Datum], [Broj], [ParID], [Klijent], [UserName], [Placanje], [Sto], [Gotovina], [Kartica], [Cek], [Virman], [ReklRacBr], [ReklamiraoIme], [ReklamiraoJMBG]
INTO KasaGArhiva
FROM KasaG
--WHERE
WHERE DATEPART(month,Datum)<@month --DATEDIFF(DAY, Datum, GetDate()) > ABS(@days)
and Klijent = @name
--and vID < (SELECT MAX(vID) FROM KasaG WHERE Klijent = @name)
and mID = @mID
SELECT [vID], [ProID], [RedBr], [Kol], [Cena], [CSaPor], [Rabat], [Tarifa], IDENTITY(int, 1, 1) AS AutoID
INTO KasaSArhiva
FROM KasaS
WHERE vID IN (
SELECT vID
FROM KasaG
--WHERE
WHERE DATEPART(month,Datum)<@month --DATEDIFF(DAY, Datum, GetDate()) > ABS(@days)
and Klijent = @name
--and vID < (SELECT MAX(vID) FROM KasaG WHERE Klijent = @name)
and mID = @mID
)
END ELSE BEGIN
INSERT INTO [KasaGArhiva]([vID], [mID], [RAS], [Datum], [Broj], [ParID], [Klijent], [UserName],
[Placanje], [Sto], [Gotovina], [Kartica], [Cek], [Virman], [ReklRacBr], [ReklamiraoIme], [ReklamiraoJMBG])
SELECT [vID], [mID], [RAS], [Datum], [Broj], [ParID], [Klijent], [UserName],
[Placanje], [Sto], [Gotovina], [Kartica], [Cek], [Virman], [ReklRacBr], [ReklamiraoIme], [ReklamiraoJMBG]
FROM KasaG
--WHERE
WHERE DATEPART(month,Datum)<@month --DATEDIFF(DAY, Datum, GetDate()) > ABS(@days)
and Klijent = @name
--and vID < (SELECT MAX(vID) FROM KasaG WHERE Klijent = @name)
and mID = @mID
INSERT INTO [KasaSArhiva]([vID], [ProID], [RedBr], [Kol], [Cena], [CSaPor], [Rabat], [Tarifa])
SELECT [vID], [ProID], [RedBr], [Kol], [Cena], [CSaPor], [Rabat], [Tarifa]
FROM KasaS
WHERE vID IN (
SELECT vID
FROM KasaG
--WHERE
WHERE DATEPART(month,Datum)<@month --DATEDIFF(DAY, Datum, GetDate()) > ABS(@days)
and Klijent = @name
--and vID < (SELECT MAX(vID) FROM KasaG WHERE Klijent = @name)
and mID = @mID
)
END
---------------------------------------------------
DECLARE @sql as nvarchar(2000)
SET @sql = '
DELETE FROM KasaS' + CAST(@ID as varchar(10)) + '
WHERE vID IN (SELECT vID FROM KasaSArhiva)
DELETE FROM KasaG' + CAST(@ID as varchar(10)) + '
WHERE vID IN (SELECT vID FROM KasaGArhiva)'
EXEC(@sql)
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
IF @@TRANCOUNT>0 BEGIN
PRINT 'Obrisan ' + CAST(@id as varchar(5)) + ' - ' + @name + ' OK'
COMMIT TRANSACTION
END
ELSE PRINT 'GRESKA ' + CAST(@id as varchar(5)) + ' - ' + @name + ' ERROR'
DROP TABLE #tmpErrors
END
FETCH NEXT FROM crsHost INTO @id, @name
END
CLOSE crsHost
DEALLOCATE crsHost