[英]Error converting data type nvarchar to bigint in SQL Server
Why is query #2 giving me an error converting data type nvarchar to bigint? 为什么查询2给我一个错误,将数据类型nvarchar转换为bigint?
Query #1: 查询1:
SELECT CAST(CAST(AsnNumber AS bigint) AS nvarchar(33)) AS AsnNumber
FROM [DB1].Schema1.Table1 (nolock)
Tried with Convert as well, but get the same error: 也尝试过使用Convert,但是会得到相同的错误:
SELECT Convert(nvarchar(33), Convert(bigint, AsnNumber)) AS AsnNumber
FROM [DB1].Schema1.Table1 (nolock)
Query #2: 查询2:
SELECT AsnNumber
FROM Query1
WHERE AsnNumber = '1777188'
Here AsnNumber
is of type nvarchar(33)
, null in Table1
此处AsnNumber
的类型为nvarchar(33)
,在Table1
null
I'm putting this as an answer, even though it doesn't really answer the question, because I can't fit this properly in a comment. 我将其作为答案,尽管它并不能真正回答问题,因为我无法在评论中适当地添加它。
I ran this against SQL Server 2008 and I don't get any errors.. 我针对SQL Server 2008运行了此程序,但未收到任何错误。
BEGIN
DECLARE @t TABLE(AsnNumber NVARCHAR(33))
INSERT INTO @t (AsnNumber) VALUES('1777188')
INSERT INTO @t (AsnNumber) VALUES('1777189')
SELECT AsnNumber FROM @t
SELECT CAST(AsnNumber AS BIGINT) as AsnNumber
FROM @t
WHERE AsnNumber = '1777188';
SELECT CAST(CAST(AsnNumber AS BIGINT) AS NVARCHAR(33)) as AsnNumber
FROM @t
WHERE AsnNumber = '1777188';
SELECT query1.*
FROM (SELECT CAST(CAST(AsnNumber AS BIGINT) AS NVARCHAR(33)) as AsnNumber FROM @t) as query1
WHERE AsnNumber = '1777188';
WITH query1 (AsnNumber) AS
(SELECT CAST(CAST(AsnNumber AS BIGINT) AS NVARCHAR(33)) as AsnNumber FROM @t)
SELECT AsnNumber FROM query1
WHERE AsnNumber = '1777188';
END
This demonstrates that your query #1 works fine, and it also demonstrates that I can use it as a subquery and I can use it in a common table expression, and still no error. 这说明您的查询#1工作正常,也说明我可以将其用作子查询,并且可以在公用表表达式中使用它,但仍然没有错误。 I have no idea why you're getting an error. 我不知道你为什么要出错。
Perhaps you can include a full set of statements to create the table, populate it with data, and then the exact query you're running that produces the error? 也许您可以包括一组完整的语句来创建表,用数据填充它,然后运行的确切查询会产生错误? If you do that, it would help me to reproduce the issue, and then I could probably explain why it's occurring. 如果这样做,将有助于我重现此问题,然后我可以解释其原因。 For now, I can't reproduce your issue. 目前,我无法重现您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.