2016-07-27 5 views
2

다음과 같이 쿼리를 작성했습니다.MS SQL에서 계산 된 열을 참조하는 방법은 무엇입니까?

create table registration(
    id int identity(1000,1) , 
    first_name varchar(45) unique, 
    sur_name varchar(45), 
    address_line1 varchar(45), 
    address_line2 varchar(45), 
    state varchar(45), 
    city varchar(45), 
    email_id varchar(45), 
    contact_no varchar(45), 
    date_of_birth date, 
    apply_type varchar(45), 
    qualification varchar(45), 
    gender varchar(45), 
    password as CONVERT(VARCHAR(4),DATEPART(dd,GETDATE())) + 
     substring(CONVERT(VARCHAR(4),DATENAME(mm,GETDATE())),1,3) + 
     convert(varchar(4),FLOOR(RAND(CHECKSUM(NEWID()))*(999-100)+100)), 
    hint_question varchar(50), 
    hint_answer varchar(50), 
    user_id as substring(apply_type,1,4) + convert(char(45),id)      
     persisted primary key not null); 

create table passport(
    id int identity(1000,1), 
    Passport_Number as substring(Booklet_type,1,2) + 
     convert(varchar(100),id) persisted primary key not null, 
    user_id varchar(200) constraint fk_uid foreign key(user_id) references 
     registration(user_id) on delete cascade, 
    Type_of_Passport varchar(45), 
    Type_of_Service varchar(50), 
    Booklet_type varchar(50), 
    Address1 varchar(50), 
    Address2 varchar(50), 
    City varchar(50), 
    State varchar(50), 
    Country varchar(50)n 
    Pin int, 
    Number_of_Years int, 
    Date_Of_Application date, 
    Issue_Date date, 
    Amount int, 
    Reason_for_reissue varchar(50), 
    Expired_Date date); 

그러나 나는 다음과 같은 오류 받고 있어요 :

Column 'registration.user_id' is not the same length or scale as referencing column 'passport.user_id' in foreign key 'fk_uid'. Columns participating in a foreign key relationship must be defined with the same length and scale. 

어떻게이 문제를 해결할 수 있습니까?

답변

3

캐스트 또는 VARCHAR (200)에 등록의 USER_ID를 변환, 즉 여권의 것과 동일

user_id as CONVERT(VARCHAR(200), 
        substring(apply_type,1,4) + 
        convert(char(45),id))      
      persisted primary key not null); 
관련 문제