我缺少什么? SQL转换失败
我正在尝试将 stf.PrimarySchoolID
(整数)添加到此现有查询中。我有一个 bat 文件,它将使用此 SQL 语句导出 CSV。但是,当我打开导出的 CSV 时,我收到此错误:
Msg 245, Level 16, State 1, Server [SERVER NAME], Line 13
Conversion failed when converting the varchar value ',"' to data type int.
注意:stf.StaffNumber
不是整数,它是字母数字。
SET NOCOUNT ON;
SELECT '"'+
stf.StaffNumber +'","'+
stf.LastName +'","'+
stf.FirstName +'","' +
COALESCE(u.username, '')+'","'+
COALESCE(sfc.[NAME],'') +'",'+
stf.PrimarySchoolID
FROM dbo.UDF_GetCurrentAcademicSessions() cur
JOIN dbo.Course c ON c.AcadSessionID = cur.AcadSessionId
JOIN dbo.Staff stf ON stf.StaffID = c.PrimaryTeacherID
JOIN dbo.StaffClassification sfc ON sfc.StaffClassificationID = stf.PrimaryClassificationID
LEFT OUTER JOIN dbo.[User] u ON u.UserID = stf.UserID
您发现我的错误了吗?部分?我对 SQL 语句不太有经验,所以非常感谢您的帮助。
I'm trying to add stf.PrimarySchoolID
(an integer) into this existing query. I have a bat file that will use this SQL statement to export a CSV. However, when I open the exported CSV I receive this error:
Msg 245, Level 16, State 1, Server [SERVER NAME], Line 13
Conversion failed when converting the varchar value ',"' to data type int.
Note: stf.StaffNumber
is not an integer, it is alpha-numeric.
SET NOCOUNT ON;
SELECT '"'+
stf.StaffNumber +'","'+
stf.LastName +'","'+
stf.FirstName +'","' +
COALESCE(u.username, '')+'","'+
COALESCE(sfc.[NAME],'') +'",'+
stf.PrimarySchoolID
FROM dbo.UDF_GetCurrentAcademicSessions() cur
JOIN dbo.Course c ON c.AcadSessionID = cur.AcadSessionId
JOIN dbo.Staff stf ON stf.StaffID = c.PrimaryTeacherID
JOIN dbo.StaffClassification sfc ON sfc.StaffClassificationID = stf.PrimaryClassificationID
LEFT OUTER JOIN dbo.[User] u ON u.UserID = stf.UserID
Do you spot a mistake on my part? I'm not very experienced with SQL statements, so any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在追加之前,尝试将
stf.StaffNumber
转换为 VARCHAR:这同样适用于您要追加字符串的任何数值。
编辑
我相信这应该涵盖所有内容:
顺便说一句,在只有一个条件的情况下,您应该使用
ISNULL
。COALESCE
可以正常工作,但它适用于多种条件,即COALESCE(Col1, Col2, Col3, Col4)
Try casting
stf.StaffNumber
to a VARCHAR before appending to it:The same applies for any numeric values that you're appending a string to.
EDIT
I believe this should cover everything:
On a side note, you should use
ISNULL
in cases where there's only one condition.COALESCE
will work fine, but it's wired for multiple conditions, i.e.COALESCE(Col1, Col2, Col3, Col4)
将
SELECT
的最后一行更改为:CAST(stf.PrimarySchoolID as varchar)
Change the last line of the
SELECT
to:CAST(stf.PrimarySchoolID as varchar)
您需要将
stf.PrimarySchoolID
转换为字符类型。You'll want to CAST
stf.PrimarySchoolID
to a character type.在串联使用之前,您需要将
stf.PrimarySchoolID
转换为字符类型(例如 varchar):您可以使用 SQL Server 的
CONVERT
或 SQL ANSI 的CAST
。You need to cast
stf.PrimarySchoolID
to a character type (say, varchar) before using it in a concatenation:You can do type cast either using SQL Server's
CONVERT
or SQL ANSI'sCAST
.由于 stf.PrimarySchoolID 是一个 int 并且您尝试将其连接到 varchar() 字段,因此您会收到此错误。尝试以下操作:
Since stf.PrimarySchoolID is an int and you are trying to concatenate it to a varchar() field, you are getting this error. Try the foll:
连接表列可能不匹配或连接给定列。仔细检查哪些列、逻辑被赋予连接。
Joining tables columns may not be matching or joining on given columns. Double check on what columns, logic are given to join.