消息102,级别15,状态1,行3'+'附近的语法不正确。五 [英] Msg 102, Level 15, State 1, Line 3 Incorrect syntax near ' + '. 5

查看:74
本文介绍了消息102,级别15,状态1,行3'+'附近的语法不正确。五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  选择SUM(dr_amount)-SUM(cr_amount) + 
来自Gl_Transaction,其中Accountno喜欢'1%'选择SUM(dr_amount)-SUM(cr_amount) +
来自Gl_Transaction,其中Accountno喜欢'2%' +
选择SUM(dr_amount)-SUM(cr_amount) +
< span class =code-string> 来自Gl_Transaction,其中Accountno喜欢'3%' +
选择SUM(dr_amount)-SUM(cr_amount) +
来自Gl_Transaction,其中Accountno喜欢'4%' +
选择SUM(dr_amount)-SUM(cr_amount) +
其中Accountno喜欢'5%';

解决方案

添加空格键没有帮助;(抱歉,同事......即使OP更改代码,这也没有用,因为他/她会得到SQL错误。



你知道SQL吗?我建议使用基础知识: SQL教程 [ ^ ]


您的示例看起来像C#语句,尽管它在语法上不正确,因为没有运算符该声明。在我的解决方案中,我添加了一个字符串的声明,名为 mySQLStatement ,并为该连接字符串的语句赋值你包含在你的问题中。结果是 mySQLStatement 包含语法正确的SQL语句。我知道这是真的,因为我创建了一个表并对其运行了语句。

  string  mySQLStatement =  选择SUM(dramount)-SUM(cramount) + 
来自GlTransaction,其中Accountno喜欢'1%'选择SUM(dramount)-SUM(cramount) +
来自GlTransaction,其中Accountno喜欢'2%' +
选择SUM(dramount)-SUM(cramount) +
来自GlTransaction,其中Accountno喜欢'3%' +
选择SUM(dramount)-SUM(cramount) +
来自GlTransaction,其中Accountno喜欢' 4%' +
选择SUM(dramount)-SUM(cramount) +
来自GlTransaction,其中Accountno喜欢'5%';

执行上述语句后,mySQLStatement的内容将会是

  选择SUM(dramount)-SUM( cramount)来自GlTransaction,其中Accountno喜欢'1%'从GlTransaction选择SUM(dramount)-SUM(cramount),其中Accountno喜欢'2%'从GlTransaction选择SUM(dramount)-SUM(cramount),其中Accountno喜欢'3%'选择来自GlTransaction的SUM(dramount)-SUM(cramount)其中Accountno喜欢'4%'从GlTransaction选择SUM(dramount)-SUM(cramount),其中Accountno喜欢'5%' 





您的SQL语句将创建五个结果集。虽然这可行,但它可能不是检索此数据的最简单方法。我建议你修改你的SQL语句,只返回一个结果集,每个值都有一个单独的列,而不是每个值一个列的五个结果集。



也许这不是你真正想做的事情......你可能想用上面的改善问题按钮澄清你的问题并解释你想要完成的事情(使用你希望结果看的结果的一个例子)等)。


"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'1%' select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'2%'" +
"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'3%'" +
"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'4%'" +
"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'5%'";

解决方案

Adding spacebar does not help ;( Sorry, collegues... Even when OP change the code, this doesn't help too, because He/She will get SQL error.

Do you know SQL? I'd suggest to strat with basics: SQL Tutorial[^]


Your example looks like a C# statement although it is syntactically incorrect since there is no operator in the statement. In my solution below, I added a declaration for a string named mySQLStatement and an assignment to that statement of the concatenated strings that you included in your question. The result is that mySQLStatement contains a syntactically correct SQL Statement. I know that is true because I created a table and ran the statement against it.

string mySQLStatement = "select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'1%' select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'2%'" +
"select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'3%'" +
"select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'4%'" +
"select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'5%'";

After executing the above statement the contents of mySQLStatement will be

"select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'1%' select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'2%'select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'3%'select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'4%'select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'5%'"



Your SQL statement will create five result sets. While that will work, it may not be the simplest way to retrieve this data. I recommend that you modify your SQL statement to return just one result set with a separate column for each value rather than five result sets of one column each.

Maybe that's not what you really want to do.... You may want to clarify your question using the Improve Question button above and explain what you are trying to accomplish (using an example of what you want the result to look like).


这篇关于消息102,级别15,状态1,行3'+'附近的语法不正确。五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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