我缺少什么? SQL转换失败

发布于 2024-12-09 11:49:43 字数 949 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

人间☆小暴躁 2024-12-16 11:49:43

在追加之前,尝试将 stf.StaffNumber 转换为 VARCHAR:

CAST(stf.StaffNumber AS VARCHAR)

这同样适用于您要追加字符串的任何数值。

编辑

我相信这应该涵盖所有内容:

SELECT '"'+    
CAST(stf.StaffNumber AS VARCHAR) +'",'+    
CAST(stf.PrimarySchoolID AS VARCHAR) +',"'+    
stf.LastName +'","'+    
stf.FirstName +'","' +    
ISNULL(u.username, '')+'","'+    
ISNULL(sfc.[NAME],'') +'",'+    
CAST(stf.PrimarySchoolID AS VARCHAR)  

顺便说一句,在只有一个条件的情况下,您应该使用ISNULLCOALESCE 可以正常工作,但它适用于多种条件,即 COALESCE(Col1, Col2, Col3, Col4)

Try casting stf.StaffNumber to a VARCHAR before appending to it:

CAST(stf.StaffNumber AS VARCHAR)

The same applies for any numeric values that you're appending a string to.

EDIT

I believe this should cover everything:

SELECT '"'+    
CAST(stf.StaffNumber AS VARCHAR) +'",'+    
CAST(stf.PrimarySchoolID AS VARCHAR) +',"'+    
stf.LastName +'","'+    
stf.FirstName +'","' +    
ISNULL(u.username, '')+'","'+    
ISNULL(sfc.[NAME],'') +'",'+    
CAST(stf.PrimarySchoolID AS VARCHAR)  

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)

不乱于心 2024-12-16 11:49:43

SELECT 的最后一行更改为:

CAST(stf.PrimarySchoolID as varchar)

Change the last line of the SELECT to:

CAST(stf.PrimarySchoolID as varchar)

明明#如月 2024-12-16 11:49:43

您需要将 stf.PrimarySchoolID 转换为字符类型。

...
CAST(stf.PrimarySchoolID AS VARCHAR(20)) -- You can fix length as needed
...

You'll want to CAST stf.PrimarySchoolID to a character type.

...
CAST(stf.PrimarySchoolID AS VARCHAR(20)) -- You can fix length as needed
...
烛影斜 2024-12-16 11:49:43

在串联使用之前,您需要将 stf.PrimarySchoolID 转换为字符类型(例如 varchar):

SET NOCOUNT ON;

SELECT '"'+
    stf.StaffNumber +'",'+
    convert(varchar, stf.PrimarySchoolID) +',"'+
    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 Server 的 CONVERT 或 SQL ANSI 的 CAST

You need to cast stf.PrimarySchoolID to a character type (say, varchar) before using it in a concatenation:

SET NOCOUNT ON;

SELECT '"'+
    stf.StaffNumber +'",'+
    convert(varchar, stf.PrimarySchoolID) +',"'+
    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

You can do type cast either using SQL Server's CONVERT or SQL ANSI's CAST.

南街女流氓 2024-12-16 11:49:43

由于 stf.PrimarySchoolID 是一个 int 并且您尝试将其连接到 varchar() 字段,因此您会收到此错误。尝试以下操作:

SELECT '"'+
    stf.StaffNumber +'",'+
    cast(stf.PrimarySchoolID as varchar(10)) +',"'+
    stf.LastName +'","'+
    stf.FirstName +'","' +
    COALESCE(u.username, '')+'","'+
    COALESCE(sfc.[NAME],'') +'",'+
    stf.PrimarySchoolID

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:

SELECT '"'+
    stf.StaffNumber +'",'+
    cast(stf.PrimarySchoolID as varchar(10)) +',"'+
    stf.LastName +'","'+
    stf.FirstName +'","' +
    COALESCE(u.username, '')+'","'+
    COALESCE(sfc.[NAME],'') +'",'+
    stf.PrimarySchoolID
不必在意 2024-12-16 11:49:43

连接表列可能不匹配或连接给定列。仔细检查哪些列、逻辑被赋予连接。

Joining tables columns may not be matching or joining on given columns. Double check on what columns, logic are given to join.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文