Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL
Error: converting data type varchar to int.
 
ALTER PROCEDURE [dbo].[SP_City]
(
 
@CityID int = null,
@Flag int = 0,
@WhereCondition varchar(200)=null,
@OrderByExpression varchar(20)=null
 
)
AS 
SET NOCOUNT ON 
BEGIN
 

ELSE IF @Flag =  4  -- SELECT ALL 
	BEGIN 
	  
	 declare @Query varchar(200);
	 set @Query='Select *from City WHERE ' +@WhereCondition+ ' ORDER BY ' +@OrderByExpression;	
     exec SP_City @Query 
	 
	END 
 
ELSE IF @Flag =  5  -- SELECT SINGLE 
	BEGIN 
	 -- Select Particular Record  Block here
	Select * FROM City WHERE CityID = @CityID 
	END 
END
Posted 12-Sep-12 2:52am
Edited 12-Sep-12 3:01am
RyanDev120.7K
v3
Comments
_Amy at 12-Sep-12 8:59am
   
Which line you are getting error?
tarun0702 at 12-Sep-12 9:01am
   
when i execute SP with below code:
USE [dbInvoice]
GO
 
DECLARE @return_value int
 
EXEC @return_value = [dbo].[SP_City]
@Flag = 4,
@WhereCondition = 'CityID=1',
@OrderByExpression = CityID
 
SELECT 'Return Value' = @return_value
 
GO
 
-------------------
Error:
Msg 8114, Level 16, State 1, Procedure SP_City, Line 0
Error converting data type varchar to int.
_Amy at 12-Sep-12 9:04am
   
Try my answer. I think, I found your error.
ryanb31 at 12-Sep-12 9:04am
   
Am I crazy or are you recursively calling this stored procedure? exec SP_City @Query This appears to be a very confusing way to do what you want.
tarun0702 at 12-Sep-12 9:13am
   
plz see Bellow Solution and comments.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
Try this:
ALTER PROCEDURE [dbo].[SP_City]
( 
    @CityID int = null,
    @Flag int = 0,
    @WhereCondition varchar(200)=null,
    @OrderByExpression varchar(20)=null 
)
AS 
SET NOCOUNT ON 
BEGIN
 
 
ELSE IF @Flag =  4  -- SELECT ALL 
	BEGIN 	  
	   declare @Query varchar(200);
	   set @Query='Select *from City WHERE ' +@WhereCondition+ ' ORDER BY ' +@OrderByExpression;	
           EXEC (@Query) --Here was the error.
	END 
 
ELSE IF @Flag =  5  -- SELECT SINGLE 
	BEGIN 
	   -- Select Particular Record  Block here
	   Select * FROM City WHERE CityID = @CityID 
	END 
END
 

--Amit
  Permalink  
v2
Comments
tarun0702 at 12-Sep-12 9:11am
   
Msg 2812, Level 16, State 62, Procedure SP_City, Line 42
Could not find stored procedure 'Select *from City WHERE CityID=1 ORDER BY CityID'
tarun0702 at 12-Sep-12 9:12am
   
that menace its takes as SP but i want to execute Query.
tarun0702 at 12-Sep-12 9:16am
   
thx U r right to give idea where error Was!!!
tarun0702 at 12-Sep-12 10:00am
   
I solved My Problem.

error is:

EXEC (@Query)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 235
4 BillWoodruff 190


Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100