De variabele die wordt opgehaald is NVARCHAR. Ik denk dat dit moet werken, omdat het gewoon tekst is.
Onderstaand de code
SQL:
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
| DECLARE @email varchar(max) = 'email@email.com' /]
Declare @BodyData nvarchar(MAX)--
Declare @LevID UniqueIdentifier --= (Select TOP 1 LevID from Bestellingen where Gerapporteerd = 0)
Declare @Suppliername nvarchar(MAX)
Declare @SupplierEmail nvarchar(MAX)
Declare @SupplierAddress nvarchar(MAX)
Declare @SupplierPostalCode nvarchar(MAX)
Declare @SupplierCity nvarchar(MAX)
Declare @SupplierPhone nvarchar(MAX)
Declare @PartArticleCode nvarchar(MAX)
Declare @PartDescription nvarchar(MAX)
Declare @OrderQuantity nvarchar(MAX)
Declare @PartUnit nvarchar(MAX)
Declare @PartPricePerUnit nvarchar(MAX)
DECLARE Suppliercursor CURSOR FOR
SELECT l.email, l.naam, l.Adres, l.Postcode, l.Woonplaats, l.Telefoon from Leveranciers L
INNER JOIN Bestellingen B on B.LevID = L.ID
Where b.Gerapporteerd = 0
OPEN SupplierCursor
FETCH NEXT FROM Suppliercursor INTO @SupplierEmail, @Suppliername, @SupplierAddress, @SupplierPostalCode, @SupplierCity, @SupplierPhone
WHILE @@FETCH_STATUS = 0
BEGIN
SET @BodyData =
'
<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;">
<tr>
<td align="center">
<h1><strong> <span style="font-size: 50px; font-family:Verdana; color:Gray; ">Bestelling</span></strong></h1>
</td>
<TD align="Right" style="padding: 40px 0 30px 0;">
<img src="images/werktookniet.png" alt="Logo" width="200" height="100" style="display: block;" />
</td>
</tr>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;">
<tr>
<td>
<HR WIDTH="100%" SIZE="2" Color="Gray">
</td>
</tr>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;font-family:Verdana;">
<tr>
<td width= "120">
<strong><Font size="4">Leverancier:</strong> </font>
</td>
<td Colspan = "3">
<Font size="4">' + @Suppliername + ' </font> <!-- Mag 75 karakters lang zijn -->
</td>
</tr>
<tr>
<td width= "120">
</TD>
<td width= "260">
<I><Font size="2">' + @SupplierAddress + ' </i> </Font>
</td>
<td width= "110">
<b> Referentie: </b>
</TD>
<td>
KV / VRD
</td>
</tr>
<tr>
<td>
</td>
<td>
<i><Font size="2">' + @SupplierPostalCode +' ' + @SupplierCity + ' </i> </Font>
</td>
<td>
<b> Datum: </b>
</td>
<td>
Datum, error moet nog oplossen
</td>
</tr>
<tr>
<td>
</td>
<td>
<i> <Font size="2">[telefoon nummer] </i> </Font>
</td>
</tr>
<tr>
<td>
</td>
<td>
<i> <Font size="2"> [email] </i> </Font>
</td>
</tr>
</Table>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;">
<tr>
<td>
<HR WIDTH="100%" SIZE="2" Color="Gray">
</td>
</tr>
</table>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;font-family:Calibri; font-size:13;">
<TR>
<TD width= "50">
<b> Art. nummer </b>
</TD>
<TD width= "270">
<b> Omschrijving </b>
</TD>
<TD width= "11">
<b> Aantal </b>
</TD>
<TD width= "55">
<b> Besteleenheid </b>
</TD>
<TD width= "25">
<b> Prijs </b>
</TD>
</TR>
</TABLE>
'
DECLARE Orderlines CURSOR FOR
Select
O.Omschrijving
,O.Artikelnummer
,O.Prijs
,O.Verpakkingseenheid
,B.Aantal
from Bestellingen B
INNER JOIN Leveranciers L ON L.id = B.LevID
INNER JOIN Onderdelen O on O.ID = B.OnderdeelID
Where L.Naam = @Suppliername AND B.Gerapporteerd = 0
OPEN Orderlines
FETCH NEXT FROM Orderlines INTO @PartDescription, @PartArticleCode, @PartPricePerUnit, @PartUnit, @OrderQuantity
WHILE @@FETCH_STATUS = 0
BEGIN
SET @BodyData = @BodyData +
'
<table align="center" layout="fixed" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;font-family:Verdana; font-size:13;">
<TR>
<TD width= "50">
' + @PartArticleCode + '
</TD>
<TD width= "270">
' + @PartDescription + ' <!-- Dit mag 62 tekens lang zijn -->
</TD>
<TD width= "11">
' + @OrderQuantity + '
</TD>
<TD width= "55">
' + @PartUnit + '
</TD>
<TD width= "25">
' + @PartPricePerUnit + '
</TD>
</TR>
</TABLE>
'
FETCH NEXT FROM Orderlines INTO @PartDescription, @PartArticleCode, @PartPricePerUnit, @PartUnit, @OrderQuantity
END
CLOSE Orderlines
DEALLOCATE Orderlines
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Gmail',
@recipients = @Email,
@subject = 'Bestelling',
@body=@BodyData,
@body_format ='HTML'
END
Update Bestellingen
Set Gerapporteerd = 1
WHere LevID = (Select ID from Leveranciers Where naam = @Suppliername)
CLOSE SupplierCursor
DEALLOCATE SupplierCursor |
Omdat ik geen idee heb waar het precies mis gaat, heb ik de hele code geplaatst.
Verder denk ik dat het in het onderstaande gedeelte fout gaat:
SQL:
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
| DECLARE Orderlines CURSOR FOR
Select
O.Omschrijving
,O.Artikelnummer
,O.Prijs
,O.Verpakkingseenheid
,B.Aantal
from Bestellingen B
INNER JOIN Leveranciers L ON L.id = B.LevID
INNER JOIN Onderdelen O on O.ID = B.OnderdeelID
Where L.Naam = @Suppliername AND B.Gerapporteerd = 0
OPEN Orderlines
FETCH NEXT FROM Orderlines INTO @PartDescription, @PartArticleCode, @PartPricePerUnit, @PartUnit, @OrderQuantity
WHILE @@FETCH_STATUS = 0
BEGIN
SET @BodyData = @BodyData +
'
<table align="center" layout="fixed" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;font-family:Verdana; font-size:13;">
<TR>
<TD width= "50">
' + @PartArticleCode + '
</TD>
<TD width= "270">
' + @PartDescription + ' <!-- Dit mag 62 tekens lang zijn -->
</TD>
<TD width= "11">
' + @OrderQuantity + '
</TD>
<TD width= "55">
' + @PartUnit + '
</TD>
<TD width= "25">
' + @PartPricePerUnit + '
</TD>
</TR>
</TABLE>
' |
De reden waarom er meerdere tabellen zijn gemaakt, is omdat de laatste cursor
orderlines de eerste orderregel van de bestelling netjes in het tabel plaatst, maar de volgende orderregels werden als tekst onder de tabel geplaatst.
Al met al, vind ik dat het script vreemd reageert op iets wat naar mijn weten gewoon mogelijk moet zijn.
Maar hier komt het probleem van uren naar een scherm turen... je gaat je blind staren
Ik begrijp dat zomaar bestanden downloaden niet verstandig is, maar het uploaden van script + database leek mij de beste manier om jullie van alle informatie te voorzien. Sorry voor het ongemak in ieder geval