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:
|
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_sendneccredits]
AS
BEGIN
SET NOCOUNT ON;
create table #nec1
(docnum nvarchar(25),
email nvarchar(255),
statusupdate bit)
insert into neccredits (docnum,docdate,numatcard,cardcode,cardname,doctotal,dscription,price,email)
select convert(nvarchar, r.docnum),r.docdate,r.numatcard,r.cardcode,r.cardname,r.doctotal,i.dscription,i.price,o.e_mail from [192.168.18.111].arjay_live.dbo.orin r
inner join [192.168.18.111].arjay_live.dbo.rin1 i on r.docentry = i.docentry
inner join [192.168.18.111].arjay_live.dbo.ocrd o on r.cardcode = o.cardcode
where r.indicator = 'nc' and o.u_storetype = 'ope' and convert(varchar,r.docdate,101) = convert(varchar,getdate(),101)
insert into #nec1 (docnum,email)
select docnum,email from neccredits where sendemail is null group by docnum,email
select * from #nec1
declare @invoice varchar(15),
@email varchar(255),@subject nvarchar(255)
declare invoicecursor CURSOR FAST_FORWARD FOR
select docnum,email from #nec1 where statusupdate is null
DECLARE @tableHTML NVARCHAR(MAX);\
OPEN invoicecursor
FETCH NEXT FROM invoicecursor
INTO @invoice,@email
WHILE @@FETCH_STATUS = 0
BEGIN
SET @tableHTML =
N'<H1>'+ (select numatcard from neccredits where docnum= @invoice group by numatcard) +'</H1>' +
N'<table border="1">' +
N'<tr><<th>Description</th>' +
N'<th>Credit Memo</th><th>Price</th></tr>' +
CAST ( ( SELECT td = dscription, '',
td = docnum, '',
td = price
FROM neccredits where docnum = @invoice
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
--if @@rowcount >= 1
EXEC msdb.dbo.sp_send_dbmail
@recipients=@email,
@subject = 'NEC CREDITS FOR' ,
@body = @tableHTML,
@body_format = 'HTML'
update dbo.#nec1 set statusupdate = 1 from dbo.#nec1 where docnum = @invoice
FETCH NEXT FROM invoicecursor
INTO @invoice
END
CLOSE invoicecursor
DEALLOCATE invoicecursor
update dbo.neccredits set sendemail = '1' from dbo.neccredits n inner join #nec1 ne on n.cardnum = ne.cardnum
drop table #nec1
END
GO
|