如果此查询返回值(并且它在数据库中有值),我需要返回 true
SELECT * from AllMembers a
join Cards c on c.IDMember = a.MemberID
where LEN(a.EmployeeNum) = 9
and LEFT(c.CardNumber,3) = '777'
and a.Email is not null
and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
and c.CardStatus = 1
and c.AddedTime > DATEADD(YEAR, -1 , GETDATE())
or
LEN(a.EmployeeNum) = 9
and LEFT(c.CardNumber,4) = '2010'
and a.Email is not null
and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
and c.CardStatus = 1
and c.AddedTime > DATEADD(YEAR, -1 , GETDATE())
但是写入这个值会返回 false,为什么?
string Query= string.Format(@"use Knowledge4All
IF EXISTS (
SELECT ID FROM {0}..AllMembers
JOIN Cards where IDMember = @memberID
and (where LEN(EmployeeNum) = 9
and LEFT(CardNumber,3) = '777'
and Email is not null
and LastUpdate > DATEADD(YEAR, -1 , GETDATE())
and CardStatus = 1
and AddedTime > DATEADD(YEAR, -1 , GETDATE())))
select 1
OR
IF exists(
(LEN(EmployeeNum) = 9
and LEFT(CardNumber,4) = '2010'
and Email is not null
and LastUpdate > DATEADD(YEAR, -1 , GETDATE())
and CardStatus = 1
and AddedTime > DATEADD(YEAR, -1 , GETDATE()) )
SELECT 1", DbName);
我写错了吗?朗姆酒的最佳理由是什么?
最佳答案
您可以像下面这样编写查询。
IF EXISTS(SELECT 1 from AllMembers a
join Cards c on c.IDMember = a.MemberID
where LEN(a.EmployeeNum) = 9
and LEFT(c.CardNumber,3) = '777'
and a.Email is not null
and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
and c.CardStatus = 1
and c.AddedTime > DATEADD(YEAR, -1 , GETDATE())
or
LEN(a.EmployeeNum) = 9
and LEFT(c.CardNumber,4) = '2010'
and a.Email is not null
and a.LastUpdate > DATEADD(YEAR, -1 , GETDATE())
and c.CardStatus = 1
and c.AddedTime > DATEADD(YEAR, -1 , GETDATE())
)
BEGIN
SELECT 1 as Status
END
ELSE
BEGIN
SELECT 0 AS Status
END
如果您使用ExecuteScalar()
,您将获得适当的值。
查询中的其他观察结果
1- 不需要 USE 语句。仅当连接字符串设置为不同的默认目录时才需要。
2- 您正在使用 string.format
而不替换任何内容。
but writing this value return false, why?
可能是由于条件与表中的任何记录不匹配。直接在 SSMS 中运行查询并检查输出。
关于c# - 从数据库获取值后,C# 中的查询返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54387016/