These days busy doing graduation design, busy knocking code, early found as the original "boring" of those technical words to knock again is not much significance, because it would be better to write down the time it takes to read it carefully or use the code to do a few practical examples, but it's a good idea. Mainly because too want to be a teacher! Now, do not do so do not write because of the temporary give up the plan to be a teacher, the two days of free time to review the database, turned to a previous written stored procedure "PRIMARY key Generator", SQL Server version of the previous project used, Oracle version of the later learning Oracle , no matter what, have looked at again, knocked again, in the future when the face of the time may be necessary.
SQL Server Edition
Use TEST
If EXISTS (SELECT name from sysobjects-check the system for objects with the same name as the custom stored procedure
WHERE name = ' Getno ' and type = ' P '
DROP PROCEDURE Getno
Go
CREATE PROCEDURE Getno
@precRecord varchar (10),--3 parameters. The 1th is the input parameter (table name), and the 3rd is input
--parameter (used to determine the output format); 2nd is an output parameter (the last generated primary key number)
@primarykey varchar OUTPUT, if the 3rd parameter equals 0 o'clock, primary key = track + period + number
--If the 3rd parameter equals 1 o'clock, the primary key = the word track + number
@sign smallint--If the 3rd parameter equals other times wrong
As
DECLARE @prctmpdate datetime, a temporary variable, to record intermediate variables in stored procedures
Begin
Myyear: = Extract (year from sysdate);
Mymonth: = Extract (month from sysdate);
Myday: = Extract (day from sysdate);
Nowdate:=sysdate;
--tablename:= ' AAAA ';
--sign:= ' ad ';
Select COUNT (*) into the tmpcount from Getmykey where my_tablename = tablename;
If Tmpcount=0 Then
Begin
Insert into Getmyke (my_tablename,my_ziguei,my_lastno,my_lastdate) values (tablename,sign,1,nowdate); TmpInt: = 1;
End
Else
Select My_lastdate into Lastdate from Getmykey where my_tablename=tablename;
if (Myyear>=extract (lastdate) and Mymonth>=extract (month from lastdate)) then
if (myday=extract from lastdate) then
Update Getmykey Set My_lastno = My_lastno + 1 where my_tablename=tablename;
Select My_lastno into Tmpint from Getmykey where my_tablename=tablename;
Else
if (Myday > Extract (Day from lastdate)) then
Update Getmykey Set My_lastno = 1 where my_tablename=tablename;
Update Getmykey Set my_lastdate = nowdate where my_tablename=tablename;
Tmpint: = 1;
Else
Dbms_output.put_line (' Server time changes, please check the system! ');
End If;
End If;
End If;
End If;
Tmpstr:=lpad (To_char (Tmpint), 4, ' 0 ');
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.