Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
this is my storeprocedure
SQL
USE [SSDB2]
GO
/****** Object:  StoredProcedure [dbo].[SPSearching]    Script Date: 25/12/34 09:11:48 ص ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Name
-- Create date: 
-- Description:	
-- =============================================
ALTER PROCEDURE [dbo].[SPSearching] 
	-- Add the parameters for the stored procedure here
	@Mode varchar(50),
	@WhereClause  varchar(Max) = null
AS
if @Mode= 'ColumnNames'
BEGIN
	SELECT column_name FROM information_schema.columns WHERE table_name = 'Hadiths_old'
END

if @Mode= 'Searching'
BEGIN
Select [Hadith_Text]
      ,[Hadith_Urdu]
      ,[Hadith_English]
      ,[Chapter_English_Name]
      ,[Chapter_Urdu_Name]
      ,[Baab_English_Name]
      ,[Baab_Urdu_Name]
      ,[Baab_Id]
      ,[Hadith_Book_Number]
	  From Hadiths_old
	  Where ID < 34192 +'  '+ @WhereClause
	  order by ID

END


when i try to execute it like this
SQL
execute SPSearching @Mode ='Searching', @WhereClause = 'Hadith_Book_Number =1' 


it show me this error
Msg 245, Level 16, State 1, Procedure SPSearching, Line 18
Conversion failed when converting the varchar value 'Hadith_Book_Number =1' to data type int.
Posted

Here is my advice: whenever you need to store integer data, use integer database types, not varchar. In your case, the stored data is probably a string not matching integer string format. Query it as a string and see what it is under debugger.

—SA
 
Share this answer
 
Comments
Muhamad Faizan Khan 30-Oct-13 2:12am    
@WhereClause is varchar and i am passing column name with value to it
Sergey Alexandrovich Kryukov 30-Oct-13 9:12am    
I see, but how about not using varchar for something intended to carry information which is int?
—SA
Try it the below given way -

C#
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Name
-- Create date: 
-- Description:	
-- =============================================
ALTER PROCEDURE [dbo].[SPSearching] 
	-- Add the parameters for the stored procedure here
	@Mode varchar(50),
	@WhereClause  varchar(Max) = null
AS
if @Mode= 'ColumnNames'
BEGIN
	SELECT column_name FROM information_schema.columns WHERE table_name = 'Hadiths_old'
END
 
if @Mode= 'Searching'
BEGIN
declare @SQL VARCHAR(MAX)

SET @SQL='Select [Hadith_Text]
      ,[Hadith_Urdu]
      ,[Hadith_English]
      ,[Chapter_English_Name]
      ,[Chapter_Urdu_Name]
      ,[Baab_English_Name]
      ,[Baab_Urdu_Name]
      ,[Baab_Id]
      ,[Hadith_Book_Number]
	  From Hadiths_old
	  Where ID < 34192 '

IF @WhereClause<>''
	SET @SQL=@SQL+'  AND '+ @WhereClause + ' order by ID'

EXEC(@SQL)
 
END
 
Share this answer
 
Comments
Muhamad Faizan Khan 30-Oct-13 1:45am    
i modify my sp according to your answer and now i am executing it and non stop execution started .. no ending now
Madhu Nair 30-Oct-13 1:52am    
In that case you have to check the query you are using!!!!
Muhamad Faizan Khan 30-Oct-13 2:14am    
Query is given

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900