如何在SQL Server的where子句中使用变量. [英] How to use a variable in sql server where clause.

查看:299
本文介绍了如何在SQL Server的where子句中使用变量.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询-
我正在通过ado.net代码传递参数.

I have a query-
I am passing the parameter from ado.net code.

select s.name as [Student Name],p.depositeamount,p.receiptno
     from Student s,paymentmaster p
     where depositedate between (@ToDate) and (@FromDate)and p.accountnumber=s.account

推荐答案

提及where子句中存放的列的表名.
p.depositedate而不是depositedate避免歧义.

您正在尝试在ToDate-FromDate场景中获取数据,它应该像
FromDate-ToDate

Mention the table name for column depositedate in where clause.
p.depositedate instead depositedate avoids ambiguity.

You are trying to get data in ToDate-FromDate scenario, it should be like
FromDate-ToDate

select s.name as [Student Name],p.depositeamount,p.receiptno
     from Student s,paymentmaster p
     where p.depositedate between (@FromDate) and (@ToDate)and p.accountnumber=s.account




如果您不使用存储过程,则将date变量写在单引号中.




If you are not using stored procedure then write dates variable in single quote.

select s.name as [Student Name],p.depositeamount,p.receiptno
     from Student s,paymentmaster p
     where p.depositedate between '9/9/2009'and '9/9/2010' and p.accountnumber=s.account


您不会说这是来自存储的proc还是内联sql.您可能需要将日期括在引号中,或者甚至根据日期的传递方式将其强制转换
You don''t say if this is from a stored proc or inline sql. You may need to enclose the dates in quotes or perhaps even cast them depending how they are being passed in


这篇关于如何在SQL Server的where子句中使用变量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆