在SQL Server存储过程中使用中文字符

时间:2017-08-14 19:51:17

标签: sql sql-server stored-procedures ssis

我有一个由SSIS包调用的存储过程。这个包调用许多存储过程,但是这个特定的一个是导致问题的唯一存储过程。基本上,我正在将表中的列从nchar转换为nvarchar并将这些值放入另一个表中。

值可以是英语,也可以是中文。

以下是代码:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[TransformCustomer]
AS
    DECLARE @MapDimCustomer TABLE
                            (
                                 SourceSystemId INT,
                                 ExCustomerKey NVARCHAR(15),
                                 CustomerName NVARCHAR(115),
                                 CustomerTypeName NVARCHAR(50)
                            )

    INSERT INTO @MapDimCustomer (SourceSystemId, ExCustomerKey, CustomerName, CustomerTypeName)
        SELECT
            4 As SourceSystemId,
            c.ABAN8 As ExCustomerKey,
            c.ABALPH As CustomerName,
            c.ABAT1 As CustomerTypeName
        FROM
            F0101 c
        LEFT JOIN
            GlobalDW.stage.DimCustomer gc ON c.ABAN8 = gc.ExCustomerKey 
                                          AND gc.SourceSystemID = 4
         WHERE
             gc.CustomerID IS NULL;

    SELECT * FROM @MapDimCustomer;

所以,它说的是

DECLARE @MapDimCustomer TABLE
(
  SourceSystemId int,
  ExCustomerKey nvarchar(15),
  CustomerName nvarchar(115),
  CustomerTypeName nvarchar(50)
)

INSERT INTO @MapDimCustomer(SourceSystemId, ExCustomerKey, CustomerName, CustomerTypeName)

CustomerName可以是英文或中文。当我运行它时,它将中文符号转换为?????。我知道,如果我输入客户名称,我将不得不使用N'Chinese Characters here'。有没有办法申报?

1 个答案:

答案 0 :(得分:2)

作为关于查看第一个字符以确定UNICODE值的问题的答案,它将是这样的。

SELECT 4 As SourceSystemId
    , c.ABAN8 As ExCustomerKey
    , case when UNICODE(left(c.ABALPH, 1)) > 127 then c.ABALPH else c.ABDC end As CustomerName
    , c.ABAT1 As CustomerTypeName
FROM F0101 c
LEFT JOIN GlobalDW.stage.DimCustomer gc ON c.ABAN8 = gc.ExCustomerKey 
                                    AND gc.SourceSystemID = 4
WHERE gc.CustomerID IS NULL;