Golang SQL 查询语法

huangapple go评论96阅读模式
英文:

Golang SQL Query Syntax

问题

在golang代码中的SQL查询中遇到了语法错误。需要正确的语法来执行这个SQL查询:

rows, errQuery := dbCon.Query("SELECT B.LatestDate, A.SVRName AS ServerName, A.DRIVE, A.TotalSpace_GB AS TotalSpaceGB, (ISNULL(A.TotalSpace_GB, 0) - ISNULL(A.FreeSpace_GB, 0)) AS UsedSpaceGB, A.FreeSpace_GB AS FreeSpaceGB, CASE WHEN ((A.FreeSpace_GB / A.TotalSpace_GB) * 100) BETWEEN 25 AND 35 THEN 1 WHEN ((A.FreeSpace_GB / A.TotalSpace_GB) * 100) <= 25 THEN 2 ELSE 0 END AS WARNINGSTATUS FROM Table_ServerDiskSpaceDetails A WITH (NOLOCK) INNER JOIN (SELECT SVRName, MAX(Dt) LatestDate FROM Table_ServerDiskSpaceDetails WITH (NOLOCK) GROUP BY SVRName) B ON A.Dt = B.LatestDate AND A.SVRName = B.SVRName ORDER BY WARNINGSTATUS DESC, ServerName, A.Drive")

请注意,这只是一个SQL查询的语法示例,具体的语法可能因数据库类型和版本而有所不同。如果仍然遇到问题,请提供更多的错误信息以便我能够帮助你解决问题。

英文:

Getting syntax erro with sql query in golang code. Required proper syntax for this SQL query in golang:

rows, errQuery := dbCon.Query(&quot;SELECT B.LatestDate
	,A.SVRName AS ServerName
	,A.DRIVE
,A.TotalSpace_GB AS TotalSpaceGB
,(ISNULL(A.TotalSpace_GB, 0) - ISNULL(A.FreeSpace_GB, 0)) AS 
 UsedSpaceGB
,A.FreeSpace_GB AS FreeSpaceGB
,CASE 
WHEN ((A.FreeSpace_GB / A.TotalSpace_GB) * 100)  between 25 and 
35
THEN 1
WHEN ((A.FreeSpace_GB / A.TotalSpace_GB) * 100) &lt;= 25   THEN 2
ELSE 0
END AS WARNINGSTATUS
	FROM Table_ServerDiskSpaceDetails A WITH (NOLOCK)
	INNER JOIN (
SELECT SVRName
,MAX(Dt) LatestDate
FROM Table_ServerDiskSpaceDetails WITH (NOLOCK)
GROUP BY SVRName
) B ON A.Dt = B.LatestDate
AND A.SVRName = B.SVRName
	ORDER BY WARNINGSTATUS DESC
,ServerName
,A.Drive&quot;)

答案1

得分: 5

你的SQL语句跨多行,但你没有使用正确的多行语法。正确的语法应该是:

someLongString := "Line 1 " +    // 不要忘记尾随空格
                  "Second line." // 这是下一行。

目前,你只是试图将所有内容都放在不同行的引号之间。


编辑:根据@Kaedys的说法,以下方法也可行,并且可能更高效。

someLongString := `Line 1
                   Second line.`
英文:

Your SQL statement is on multiple lines, but you're not using the correct multi-line syntax. The correct syntax would be:

someLongString := &quot;Line 1 &quot; +    // Don&#39;t forget the trailing space
                  &quot;Second line.&quot; // This is on the next line.

Currently you're just trying to stuff everything between a set of quotes on different lines.


EDIT: Per as @Kaedys says below, the following also works and may be more performant.

someLongString := `Line 1
                   Second line.`

答案2

得分: 0

将你的第一个和最后一个“\”都改为“`”,或者引用查询字符串的每一行,然后在每一行之间添加一个“+”,就像这样:

"select" +
" *" +
" from" +
" table"
英文:

change both your first and last "&quot;" to "`", or quote your each line of the query string and then add a "+" between each line like

&quot;select&quot; +
&quot; *&quot; +
&quot; from&quot; +
&quot; table&quot;

huangapple
  • 本文由 发表于 2017年5月9日 01:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/43853506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定