?
Solved

Getting this SQL Error?

Posted on 2014-04-10
10
Medium Priority
?
278 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 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 1500 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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 deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

850 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