在我的存储过程中,我从三个表中获取数据但是出现了这个错误:
错误将数据类型varchar转换为数字。
我认为问题在于: -
'NULL' AS DATE (in 1st query of this SP)------(1)
CONVERT(VARCHAR, tbl_IPD.ipd_doa, 103) AS DATE (in 2nd query of this SP)-----(2)
我的问题是:如何将(1)行转换为varchar
SELECT
tbl_CompanyProfile.Company_Name, tbl_CompanyProfile.Company_Address,
tbl_CompanyProfile.Company_ContactNo, tbl_CompanyProfile.Company_Email,
tbl_CompanyProfile.Company_Website,
'NULL' AS NAME, 'NULL' AS DATE,
'NULL' AS AMOUNT, 'NULL' AS DEPARTMENT,
0 AS Age, @from AS StartDate, @to AS EndDate
FROM
tbl_CompanyProfile
UNION ALL
SELECT
'--', '--',
'--', '--', '--',
tbl_IPD.ipd_pfname + ' ' + tbl_IPD.ipd_plname AS NAME,
CONVERT(VARCHAR, tbl_IPD.ipd_doa, 103) AS DATE,
ISNULL(SUM(tbl_fee.fee_amount), 0) AS AMOUNT,
tblDepartment.DeptName AS DEPARTMENT, tbl_IPD.ipd_age AS Age,
@from AS StartDate, @to AS EndDate
FROM
tbl_fee
LEFT OUTER JOIN
tbl_IPD ON tbl_fee.ipd_id = tbl_IPD.ipd_id
LEFT OUTER JOIN
tblDepartment ON tbl_IPD.ipd_dpt = tblDepartment.DeptId
WHERE
(tbl_IPD.ipd_doa BETWEEN @from AND @to)
GROUP BY
tbl_IPD.ipd_pfname, tbl_IPD.ipd_plname, tbl_IPD.ipd_doa, tblDepartment.DeptName, tbl_IPD.ipd_age
答案 0 :(得分:2)
如果您尝试返回NULL
值,则应移除'NULL'
上的引号,否则您将返回varchar
并且这不是您正在尝试做的事情。
如果我的假设是正确的,那么这就是你的查询的样子。
...NULL AS NAME, NULL AS DATE, NULL AS AMOUNT, NULL AS DEPARTMENT,...
答案 1 :(得分:0)
我认为问题实际上在AMOUNT
列中 - 您在第一个查询中选择NULL
(字符串)并尝试将其连接到第二个查询中的数字结果。如果要在联合中使用实际的NULL
值,只需从NULL
引用中取出撇号。