在sql ce中声明变量

发布于 2024-11-07 21:48:31 字数 311 浏览 0 评论 0原文

我正在尝试在 sql 紧凑版中从一个月内的表中获取记录。 这是我知道的sql查询:

DECLARE @startDate as DATETIME, @EndDate as DATETIME

@startDate = GetDate();
@ENdDate = DATEADD(m,1,@startDate)

select * from table where (columnname between @startdate and @enddate)

我知道你必须一次发送一个脚本,但是你如何在sql ce中声明变量(我猜它不接受声明)?

I am tryin to get records from a table within a month in sql compact edition.
Here is the sql query I know:

DECLARE @startDate as DATETIME, @EndDate as DATETIME

@startDate = GetDate();
@ENdDate = DATEADD(m,1,@startDate)

select * from table where (columnname between @startdate and @enddate)

I know that you have to send one script at a time, but how can you declare variables in sql ce(I guess it doesn't accept declare)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵你手 2024-11-14 21:48:31

我认为我的回答对于这个问题来说已经很晚了,但希望它对某人有用。

您不能在 SQL CE 中声明某些变量,因为每个命令只能使用一个语句。正如 ErikEJ 在此 链接

如果可能的话,您需要将脚本重构为一个大语句!

我会很高兴听到更好的解决方案。

I think my answer is very late for this question, but hope it will be useful for someone.

You can't declare some variable in SQL CE, because just one statement can be used per command. As ErikEJ said in this link.

You need to refactor your script to one big statement, if it's possible!

I will be very happy to hear a better solution.

溺ぐ爱和你が 2024-11-14 21:48:31

看看帖子 如何填充SQL Server Compact 数据库?
看看那里引用的工具是否可以帮助您。

Take a look at post How do I populate a SQL Server Compact database?
and see if tool referenced there may help you.

最初的梦 2024-11-14 21:48:31

如果您通过应用程序调用它(我不确定您如何读取数据),

请像这样准备您的查询:

select * from table where (columnname between ? and ?)

但我不确定您是否可以使用 Between 关键字。可能你需要改变这个。

那么您需要像这样添加 SqlCeParameter 对象:

cmd.Parameters.Add(new SqlCeParameter("p1", SqlDbType.DateTime, myDate));

If you call it through an application (I'm not sure how you read the data)

Prepare your query just like this:

select * from table where (columnname between ? and ?)

but I'm not sure if you can use the between keyword. may be you need to change this.

then you need to add your SqlCeParameter objects like this:

cmd.Parameters.Add(new SqlCeParameter("p1", SqlDbType.DateTime, myDate));
不爱素颜 2024-11-14 21:48:31

我不熟悉 SQL-CE,但我认为您缺少一些 Set 语句。 试试这个

DECLARE @startDate as DATETIME, @EndDate as DATETIME

Set @startDate = GetDate();
Set @ENdDate = DATEADD(m,1,@startDate)

select * from table where (columnname between @startdate and @enddate)

更新
请参阅在查询中使用参数来自 MSDN 的 SQL CE 参考。您是正确的,因为 Declare 不是有效的关键字,因此您需要将查询作为应用程序本身的参数化版本。

从表中选择*,其中(?和?之间的列名)

I'm not familiar with SQL-CE, but I think you're missing some Set statements. Try this:

DECLARE @startDate as DATETIME, @EndDate as DATETIME

Set @startDate = GetDate();
Set @ENdDate = DATEADD(m,1,@startDate)

select * from table where (columnname between @startdate and @enddate)

Update
See the Using Parameters in Queries in SQL CE reference from MSDN. You are correct in that Declare is not a valid keyword, so you'll need to the query as a parameterized version from the application itself.

select * from table where (columnname between ? and ?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文