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
RyanDev90.8K
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
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 490
1 Shai Vashdi 339
2 Sergey Alexandrovich Kryukov 291
3 Peter Leow 135
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,335
2 Peter Leow 4,040
3 Maciej Los 3,540
4 Abhinav S 3,308


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