为什么这个 sql server CE 命令不起作用?
我有以下命令:
INSERT INTO tbl_media
(DateAdded) VALUES (GetDate())
SELECT CAST(@@Identity AS int)
它对标准 sql db 运行良好,但对 CE db 不起作用我收到以下错误:
SQL 执行错误。
Executed SQL statement...
Error Source: SQL Server Compact ADO.NET Data Provider
Error Message: There was an error parsing the query. [Token line number = 2, Token line offset = 31, Token in error = )]
可惜这个错误没有更有用,有人知道会发生什么吗?
干杯
UPDATE:::::::
在使用 Visual Studio 编辑器(垃圾)后,我下载了数据端口并阅读了 MSDN。似乎有两个问题...
1) SELECT CAST(@@Identity AS int) is not valid sql
SELECT @@Identity is
2) 当我将这两个命令放在一起时,SqlCe 服务器不喜欢它:
INSERT INTO tbl_media (添加日期)值 (getdate()) SELECT @@Identity
如果我在不同时间进行插入和选择,那么它会起作用。那么我该如何解决这个问题呢?我不能在不同的时间这样做,我需要在创建对象时知道它们的 ID!
更新 2:
根据非常乐于助人的 Erik E 的说法,你不能同时执行 2 个语句。因此,以下解析正确但不起作用:
INSERT INTO tbl_media (添加日期)值(getdate()); 选择@@身份;
那么我真正想知道的是如何保证添加记录时身份不会混淆?
即,如果有人创建一条记录,而有人正在获取他们刚刚插入的记录的身份,该怎么办?
I have the command:
INSERT INTO tbl_media
(DateAdded) VALUES (GetDate())
SELECT CAST(@@Identity AS int)
It works fine against a standard sql db but not against a CE db I get the following error:
SQL Execution Error.
Executed SQL statement...
Error Source: SQL Server Compact ADO.NET Data Provider
Error Message: There was an error parsing the query. [Token line number = 2, Token line offset = 31, Token in error = )]
Shame the error isn't more useful anyone know what could be going on?
Cheers
UPDATE::::::
After much messing around with visual studio editor (rubbish) I downloaded dataport and read the MSDN. It seems there are 2 problems...
1) SELECT CAST(@@Identity AS int) is not valid sql
SELECT @@Identity is
2) SqlCe server does not like it when I put these two commands together:
INSERT INTO tbl_media
(DateAdded) VALUES (getdate())
SELECT @@Identity
If I do the insert and select at different times then it works. So how do I get round this? I cant do it at different times I need to know the ID of the objects as I create them!!!
UPDATE 2:
According to the very helpful Erik E you cant do 2 statements at the same time. So the following parses as correct but wont work:
INSERT INTO tbl_media
(DateAdded) VALUES (getdate());
SELECT @@Identity;
So what I really want to know is how do I guarantee that identities wont get mixed up when adding records?
I.e. what if someone creates a record while someone is getting the identity for one they have just inserted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个额外的 ) 我不知道这是否会解决您的错误,但请查看您拥有
VALUES(GETDATE()))
'<-- one ) extra 的 VALUES。改成这样:
You have an extra ) I dont know if that will fix your error but look at VALUES you have
VALUES(GETDATE()))
'<-- one ) extra.Change it to this:
每个 ExecuteNonQuery 调用只能运行一条 SQL 语句。所以你必须分两次调用。
You can only run a single SQL statement per ExecuteNonQuery call. So you must spilt in 2 calls.