sql primary key procedure
--------主鍵生成器---------
--製作人:孔凡吉
--製作日期:2007年8月14日
--------------------------
--判斷是否存在這個資料庫表
if exists(select name from sysobjects where name='primarykey_table')
drop table primarykey_table
go
--建立表
create table primarykey_table
(
pkID int not null primary key identity(1000,1),
tablename varchar(50) not null ,
producedate smalldatetime not null,
pkNumber int not null,
wordtrack varchar(3) not null
)
go
--判斷儲存過程是否存在,如果存在刪除,如果不存在建立一個新的
if exists (select * from sysobjects where name='makeprimarykey' and type='P')
drop procedure makeprimarykey
go
--建立一個新得儲存過程
create procedure makeprimarykey (@primarykey varchar(14) output,@tablename varchar(50))with encryption
as
--定義在儲存過程中的變數
declare
@today smalldatetime,
@wordtrack varchar(2),
@producedate smalldatetime,
@year varchar(2),
@month varchar(2),
@pkNumber varchar(6),
@day varchar(2)
--設定當前時間
set @today=getdate()
--看錶中是否有記錄,如果有就開始把建立時間和當前時間做比較,H?{C;SP>M2eHkR;LuPB
begin
insert into primarykey_table(tablename,producedate,pkNumber,wordtrack)
values(@tablename,@today,1,left(@tablename,2))
end
else
begin
select @producedate=producedate from primarykey_table where
if(datediff(day,@today,@producedate)<0)
begin
update primarykey_table set pkNumber=1,producedate=@today where
end
else if(datediff(day,@today,@producedate)=0)
begin
update primarykey_table set pkNumber=pkNumber+1 where
end
else if(datediff(day,@today,@producedate)>0)
begin
raiserror ('時間錯誤,請調整時間,並與管理員聯絡',16,1)
return 1
end
end
select @producedate=producedate,@wordtrack=wordtrack,@pkNumber=pkNumber from primarykey_table
where
--取出值之後進行賦值以及字串的拼接
set @wordtrack=left(@tablename,2)
set @pkNumber=case len(@pkNumber)
when 1 then '00000'+rtrim(ltrim(@pkNumber))
when 2 then '0000'+rtrim(ltrim(@pkNumber))
when 3 then '000'+rtrim(ltrim(@pkNumber))
when 4 then '00'+rtrim(ltrim(@pkNumber))
when 5 then '0'+rtrim(ltrim(@pkNumber))
when 6 then rtrim(ltrim(@pkNumber))
end
set @year=right(cast(datepart(year,ltrim(rtrim(@producedate)))as varchar(4)),2)
set @month=case len (datepart(month,@producedate))
when 1 then '0'+cast(datepart(month,ltrim(rtrim(@producedate)))as varchar(2))
when 2 then cast(datepart(month,ltrim(rtrim(@producedate)))as varchar(2))
end
set @day =case len(datepart(day,@producedate))
when 1 then '0'+cast(datepart(day,ltrim(rtrim(@producedate)))as varchar(2))
when 2 then cast(datepart(day,ltrim(rtrim(@producedate)))as varchar(2))
end
set @primarykey = @wordtrack+@year+@month+@day+@pkNumber
go
declare
@primarykey varchar(14),
@ResultRaise int
exec makeprimarykey @primarykey output,'kongguan'
select @primarykey
select @ResultRaise
[@more@]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-964668/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL Server Primary Key ConstraintsSQLServerAI
- mysql中key 、primary key 、unique key 與index區別MySqlIndex
- unique index與primary key的區別Index
- oracle資料庫primary key和unique key的異同Oracle資料庫
- 簡單分析MySQL中的primary key功能MySql
- EXECUTE IMMEDIATE dynamic sql in procedureSQL
- 分頁procedure (SQL Server)SQLServer
- 關於primary key和foreign key的問題處理
- ALTER TABLE causes auto_increment resulting key 'PRIMARY'REM
- 【SQL】15 SQL 約束(Constraints)、NOT NULL 約束、UNIQUE 約束、PRIMARY KEY 約束、FOREIGN KEY 約束、CHECK 約束、DEFAULT約束SQLAINull
- SQL 建立儲存過程PROCEDURESQL儲存過程
- ORA-02429: cannot drop index used for enforcement of unique/primary keyIndex
- PL/SQL 05 儲存過程 procedureSQL儲存過程
- DB2 PL/SQL Example: Sleep ProcedureDB2SQL
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'Error
- 如何證明INNODB輔助索引葉子結點KEY值相同的按照PRIMARY KEY排序索引排序
- mysql主從複製錯誤:Last_SQL_Error: Error 'Duplicate entry '327' for key 'PRIMARY'' on query. Default databa...MySqlASTError
- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'Error
- OGG How to handle / replicate tables with no (without) primary key (PK) or unique index (UI) (UPI) [IndexUI
- 在SQL Developer中Debug一個procedureSQLDeveloper
- 一個關於支付的SQL-ProcedureSQL
- 建立快速重新整理物化檢視使用with rowid還是with primary key?
- 帝國CMS後臺新增資訊報錯Duplicate entry xx for key PRIMARY
- SQL error 2812: Could not find stored procedure.docSQLError
- oracle procedure plsql 動態sql之動態傳遞表OracleSQL
- Procedure加密加密
- [AlwaysOn] 建立SQL Server高可用性組T-SQL語法:PRIMARY_ROLE子句SQLServer
- SQL FOREIGN KEY 約束SQL
- oracle儲存過程(procedure)中執行動態SQL小記Oracle儲存過程SQL
- Oracle Wrap ProcedureOracle
- alter package/procedurePackage
- utlrp.sql 一次性統一編譯無效物件(pl/sql procedure等)SQL編譯物件
- SQL Server中對比表數量,索引數量及procedure數量SQLServer索引
- MySQL中使用procedureMySql
- 怎樣加密procedure加密
- Renaming a Datafile in the Primary DatabaseDatabase
- standby database to primary database.Database
- 使用after create 建立trigger記錄procedure PL/SQL程式碼變更SQL