-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_server.html
66 lines (52 loc) · 1.67 KB
/
sql_server.html
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
<h1>Validar RUT!<br>SQL Server 2000</h1>
<p>Enviado por German González</p>
<pre>
/****** Object: Trigger dbo.verif_rut Script Date: 01/07/03 23:01:09 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[verif_rut]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)
drop trigger [dbo].[verif_rut]
GO
/****** Object: Trigger dbo.verif_rut Script Date: 01/07/03 23:01:34 ******/
/*** como se ve en el codigo el rut se saca del atributo rut_cli de la tabla cliente ***/
/*** hay que ingresar el rut completo sin punto y guion (. -)
/*** ejemplo: 12.345.678-9 se ingresaria: 123456789 ***/
/*** ejemplo: 9.876.543-2 se ingresaria: 098765432 (hay que ingresar el 0 ya que siempre son 9 numeros ***/
CREATE TRIGGER verif_rut
ON [dbo].[cliente]
FOR INSERT
AS
declare @valor int
declare @ruti char(9)
declare @dv char
set @ruti = (select rut_cli from inserted)
set @valor = 0
if (len(@ruti))=9
begin
set @valor = @valor + convert(int,substring(@ruti,8,1))*2
set @valor = @valor + convert(int,substring(@ruti,7,1))*3
set @valor = @valor + convert(int,substring(@ruti,6,1))*4
set @valor = @valor + convert(int,substring(@ruti,5,1))*5
set @valor = @valor + convert(int,substring(@ruti,4,1))*6
set @valor = @valor + convert(int,substring(@ruti,3,1))*7
set @valor = @valor + convert(int,substring(@ruti,2,1))*2
set @valor = @valor + convert(int,substring(@ruti,1,1))*3
set @valor = @valor % 11
if (@valor = 1)
begin
set @dv='k'
end
if @valor = 0
begin
set @dv='0'
end
if @valor>1 and @valor<11
begin
set @dv=str(11-@valor,1)
end
if @dv<>substring(@ruti,9,1)
begin
raiserror('verifique su rut',16,1)
rollback transaction
end
end
GO
s</pre>