如何在SQL Server中自动将YES / NO转换为BIT?

时间:2016-07-21 06:42:05

标签: sql-server database sql-function

我想在SQL Server中覆盖系统定义的转换。是可能的。

问题陈述

我有一个大的csv文件来上传数据库中的数据。有BIT类型的列包含True/FalseYes/No1/0

当我使用批量插入True/False和' 1/0'将被视为正确的值,但Yes/No(如预期的那样)将引发转换错误。

有没有办法在没有任何循环或逐个值的情况下转换Yes/No

修改

自定义数据类型PanelMemberType

CREATE TYPE [dbo].[PanelMemberType] AS TABLE(
 [Email] [nvarchar](255) NOT NULL,
 [LocationName] [nvarchar](255) NOT NULL,
 [OptInPermission] [nvarchar](255) NOT NULL
)
GO

存储过程代码:

ALTER PROCEDURE [dbo].[PanelMemberBulkUpdate]
     @tblPanelMember PanelMemberType READONLY, 
     @PanelID int, 
     @UserID nvarchar(128)
AS
BEGIN
     SET NOCOUNT ON;

     MERGE INTO PanelMember p1
     USING @tblPanelMember p2 ON p1.Email= p2.Email

     WHEN MATCHED THEN
         UPDATE SET  
             p1.PanelID = @PanelID,
             p1.LocationID = (SELECT TOP(1) [LocationID] 
                              FROM [dbo].[Location] 
                              WHERE [LocationName] = p2.LocationName),
             p1.Email = p2.Email,
             p1.OptInPermission = CONVERT(BIT, p2.OptInPermission),
             p1.DateAdded = p1.DateAdded, 
             p1.DateLastUpdated = (SELECT GETDATE()),
             p1.LastUpdateUserID = @UserID

     WHEN NOT MATCHED THEN
          INSERT (PanelID, LocationID, Email, OptInPermission, DateAdded) 
          VALUES (@PanelID, (SELECT TOP(1) [LocationID] 
                             FROM [dbo].[Location] 
                             WHERE [LocationName] = p2.LocationName), 
                  p2.Email, CONVERT(BIT, p2.OptInPermission),
                  (SELECT GETDATE()));
END

2 个答案:

答案 0 :(得分:5)

只需应用更多逻辑,而不是试图盲目转换为bit

p1.OptInPermission = convert( bit,
      CASE p2.OptInPermission
        WHEN 'Yes' THEN 1
        WHEN 'No' THEN 0
        ELSE p2.OptInPermission END)

(或者,为了避免在MERGE的两个分支中复制此逻辑,请在源代码中执行此操作:

USING (select Email,LocationName,convert( bit,
          CASE p2.OptInPermission
            WHEN 'Yes' THEN 1
            WHEN 'No' THEN 0
            ELSE p2.OptInPermission END as OptInPermission from @tblPanelMember) p2

)

答案 1 :(得分:2)

首先将CSV文件中的记录插入临时表。

然后在表中运行实际插入,select语句应如下所示:

INSERT INTO ActualTable(column1, column2, column3)
SELECT column1
    , column2
    , CAST(CASE column3
        WHEN 'Yes' THEN 1
        WHEN 'No'  THEN 0
        ELSE column3
    END AS BIT) AS YourBitColumn
FROM StagingTable;

根据您的方法,这应该有效:

ALTER PROCEDURE [dbo].[PanelMemberBulkUpdate]
(
    @tblPanelMember PanelMemberType READONLY
    , @PanelID INT
    , @UserID NVARCHAR(128)
)
AS
BEGIN TRY
    SET NOCOUNT ON;

    MERGE INTO PanelMember p1
    USING @tblPanelMember p2
        ON p1.Email = p2.Email
    WHEN MATCHED THEN
    UPDATE
    SET p1.PanelID = @PanelID
        , p1.LocationID = (SELECT TOP (1) [LocationID] FROM [dbo].[Location] WHERE [LocationName] = p2.LocationName)
        , p1.Email = p2.Email
        , p1.OptInPermission = CONVERT(BIT, CASE p2.OptInPermission
            WHEN 'Yes' THEN 1
            WHEN 'No' THEN 0
            ELSE p2.OptInPermission
        END)
        , p1.DateAdded = p1.DateAdded
        , p1.DateLastUpdated = GETDATE()
        , p1.LastUpdateUserID = @UserID
    WHEN NOT MATCHED THEN
    INSERT (PanelID, LocationID, Email, OptInPermission, DateAdded)
    VALUES
    (
        @PanelID
        , (SELECT TOP (1) [LocationID] FROM [dbo].[Location] WHERE [LocationName] = p2.LocationName)
        , p2.Email
        , CONVERT(BIT, CASE p2.OptInPermission
            WHEN 'Yes' THEN 1
            WHEN 'No' THEN 0
            ELSE p2.OptInPermission
        END)
        , GETDATE()
    );
END TRY
BEGIN CATCH
    THROW;
END CATCH

您无法覆盖默认功能,但您可以编写自己的功能。例如:

CREATE FUNCTION dbo.ConvertBit
(
    @Input VARCHAR(5)
)
RETURNS TABLE AS RETURN
SELECT CONVERT(BIT, CASE @Input
        WHEN 'True'     THEN 1
        WHEN 'Yes'      THEN 1
        WHEN '1'        THEN 1
        WHEN 'False'    THEN 0
        WHEN 'No'       THEN 0
        WHEN '0'        THEN 0
    END AS BitColumn;

然后只需这样查询:

ALTER PROCEDURE [dbo].[PanelMemberBulkUpdate]
(
    @tblPanelMember PanelMemberType READONLY
    , @PanelID INT
    , @UserID NVARCHAR(128)
)
AS
BEGIN TRY
    SET NOCOUNT ON;

    MERGE INTO PanelMember p1
    USING (
        SELECT T.Email
            , T.LocationName
            , B.OptInPermission
        FROM @tblPanelMember AS T
        CROSS APPLY dbo.ConvertBit(T.OptInPermission) AS B(OptInPermission)
        ) AS p2
        ON p1.Email = p2.Email
    WHEN MATCHED THEN
    UPDATE
    SET p1.PanelID = @PanelID
        , p1.LocationID = (SELECT TOP (1) [LocationID] FROM [dbo].[Location] WHERE [LocationName] = p2.LocationName)
        , p1.Email = p2.Email
        , p1.OptInPermission = p2.OptInPermission
        , p1.DateAdded = p1.DateAdded
        , p1.DateLastUpdated = GETDATE()
        , p1.LastUpdateUserID = @UserID
    WHEN NOT MATCHED THEN
    INSERT (PanelID, LocationID, Email, OptInPermission, DateAdded)
    VALUES
    (
        @PanelID
        , (SELECT TOP (1) [LocationID] FROM [dbo].[Location] WHERE [LocationName] = p2.LocationName)
        , p2.Email
        , p2.OptInPermission
        , GETDATE()
    );
END TRY
BEGIN CATCH
    THROW;
END CATCH