且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server的where子句中使用变量.

更新时间:2022-12-04 07:54:25

提及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