CREATE TABLE #tmpCustomers(
[CustomerID] [int] NOT NULL,
[CustomerCode] [varchar](20) NULL,
[CustomerName] [varchar](128) NULL,
[LeftCount] [int] NULL,
[RightCount] [int] NULL,
[CreationDate][datetime]null,
)
DECLARE Customer_Cursor CURSOR FOR
SELECT customerid
FROM Customers
OPEN Customer_Cursor;
declare @left int
declare @right int
declare @customerid int
FETCH NEXT FROM Customer_Cursor into @customerid
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC dbo.CountChildren @customerId,@left out,@right out
insert into #tmpCustomers
select customerId,[CustomerCode],[CustomerName],(CONVERT(NUMERIC(38,2), Creationdate)),@left,@right from Customers where CustomerID=@customerid
FETCH NEXT FROM Customer_Cursor into @customerid;
END;
select *, case when leftcount>RightCount then RightCount else LeftCount end as Pairs from #tmpCustomers a
drop table #tmpCustomers
CLOSE Customer_Cursor;
DEALLOCATE Customer_Cursor;
以下是代码,此代码提供以下错误:
Msg 8115,Level 16,State 2,Line 24
将表达式转换为数据类型datetime的算术溢出错误。
请指导我如何设置。
答案 0 :(得分:0)
我看到两件事看起来不对劲:
NUMERIC(18,2)
值(由于CONVERT
)插入整数字段,并将int
值插入DATETIME
字段。这可能就是您在结果中看到CreationDate
的错误值的原因。我怀疑你想要这样的东西:
INSERT INTO #tmpCustomers
SELECT
customerId,
[CustomerCode],
[CustomerName],
@left,
@right,
Creationdate
FROM Customers
WHERE CustomerID=@customerid