Solved

Getting this SQL Error?

Posted on 2014-04-10
10
270 Views
Last Modified: 2014-04-10
I'm getting this error in my SP and I understand why I'm getting it...just not sure how to rectify it without using dynamic sql.

The error that I'm getting is;

"Converion failed when converting the varchar value ' AND A.accredited <> 0' to data type Int".

Here is my SP:

ALTER PROCEDURE [dbo].[spLoadEquipmentCalibrationGrid]
	@type_ID		INT=NULL,
	@useAccredited	INT=NULL
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @Accredited		VARCHAR(50)=NULL
	
	IF @useAccredited <> 0 
		BEGIN
			SET @Accredited = ' AND A.accredited <> 0'
		END

		
	IF @type_ID = 0
		BEGIN
			SELECT	CONVERT(bit,0) AS 'Selected',
					A.equipment_ID AS 'Equipment ID',
					B.equipmentType AS 'Type',
					C.equipmentName AS 'Name',
					CONVERT(bit,A.accredited) AS 'Accredited',
					F.department AS 'Department',
					REPLACE(G.calibrationHouse,'~','''') AS 'Calibration House',
					CONVERT(bit,A.active) AS 'Active',
					D.userAccount AS 'Created By',
					D.dateCreated AS 'Date Created',
					E.userAccount AS 'Updated By',
					e.dateUpdated AS 'Date Updated'
			  FROM  HEADER_Equipment A LEFT JOIN LKUP_EquipmentTypes B ON A.equipmentType_ID = B.equipmentType_ID
					LEFT JOIN LKUP_EquipmentNames C ON A.equipmentName_ID = C.equipmentName_ID
					LEFT JOIN LKUP_UserAccounts D ON A.createdBy_ID = D.userAccount_ID
					LEFT JOIN LKUP_UserAccounts E ON A.updatedBy_ID = E.userAccount_ID
					LEFT JOIN LKUP_Departments F ON A.department_ID = F.department_ID					
					LEFT JOIN LKUP_CalibrationHouses G ON A.calibrationHouse_ID = G.calibrationHouse_ID
			 WHERE	A.equipmentType_ID >= 0 + @Accredited
		END

Open in new window

0
Comment
Question by:BlakeMcKenna
  • 4
  • 3
  • 3
10 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39991894
Why not just hard-code the WHERE clause?  I don't see any logic in here that would necessitate the use of the @Accredited variable.

WHERE A.equipmentType_ID >= 0 AND A.accredited <> 0
0
 

Author Comment

by:BlakeMcKenna
ID: 39991920
I did not put all the code in. There is still an Else to the main IF statement; when the @type_ID <> 0.

I'm just trying to reduce code!
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39991934
You can't do this:

WHERE      A.equipmentType_ID >= 0 + @Accredited

Because @Accredited is SQL, you need to put the whole statement into a variable and execute it as dynamic SQL.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 39991939
>WHERE      A.equipmentType_ID >= 0 + @Accredited
The error message is being generated because it of the addition of the numeric zero and the varchar @accredited.

>There is still an Else to the main IF statement; when the @type_ID <> 0.
>I'm just trying to reduce code!
I'm not seeing a 'dynamic' way to pull this off other than Dynamic SQL, which should be avoided unless absolutely necessary, based on the code provided in this question.  

If you could provide more details as to why you're trying to do it this way, we might be able to provide a workaround.  Outside of that, I'd just hard-code it.
0
 

Author Comment

by:BlakeMcKenna
ID: 39991942
Here is the rest of the code. It's not a complicated Query. I'm just trying to reduce the amount of code.

ALTER PROCEDURE [dbo].[spLoadEquipmentCalibrationGrid]
	@type_ID		INT=NULL,
	@useAccredited	INT=NULL
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @Accredited		VARCHAR(50)=NULL
	
	IF @useAccredited <> 0 
		BEGIN
			SET @Accredited = ' AND A.accredited <> 0'
		END

		
	IF @type_ID = 0
		BEGIN
			SELECT	CONVERT(bit,0) AS 'Selected',
					A.equipment_ID AS 'Equipment ID',
					B.equipmentType AS 'Type',
					C.equipmentName AS 'Name',
					CONVERT(bit,A.accredited) AS 'Accredited',
					F.department AS 'Department',
					REPLACE(G.calibrationHouse,'~','''') AS 'Calibration House',
					CONVERT(bit,A.active) AS 'Active',
					D.userAccount AS 'Created By',
					D.dateCreated AS 'Date Created',
					E.userAccount AS 'Updated By',
					e.dateUpdated AS 'Date Updated'
			  FROM  HEADER_Equipment A LEFT JOIN LKUP_EquipmentTypes B ON A.equipmentType_ID = B.equipmentType_ID
					LEFT JOIN LKUP_EquipmentNames C ON A.equipmentName_ID = C.equipmentName_ID
					LEFT JOIN LKUP_UserAccounts D ON A.createdBy_ID = D.userAccount_ID
					LEFT JOIN LKUP_UserAccounts E ON A.updatedBy_ID = E.userAccount_ID
					LEFT JOIN LKUP_Departments F ON A.department_ID = F.department_ID					
					LEFT JOIN LKUP_CalibrationHouses G ON A.calibrationHouse_ID = G.calibrationHouse_ID
			 WHERE	A.equipmentType_ID >= 0 + @Accredited
		END
	ELSE
		BEGIN
			SELECT	CONVERT(bit,0) AS 'Selected',
					A.equipment_ID AS 'Equipment ID',
					B.equipmentType AS 'Type',
					C.equipmentName AS 'Name',
					CONVERT(bit,A.accredited) AS 'Accredited',
					F.department AS 'Department',
					REPLACE(G.calibrationHouse,'~','''') AS 'Calibration House',
					CONVERT(bit,A.active) AS 'Active',
					D.userAccount AS 'Created By',
					D.dateCreated AS 'Date Created',
					E.userAccount AS 'Updated By',
					e.dateUpdated AS 'Date Updated'
			  FROM  HEADER_Equipment A LEFT JOIN LKUP_EquipmentTypes B ON A.equipmentType_ID = B.equipmentType_ID
					LEFT JOIN LKUP_EquipmentNames C ON A.equipmentName_ID = C.equipmentName_ID
					LEFT JOIN LKUP_UserAccounts D ON A.createdBy_ID = D.userAccount_ID
					LEFT JOIN LKUP_UserAccounts E ON A.updatedBy_ID = E.userAccount_ID
					LEFT JOIN LKUP_Departments F ON A.department_ID = F.department_ID					
					LEFT JOIN LKUP_CalibrationHouses G ON A.calibrationHouse_ID = G.calibrationHouse_ID
			 WHERE	A.equipmentType_ID = @type_ID + @Accredited
		END
END

Open in new window

0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 39991950
HArd code the where clause. Your only other option is dynamic SQL which isn't recommended unless absolutely necessary.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39991968
Just for kicks and giggles, try this..

SELECT blah, blah, blah
FROM your_tables
WHERE A.equipmentType_ID >= 0 
   AND CASE WHEN @useAccredited <> 0 THEN accredited END <> 0

Open in new window

I have an article on SQL Server CASE Solutions, but one of the things I'll have to add is using CASE in a WHERE clause.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39992074
I believe the content you accepted was provided in the very first comment...
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39992087
Indeed it was
0
 

Author Comment

by:BlakeMcKenna
ID: 39992144
Thanks Jim but I've already got it coded. I will save that snippet and test later though!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query questions 2 41
Correct Thousand and decimal separator in sql server 2008 3 30
MSSQL Speen Degradation 4 31
TSQL - How to declare table name 26 41
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question