显示类型转换警告的执行计划 - ON Cond列

时间:2017-03-24 06:55:40

标签: sql-server

在表达式中获取类型转换.....可能会影响" CardinalityEstimate"在查询泛选择中。 表T1和T2都有聚簇索引。

如何克服警告?

CREATE TABLE T1
(
    KEY INT IDENTITY(1,1) NOT NULL,
    CODE VARCHAR(50), 
    DESCRIPTION VARCHAR(100),
    EXTERNAL_KEY VARCHAR(15),
    FLAG BIT
) 

INSERT INTO T1 VALUES(1,'ASS','DESC','NULL',0) 
INSERT INTO T1 VALUES(1,'ASS1','DESC','45213',1) 
INSERT INTO T1 VALUES(1,'ASS2','DESC','NULL',1) 
INSERT INTO T1 VALUES(1,'ASS3','DESC','NULL',0) 
INSERT INTO T1 VALUES(1,'ASS4','DESC','56321',1)

CREATE TABLE T2
(
    KEY INT IDENTITY(1,1) NOT NULL,
    CODE VARCHAR(50), 
    DESCRIPTION VARCHAR(100),
    EXTERNAL_KEY NUMERIC(14,0)
)

INSERT INTO T2 VALUES(1,'DSA','DESC',51256) 
INSERT INTO T2 VALUES(1,'DSA1','DESC',45213) 
INSERT INTO T2 VALUES(1,'DSA2','DESC',51256) 
INSERT INTO T2 VALUES(1,'DSA3','DESC',56321)

实际查询:

SELECT T1.KEY,T1.FLAG,T2.KEY, FROM T2 INNER JOIN T1 ON 
CAST(NULLIF(RTRIM(T1.EXTERNAL_KEY),'') AS NUMERIC(14,0)) = T2.EXTERNAL_KEY
WHERE T1.EXTERNAL_KEY IS NOT NULL

1 个答案:

答案 0 :(得分:1)

您的Sample数据和查询存在一些问题。 class MyRecord extends Immutable.Record<{ field?: string }>({ field: undefined }) { } 应更改为KEY INT IDENTITY(1,1) NOT NULL或者不要插入标识列[KEY] NOT NULL此外,当您附加“空白”时,您不会插入DB NULL但Text为null。用单引号。为了这。以下代码应该可以使用

INSERT INTO T1 VALUES('ASS','DESC','NULL',0)

但是我认为你想要NULL为NULL而不是文本所以请将你的插入从SELECT T1.[KEY],T1.FLAG,T2.[KEY] FROM T2 INNER JOIN T1 ON NULLIF(T1.EXTERNAL_KEY ,'NULL')=T2.EXTERNAL_KEY WHERE T1.EXTERNAL_KEY IS NOT NULL 更改为`INSERT INTO T1(代码,描述,EXTERNAL_KEY,FLAG)值(&#39; ASS&#39; &#39; DESC&#39;,NULL,0)

样本数据

INSERT INTO T1(CODE,DESCRIPTION,EXTERNAL_KEY,FLAG) VALUES('ASS','DESC','NULL',0)

代码

IF OBJECT_ID('tempdb..#T1') IS NOT NULL
    DROP TABLE #T1

IF OBJECT_ID('tempdb..#T2') IS NOT NULL
    DROP TABLE #T2

CREATE TABLE #T1
(
    [KEY] INT IDENTITY(1,1) NOT NULL,
    CODE VARCHAR(50), 
    DESCRIPTION VARCHAR(100),
    EXTERNAL_KEY VARCHAR(15),
    FLAG BIT
) 
GO

INSERT INTO #T1(CODE,DESCRIPTION,EXTERNAL_KEY,FLAG) VALUES('ASS','DESC',NULL,0) 
INSERT INTO #T1(CODE,DESCRIPTION,EXTERNAL_KEY,FLAG) VALUES('ASS1','DESC','45213',1) 
INSERT INTO #T1(CODE,DESCRIPTION,EXTERNAL_KEY,FLAG)VALUES('ASS2','DESC',NULL,1) 
INSERT INTO #T1(CODE,DESCRIPTION,EXTERNAL_KEY,FLAG)VALUES('ASS3','DESC',NULL,0) 
INSERT INTO #T1(CODE,DESCRIPTION,EXTERNAL_KEY,FLAG)VALUES('ASS4','DESC','56321',1)

CREATE TABLE #T2
(
    [KEY] INT IDENTITY(1,1) NOT NULL,
    CODE VARCHAR(50), 
    DESCRIPTION VARCHAR(100),
    EXTERNAL_KEY NUMERIC(14,0)
)
GO

INSERT INTO #T2 (CODE,DESCRIPTION,EXTERNAL_KEY) VALUES('DSA','DESC',51256) 
INSERT INTO #T2 (CODE,DESCRIPTION,EXTERNAL_KEY)VALUES('DSA1','DESC',45213) 
INSERT INTO #T2 (CODE,DESCRIPTION,EXTERNAL_KEY)VALUES('DSA2','DESC',51256) 
INSERT INTO #T2 (CODE,DESCRIPTION,EXTERNAL_KEY)VALUES('DSA3','DESC',56321)