查询中的SQL行为

时间:2018-07-19 14:04:53

标签: sql database

我创建了一个非常简单的SQL语句来获取数据库中的特定行,但是尽管我知道数据在数据库中,但是首先我没有任何数据。

所以我以另一种方式再次尝试,然后找到了条目,但现在我想知道,这些语句之间有什么区别(请参见屏幕截图)。

编辑:语句中的PolNrKorin值与结果中的值不同,因为我在将其上传到此处之前进行了更改(数据保护)

这是代码:

declare @searchParam nvarchar(8) 
set @searchParam = '04521865'

declare @searchParam2 int 
set @searchParam = 04521865

select 
    QuittungGuid, AngebotGuid, QuittungID, AntragsNr, PolNrKorin, Datum 
from 
    Quittung 
where 
    PolNrKorin = 04521865

select 
    QuittungGuid, AngebotGuid, QuittungID, AntragsNr, PolNrKorin, Datum 
from 
    Quittung 
where 
    PolNrKorin = '04521865'

select  
    QuittungGuid, AngebotGuid, QuittungID, AntragsNr, PolNrKorin, Datum 
from 
    Quittung 
where 
    PolNrKorin = @searchParam 

select  
    QuittungGuid, AngebotGuid, QuittungID, AntragsNr, PolNrKorin, Datum 
from 
    Quittung 
where 
    PolNrKorin = @searchParam2

sql statement

2 个答案:

答案 0 :(得分:0)

这可能是第二个声明,应设置@ searchParam2。 @searchParam似乎设置了两次。

答案 1 :(得分:-1)

您在WHERE子句中指定的ID周围的单引号迫使搜索针对ID以文本形式(而不是整数形式)执行。如果数据以数字的TEXT表示形式存储在表中,那么您将需要在字段周围使用单引号在WHERE子句中对其进行过滤。