我的查询如下:
IF OBJECT_ID('tempdb..#Temp1') IS NOT NULL BEGIN
drop table #Temp1
end
SELECT * into #Temp1
from (
SELECT 1, NULL, NULL, NULL, NULL
UNION ALL SELECT 2, NULL, NULL, NULL, NULL
UNION ALL SELECT 3, NULL, NULL, NULL, NULL
UNION ALL SELECT 4, NULL, NULL, NULL, NULL
UNION ALL SELECT 5, NULL, NULL, NULL, NULL
UNION ALL SELECT 6, NULL, NULL, NULL, NULL
UNION ALL SELECT 7, NULL, NULL, NULL, NULL
UNION ALL SELECT 8, NULL, NULL, NULL, NULL
UNION ALL SELECT 9, NULL, NULL, NULL, NULL
)
as databases (tempid, tasknr, devcat, taskop, taskcl)
IF OBJECT_ID('tempdb..#Temp1a') IS NOT NULL BEGIN
drop table #Temp1a
end
SELECT * into #Temp1a
from (
select
cast(ROW_NUMBER() OVER(PARTITION BY parent_change ORDER BY number)as int) as tempid
,number as tasknr
,category as cat
,orig_date_entered as taskop
,case when (CM3TM1.status = 'closed') then CM3TM1.date_entered else NULL end as taskcl
FROM CM3TM1
WHERE parent_change IN ('C45168')
and category = 'NEU Development'
)
as databases (tempid, tasknr, devcat, taskop, taskcl)
UPDATE
#Temp1
SET
#Temp1.tasknr = #Temp1a.tasknr,
#Temp1.devcat = #Temp1a.devcat
FROM
#Temp1a
INNER JOIN
#Temp1
ON
#Temp1.tempid = #Temp1a.tempid
我正在
Msg 245,Level 16,State 1,Line 38转换时转换失败 varchar值'T85158'到数据类型int。
T85158与'tasknr'栏有关。
我需要使用不同类型的数据来更新这样的NULL表,其中行数是可变的。我曾尝试添加,cast((number) as int) as tasknr
但它没有帮助。
请让我知道如何正常工作。
答案 0 :(得分:1)
您在创建#temp1
时没有给SQL Server使用任何类型信息,因此它(显然)决定每列都是int
类型。如果您想拥有特定类型的列,请更明确地创建表:
CREATE TABLE #temp1 (
tempid int not null,
tasknr varchar(19) null,
devcat decimal(13,4) null,
taskop int null,
taskcl xml null
)
INSERT INTO #temp1(tempid, tasknr, devcat, taskop, taskcl) VALUES
(1, NULL, NULL, NULL, NULL),
(2, NULL, NULL, NULL, NULL),
(3, NULL, NULL, NULL, NULL),
(4, NULL, NULL, NULL, NULL),
(5, NULL, NULL, NULL, NULL),
(6, NULL, NULL, NULL, NULL),
(7, NULL, NULL, NULL, NULL),
(8, NULL, NULL, NULL, NULL),
(9, NULL, NULL, NULL, NULL)
...
答案 1 :(得分:1)
达米恩正确地提出了错误的原因。以下是可能修复的另一种风格。 将脚本的上半部分修改为:
SELECT * into #Temp1
from (
SELECT 1 tempid, cast (NULL as varchar(19)) tasknr , cast(NULL as decimal(13,4)) devcat, cast(NULL as int) taskop, cast(NULL as xml) taskcl
UNION ALL SELECT 2, NULL, NULL, NULL, NULL
UNION ALL SELECT 3, NULL, NULL, NULL, NULL
UNION ALL SELECT 4, NULL, NULL, NULL, NULL
UNION ALL SELECT 5, NULL, NULL, NULL, NULL
UNION ALL SELECT 6, NULL, NULL, NULL, NULL
UNION ALL SELECT 7, NULL, NULL, NULL, NULL
UNION ALL SELECT 8, NULL, NULL, NULL, NULL
UNION ALL SELECT 9, NULL, NULL, NULL, NULL
)
.......
编辑:要DELETE
每列都为NULL的行,请使用以下脚本:
DELETE FROM #temp1 WHERE
COALESCE(tasknr,devcat,taskop,taskcl) IS NULL