表的字段爲: ID、會員卡號、卡類型、密碼
會員卡 13000張 卡號不能重複
卡號要求爲:長度爲12位數 2000001013000——2000001000001
密碼隨機生成
貴賓卡:5000張 卡號不能重複
卡號要求爲:長度爲12位數 3000001005000——3000001000001
密碼爲隨機生成
金卡:1500張 卡號不能重複
卡號要求爲: 長度爲12位數 5000001001500——5000001000001
密碼爲隨機生成
白金卡:1000張 卡號不能重複
卡好要求爲:長度爲12位數 6000001001000——6000001000001
密碼爲隨機生成
鑽石卡:500張 卡號不能重複
卡號要求爲: 長度爲12位數 7000001000500——7000001000001
密碼爲隨機生產
--------------------------------------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_rand]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_rand]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v_rand]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[v_rand]
GO
--需要這樣一個視圖
create view v_rand as select re=rand()
go
/*----取得指定上下限的隨機數
--鄒建 2003.12(引用請保留此信息)--*/
/*--調用示例
select dbo.f_rand(10,100),dbo.f_rand(10,100)
--*/
create function f_rand(
@下限 int,
@上限 int
)
returns decimal(38,0)
as
begin
declare @r decimal(38,0)
select @r=cast(re*(@上限-@下限)+@下限 as decimal(38,0)) from v_rand
return(@r)
end
go
/*--應用實例,生成1000個5位的不重複隨機數--*/
--生成隨機數用到的臨時表
create table #t(隨機數 int unique) --用唯一約束保證不重複
go
--生成1000個5位的不重複隨機數(錯誤提示不要理會,這是正常的)
declare @i int
set @i=1000 --1000要要生成的隨機數的數量
while @i>0
begin
set rowcount @i
insert #t select dbo.f_rand(10000,99999) from sysobjects
set @i=@i-@@rowcount
end
set rowcount 0
--顯示結果
select count(*) from #t
go
--刪除測試
drop table #t
--自定義函數:取得指定上下限的隨機數
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_rand]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_rand]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v_rand]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[v_rand]
GO
--需要這樣一個視圖
create view v_rand as select re=rand(),re2=replace(cast(newid() as varchar(36)),'-','')
go
--自定義函數:取得指定上下限長度的隨字符串
create function f_rand(
@最小 int,--最小長度(範圍1-32)
@最大 int--最大長度(範圍1-32)
)returns varchar(32)
as
begin
declare @r varchar(32)
select @r=left(re2,re*(@最大-@最小)+@最小) from v_rand
return(@r)
end
go
--調用示例
select dbo.f_rand(2,10),dbo.f_rand(2,10)