Solved

Getting this SQL Error?

Posted on 2014-04-10
10
267 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

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.

Join & Write a Comment

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now