problem with stored procedure

  • I have a table

    Create table SINHVIEN (

    MASVnvarchar(20) not null,

    HOTENnvarchar(100) not null,

    NGAYSINHdatetime,

    DIACHInvarchar(200),

    MALOPvarchar(20),

    TENDNnvarchar(100) not null,

    MATKHAUvarbinary not null,

    constraint PK_SINHVIEN primary key (MASV)

    )

    go

    and I want to create a stored procedure to insert values for this table in security by use md5 hash function.

    CREATE PROCEDURE SP_INS_SINHVIEN

    @MASVnvarchar(20),

    @HOTENnvarchar(100),

    @NGAYSINHdatetime,

    @DIACHI nvarchar(200),

    @MALOPvarchar(20),

    @TENDN nvarchar(100),

    @MATKHAUvarchar(100)

    AS

    INSERT INTO SINHVIEN

    VALUES

    (

    @MASV,

    @HOTEN,

    @NGAYSINH,

    @DIACHI,

    @MALOP,

    @TENDN,

    HASHBYTES('MD5',@MATKHAU)

    )

    GO

    but when I try to execute this stored procedure,

    insert into SINHVIEN values ( '1012283','Vo Thi Yen Nhi', '01/01/1992', 'VietNam', 'TH2010/4', 'meomun', HashBytes('MD5', '123456'))

    I get error

    Msg 8152, Level 16, State 11, Line 1

    String or binary data would be truncated.

    The statement has been terminated.

    I debug and know the problem is HashBytes('MD5', '123456')

    how can I do to fix this problem?

  • Hi,

    Change table:

    alter table SINHVIEN

    alter column MATKHAU varbinary(max) not null

    Change SP:

    Alter PROCEDURE SP_INS_SINHVIEN

    @MASVnvarchar(20),

    @HOTENnvarchar(100),

    @NGAYSINHdatetime,

    @DIACHI nvarchar(200),

    @MALOPvarchar(20),

    @TENDN nvarchar(100),

    @MATKHAUvarchar(8000)

    AS

    INSERT INTO SINHVIEN

    VALUES

    (

    @MASV,

    @HOTEN,

    @NGAYSINH,

    @DIACHI,

    @MALOP,

    @TENDN,

    convert(varbinary(max),HASHBYTES('MD5',@MATKHAU))

    )

    GO

    Regards

    IgorMi

    Igor Micev,My blog: www.igormicev.com

  • Thank you so much!

    Send from VietNam!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply