Solved

Getting this SQL Error?

Posted on 2014-04-10
10
272 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 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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.

736 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