如何在select语句vb.net中定义变量?

问题描述 投票:-2回答:2

我有这样的查询

Dim view_src_14 As String = GetParameterValue(“ViewSrc14”)

Dim calendar_date_14 As String = GetParameterValue(“CalendarDate14”)

select calendar_date,view_src,sum(effective) effective_total, sum(ineffective) ineffective_total 
from wrk_alert_effectiveness 
where calendar_date='" + calendar_date_14 + "' and '" + view_src_14 + "' 
group by 1,2 
order by 1 desc;

calender_date_14view_src_14是变量...当我运行查询时它带来了这个错误:

类型日期的输入语法无效:“”

我在哪里做出改变?

sql
2个回答
1
投票

我认为这不足以回答问题,但评论时间太长了。

您正在尝试执行一个SQL语句,在该语句中传入语句中的常量值。这是允许的,也是SQL的一部分 - 使用参数。有两种类型的参数,命名参数和位置参数。

select calendar_date, view_src,
       sum(effective) as effective_total, sum(ineffective) as ineffective_total 
from wrk_alert_effectiveness 
where calendar_date = @date1 and @date2
group by 1, 2 
order by 1 desc;

通常,这些由?表示匿名参数。有时命名的冒号是用冒号引入的。

确切的语法取决于您使用的数据库和应用程序接口。我的观点是,您应该了解参数以及如何使用它们。


0
投票
"select calendar_date, view_src, sum(effective) effective_total, 
sum(ineffective) ineffective_total 
from wrk_alert_effectiveness 
where calendar_date= '" + @CalendarDate + "' AND " +  @ViewSrc + " 
group by 1,2 
order by 1 desc;"
© www.soinside.com 2019 - 2024. All rights reserved.