Solved

Getting this SQL Error?

Posted on 2014-04-10
10
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 66

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 66

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 66

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 66

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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