存储过程生成一个16位的随机数,并为每个银行的每个用户存储它

时间:2013-05-12 06:00:47

标签: sql stored-procedures

存储过程以生成16位随机数并为每个用户为每个用户存储每种卡类型(借方,贷方)和每个卡名(主,签证)。请检查出了什么问题.. 只有每个银行的循环才能正常工作。而卡片类型和卡片名称的其他循环不起作用,信息只存储一种卡片类型和一个卡片名称..请检查我出错的地方..

ALTER PROCEDURE dbo.storecardinfo(@uer_id INT)
    /*
    (
    @parameter1 int = 5,
    @parameter2 datatype OUTPUT
    )
    */


AS
/* SET NOCOUNT ON */
declare
@c_type_id int ,    
@c_name_id int  ,
@b_id int , 
@c_name varchar(50) ,
@c_type varchar(50) ,
@b_name varchar(50),
@rand_no varchar(50)
begin
set @c_type_id=(select min(card_type_id)  from tbl_card_type);
set @c_name_id=( select min(card_id) from tbl_card_master);
set @b_id=(select min(bank_id) from tbl_bank_master);
while @c_type_id is not null
begin
while @c_name_id is not null
begin
while @b_id is not null
begin
set @c_name=(select card_name from tbl_card_master where card_id=@c_name_id);
set @c_type=(select card_type_name from tbl_card_type where card_type_id=@c_type_id);
set @b_name=(select bank_name from tbl_bank_master where bank_id=@b_id);
set @rand_no=(SELECT RAND( (DATEPART(mm, GETDATE()) * 100000 )
+ (DATEPART(ss, GETDATE()) * 1000 )
+ DATEPART(ms, GETDATE()) ));
insert into tbl_card_info(user_id,card_name,card_type,card_bank,card_number) values(@uer_id,@c_name,@c_type,@b_name,@rand_no);

set @b_id=(select min(bank_id) from tbl_bank_master where (@b_id<bank_id));
end
set @c_name_id=(select min(card_id) from tbl_card_master where (@c_name_id<card_id));
end
set @c_type_id=(select min(card_type_id) from tbl_card_type where (@c_type_id<card_type_id));
end
end

1 个答案:

答案 0 :(得分:0)

您需要重置外部循环上的值,因为内部循环后的值(@b_id,@ c_name_id)为NULL。

ALTER PROCEDURE dbo.storecardinfo(@uer_id INT)
    /*
    (
    @parameter1 int = 5,
    @parameter2 datatype OUTPUT
    )
    */
AS
/* SET NOCOUNT ON */
declare @c_type_id int,    
        @c_name_id int,
        @b_id int, 
        @c_name varchar(50),
        @c_type varchar(50),
        @b_name varchar(50),
        @rand_no varchar(50)
begin
  set @c_type_id=(select min(card_type_id)  from tbl_card_type);
  set @c_name_id=(select min(card_id) from tbl_card_master);
  set @b_id=(select min(bank_id) from tbl_bank_master);
while @c_type_id is not null
  begin
    while @c_name_id is not null
      begin
        while @b_id is not null
          begin
            set @c_name=(select card_name from tbl_card_master where card_id=@c_name_id);
            set @c_type=(select card_type_name from tbl_card_type where card_type_id=@c_type_id);
            set @b_name=(select bank_name from tbl_bank_master where bank_id=@b_id);
            set @rand_no=(SELECT RAND((DATEPART(mm, GETDATE()) * 100000)
                                 + (DATEPART(ss, GETDATE()) * 1000)
                                 + DATEPART(ms, GETDATE())));
            insert into tbl_card_info(user_id,card_name,card_type,card_bank,card_number)
            values(@uer_id,@c_name,@c_type,@b_name,@rand_no);

            set @b_id=(select min(bank_id) from tbl_bank_master where (@b_id<bank_id));
          end
        set @c_name_id=(select min(card_id) from tbl_card_master where (@c_name_id<card_id));
        set @b_id=(select min(bank_id) from tbl_bank_master);
      end
    set @c_type_id=(select min(card_type_id) from tbl_card_type where (@c_type_id<card_type_id));
    set @b_id=(select min(bank_id) from tbl_bank_master);
    set @c_name_id=(select min(card_id) from tbl_card_master);
  end
end

SQLFiddle上的演示