今天在工作中遇到一个错误,“从数据类型 nvarchar 转换为 numeric 时出错”看上去很简单,但是我确搞了好久还是不清楚为什么出错。请大侠们帮我看看吧。
数据表如下:
代码
//数据类型, 有两种‘Number’和‘Picker’
CREATE TABLE DataType
(
ID INT IDENTITY,
Code NVARCHAR(20) NOT NULL
)
INSERT INTO DataType VALUES ('Number')
INSERT INTO DataType VALUES ('Picker')
//数据类型为Picker的值
CREATE TABLE PickerValue
(
ID INT IDENTITY,
[Value] NVARCHAR(20) NOT NULL
)
INSERT INTO PickerValue VALUES ('AAA')
INSERT INTO PickerValue VALUES ('BBB')
//数据表1
CREATE TABLE DataValue
(
ID INT IDENTITY,
ParentID INT NOT NULL,
TypeID INT NOT NULL,
[Value] DECIMAL(5,1),
[PickerID] INT
)
INSERT INTO DataValue VALUES (1,1,'123.0',NULL)
INSERT INTO DataValue VALUES (1,2,NULL,1)
//数据表2
CREATE TABLE DataValue2
(
ID INT IDENTITY,
ParentID INT NOT NULL,
TypeID INT NOT NULL,
[Value] NVARCHAR(20) NOT NULL
)
INSERT INTO DataValue2 VALUES (1,1,'123.0')
INSERT INTO DataValue2 VALUES (1,2,'AAA')
CREATE TABLE DataType
(
ID INT IDENTITY,
Code NVARCHAR(20) NOT NULL
)
INSERT INTO DataType VALUES ('Number')
INSERT INTO DataType VALUES ('Picker')
//数据类型为Picker的值
CREATE TABLE PickerValue
(
ID INT IDENTITY,
[Value] NVARCHAR(20) NOT NULL
)
INSERT INTO PickerValue VALUES ('AAA')
INSERT INTO PickerValue VALUES ('BBB')
//数据表1
CREATE TABLE DataValue
(
ID INT IDENTITY,
ParentID INT NOT NULL,
TypeID INT NOT NULL,
[Value] DECIMAL(5,1),
[PickerID] INT
)
INSERT INTO DataValue VALUES (1,1,'123.0',NULL)
INSERT INTO DataValue VALUES (1,2,NULL,1)
//数据表2
CREATE TABLE DataValue2
(
ID INT IDENTITY,
ParentID INT NOT NULL,
TypeID INT NOT NULL,
[Value] NVARCHAR(20) NOT NULL
)
INSERT INTO DataValue2 VALUES (1,1,'123.0')
INSERT INTO DataValue2 VALUES (1,2,'AAA')
我说说我的意图:
DataValue2中的Value是NVARCHAR类型,他存放了DataValue中的Value何PickerID。现在我需要将DataValue(DV)和DataValue2(DV2)进行关联查询出数据类型相等并且Value相等的数据时,返回IsActive为1,否则为0.
最初我写的查询语句
代码
SELECT DISTINCT
DV.ID,
DV.TypeID,
IsActive = CASE WHEN
(DT.Code = 'Number' AND CAST(DV2.Value AS DECIMAL(5,1)) = DV.Value) OR
(DT.Code = 'Picker' AND DV2.Value = PV.Value) THEN
1
ELSE
0
END
FROM DataValue DV
INNER JOIN DataValue2 DV2 ON DV.ParentID = DV2.ParentID
INNER JOIN DataType DT ON DV.TypeID = DT.ID AND DV2.TypeID = DT.ID
LEFT JOIN PickerValue PV ON PV.ID = DV.PickerID
DV.ID,
DV.TypeID,
IsActive = CASE WHEN
(DT.Code = 'Number' AND CAST(DV2.Value AS DECIMAL(5,1)) = DV.Value) OR
(DT.Code = 'Picker' AND DV2.Value = PV.Value) THEN
1
ELSE
0
END
FROM DataValue DV
INNER JOIN DataValue2 DV2 ON DV.ParentID = DV2.ParentID
INNER JOIN DataType DT ON DV.TypeID = DT.ID AND DV2.TypeID = DT.ID
LEFT JOIN PickerValue PV ON PV.ID = DV.PickerID
本以为大功告成,谁知执行的时候出现了错误"从数据类型 nvarchar 转换为 numeric 时出错".
开始我认为SQL中会将DT.Code = 'Number' 和CAST(DV2.Value AS DECIMAL(5,1)) = DV.Value)都执行,后来我将(DT.Code = 'Number' AND CAST(DV2.Value AS DECIMAL(5,1)) = DV.Value)改为(1 = 0 AND CAST(DV2.Value AS DECIMAL(5,1)) = DV.Value),就不会出错,也就是说,当程序判读到1=0是为false时,就不会在执行后面的语句。 这就让我更加晕了。
没有办法,最后只能这样写了。
代码
SELECT DISTINCT
DV.ID,
DV.TypeID,
IsActive =
CASE WHEN DT.Code = 'Number' THEN
CASE WHEN CAST(DV.Value AS DECIMAL(5,1)) = DV2.Value THEN
1
ELSE
0
END
ELSE
CASE WHEN DT.Code = 'Picker' THEN
CASE WHEN PV.Value = DV2.Value THEN
'1'
ELSE
'0'
END
END
END
FROM DataValue DV
INNER JOIN DataValue2 DV2 ON DV.ParentID = DV2.ParentID
INNER JOIN DataType DT ON DV.TypeID = DT.ID AND DV2.TypeID = DT.ID
LEFT JOIN PickerValue PV ON PV.ID = DV.PickerID
DV.ID,
DV.TypeID,
IsActive =
CASE WHEN DT.Code = 'Number' THEN
CASE WHEN CAST(DV.Value AS DECIMAL(5,1)) = DV2.Value THEN
1
ELSE
0
END
ELSE
CASE WHEN DT.Code = 'Picker' THEN
CASE WHEN PV.Value = DV2.Value THEN
'1'
ELSE
'0'
END
END
END
FROM DataValue DV
INNER JOIN DataValue2 DV2 ON DV.ParentID = DV2.ParentID
INNER JOIN DataType DT ON DV.TypeID = DT.ID AND DV2.TypeID = DT.ID
LEFT JOIN PickerValue PV ON PV.ID = DV.PickerID
虽然执行过去了,但是真的不知道其中的原因,希望高手能帮我点点经。