导入SQL / SSIS中的性别字段

时间:2016-04-12 05:09:59

标签: sql ssis

您如何处理仅将男性(女)或女性(f)数据导入性别字段,以便在SQL或SSIS中不会填充任何潜在的无关源数据?

2 个答案:

答案 0 :(得分:1)

您可以创建列约束:

CREATE TABLE Persons
(
Id int NOT NULL,
LastName varchar(255),
FirstName varchar(255),
Gender varchar(1),
CONSTRAINT chk_Gender CHECK (Gender ='m' or Gender='f')
)

Insert into Persons values(1,'smith', 'jane', 'f');
insert into Persons values(2,'smith', 'john', SUBSTRING('male', 1, 1));
/* this will fail
INSERT INTO Persons values (3,'foo', 'bar', 'u');
*/
select * FROM Persons;

Working Fiddle

答案 1 :(得分:1)

添加检查约束很简单,例如:

CREATE TABLE #Test (
  GENDER CHAR(1) CONSTRAINT Constraint_Gender CHECK (GENDER IN ('A','B','C'))
)
INSERT INTO #Test VALUES ('A')
INSERT INTO #Test VALUES ('D')

(1 row(s) affected)
Msg 547, Level 16, State 0, Line 9
The INSERT statement conflicted with the CHECK constraint "Constraint_Gender". 

但是,您应该以与facebook等https://www.facebook.com/facebookdiversity/posts/774221582674346类似的方式来满足非二进制性别(因此在上面的示例中为A,B,C)