데이터베이스 데이터 초기화의 예

/****
         
@author zdw
*****/
ALTER proc [dbo].[init_all]
as
--     
exec dbo.init_city
--    
exec dbo.init_airport
--    
exec dbo.init_airline
--      
exec dbo.init_other
--  
exec dbo.init_flight
--     
exec dbo.init_ticket
---        
exec  dbo.init_airtranhis
/**
                   
@author ZDW
@createtime 2012-01-05
***/
ALTER PROC [dbo].[p_randomN]
@count INT=18
as
 SELECT RIGHT(20000 + CONVERT(bigint, ABS(CHECKSUM(NEWID()))), @count)
ALTER  PROC [dbo].[p_randomName]
@name VARCHAR(20)='' OUTPUT
  as   
  BEGIN
		declare @firstNames varchar(max)    /*        */
		declare @lastNames varchar(max)     /*        */
		set @firstNames = ' , , , , , , , , , , , , , , , , , , , , , , , , , , ' /*         */
		set @lastNames = ' , , , , , , , , , , , , , , , , '    /*         */
		declare @lastNamesLength int
		declare @firstNamesLength int
		set @lastNamesLength = (LEN(@lastNames) - 1) / 2 + 1
		set @firstNamesLength = (LEN(@firstNames) - 1) / 2 + 1
		 
		declare @firstNameRandom int
		declare @lastNameRandom int
		 
		declare @resultFullNames varchar(max)
		set @resultFullNames = ''
		 

			set @firstNameRandom = CEILING(rand()*@firstNamesLength)
			set @lastNameRandom = CEILING(rand()*@lastNamesLength)
			/*
				    1   ,    Index   1,  :    Index      1。
				    2   ,    Index   3,
				    3   ,    Index   5,
				...
			*/
			set @resultFullNames = @resultFullNames + ( SUBSTRING(@lastNames, @lastNameRandom * 2 - 1, 1) + SUBSTRING(@firstNames, @firstNameRandom * 2 - 1, 1))
		SET @name=@resultFullNames
		--SELECT @name
  end   
/***            ***/
TRUNCATE TABLE Travelroute
DECLARE @i INT=1,@sn VARCHAR(50),@random INT=1,@emid varchar(50)
WHILE @i <= 2000
BEGIN
	--  10   
	SELECT @sn=right('0000000000'+cast(@i as varchar),10)
	--  1-100      
	select @random=cast( floor(rand()*100) as int)
	select top 1 @emid=emid from expressmail order by NEWID()
	INSERT Travelroute(tlid,emid,sn,batsn,createtime,usid,fee,status)
	SELECT REPLACE(LOWER(NEWID()),'-',''),@emid,@sn,'001',GETDATE(),1,@random*100,1
	SET @i = @i +1
END
SELECT * FROM Travelroute 
/***            ***/

/***           ***/
TRUNCATE TABLE Passenger
DECLARE @name VARCHAR(10),@ic VARCHAR(50),@phone VARCHAR(20)
 set  @i =0
WHILE @i <= 1000
BEGIN
	 --  18      
	  select top 1 @ic=(abs(checksum(newid()))%9+1)*100000000000000000+abs(checksum(newid()))%100000000000000000
		from sysobjects
	--  11    
	select top 1 @phone=(abs(checksum(newid()))%9+1)*10000000000+abs(checksum(newid()))%10000000000
	--      
	EXEC p_randomName @name OUTPUT
	INSERT Passenger(psid,name,type,sn,phone,ptype,birth,psid2)
	SELECT REPLACE(LOWER(NEWID()),'-',''),@name,1,@ic,@phone,1,null,0
	SET @i = @i +1
END
SELECT * FROM Passenger 
/***           ***/

좋은 웹페이지 즐겨찾기