英文:
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("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")
答案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 := "Line 1 " + // Don't forget the trailing space
"Second line." // 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 """ to "`", or quote your each line of the query string and then add a "+" between each line like
"select" +
" *" +
" from" +
" table"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论