Pomoc przy zadaniu MS SQL

0

Mam do wykonania następujące zadanie:
Napisz wyzwalacz, która umożliwi dodanie
do bazy osobę z poprawnym numerem
pesel

create table osoby
(
id int identity(1,1) not null,
nazwisko varchar(40) not null,
data_urodzenia datetime not null,
pesel varchar(11)
)

Może ktoś pomóc?

0

tu masz algorytm: http://pl.wikipedia.org/wiki/PESEL#Numer_PESEL

to nie jest trudne, powodzenia.
/e a jesli Twój problem tkwi w samej formie wyzwalacza to moge tylko dokumentacje polecić.

0

create trigger psl on osoby
for
insert
as
declare @pesel varchar
if
( (cast(substring(@pesel,1,1) as tinyint)*1)
+(cast(substring(@pesel,2,1) as tinyint)*3)
+(cast(substring(@pesel,3,1) as tinyint)*7)
+(cast(substring(@pesel,4,1) as tinyint)*9)
+(cast(substring(@pesel,5,1) as tinyint)*1)
+(cast(substring(@pesel,6,1) as tinyint)*3)
+(cast(substring(@pesel,7,1) as tinyint)*7)
+(cast(substring(@pesel,8,1) as tinyint)*9)
+(cast(substring(@pesel,9,1) as tinyint)*1)
+(cast(substring(@pesel,10,1) as tinyint)*3) ) % 10
= (cast(substring(@pesel,11,1) as tinyint)*1 )

rollback
tak ma być?

0
= (cast(substring(@pesel,11,1) )

Incorrect syntax near 'cast', expected 'AS'.
Powinno być:

= (cast(substring(@pesel,11,1) As bigint)
0

Do tej pory doszedłem do czegoś takiego:
create trigger wwwww on osoby
for insert
as
declare @pesel varchar(11)
if
( (((cast(substring(@pesel,1,1) as tinyint)*9)
+(cast(substring(@pesel,2,1) as bigint)*7)
+(cast(substring(@pesel,3,1) as bigint)*3)
+(cast(substring(@pesel,4,1) as bigint)*1)
+(cast(substring(@pesel,5,1) as bigint)*9)
+(cast(substring(@pesel,6,1) as bigint)*7)
+(cast(substring(@pesel,7,1) as bigint)*3)
+(cast(substring(@pesel,8,1) as bigint)*1)
+(cast(substring(@pesel,9,1) as bigint)*9)
+(cast(substring(@pesel,10,1) as bigint)*7) ) % 10)
= 0 and (cast(substring(@pesel,11,1) as bigint)*1 ) =0)
or
10-(((cast(substring(@pesel,1,1) as tinyint)*9)
+(cast(substring(@pesel,2,1) as bigint)*7)
+(cast(substring(@pesel,3,1) as bigint)*3)
+(cast(substring(@pesel,4,1) as bigint)*1)
+(cast(substring(@pesel,5,1) as bigint)*9)
+(cast(substring(@pesel,6,1) as bigint)*7)
+(cast(substring(@pesel,7,1) as bigint)*3)
+(cast(substring(@pesel,8,1) as bigint)*1)
+(cast(substring(@pesel,9,1) as bigint)*9)
+(cast(substring(@pesel,10,1) as bigint)*7) ) % 10)
=(cast(substring(@pesel,11,1) as bigint)*1 )
rollback

wagi jakie zastosowałem to 9,7,3,1-dla wyeliminowania przypadku kiedy mod =10
Przykład dla numeru PESEL 44051401358:

14 + 34 + 70 + 95 + 11 + 34 + 70 + 91 + 13 + 35 = 101
Wyznaczamy resztę z dzielenia sumy przez 10:

101:10 = 10 reszta = 1
Jeżeli reszta = 0, to cyfra kontrolna wynosi 0. Jeżeli reszta ≠ 0, to cyfra kontrolna będzie uzupełnieniem reszty do 10, czyli w podanym przykładzie jest to cyfra 9.

10 - 1 = 9
Wynik 9 nie jest równy ostatniej cyfrze numeru PESEL, czyli 8, więc numer jest błędny.

więc pierwsza długa część sprawdza że jeśli reszta jest równa zero czy jest równa cyfrze kontrolnej,która powinna być zero
druga częśc kodu od 10 odejmuje modulo i sprawdza czy jest równe cyfrze kontrolnej

Wyzwalacz jednak przepuszcza każdy pesel,dlaczego? Czego brakuje w tym kodzie bo już nie mam pomysłu

1 użytkowników online, w tym zalogowanych: 0, gości: 1