Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my stored procedure:
SQL
ALTER PROCEDURE [dbo].[cls_search]
	@serarchtype int=null,   // this mode use for multiple select
	@cls_code varchar(20)=null,// other twoo are column
	@cls_name varchar(30)=null
AS
BEGIN
		if (@serarchtype = 1)
		BEGIN
		select cls_code,cls_name from Cls_master where cls_code=@cls_code 
		END
		else
		BEGIN
		select cls_code,cls_name from Cls_master where cls_name=@cls_name 
		END
END



i want to use 1 as @serarchtype and @cls_code but no give @cls_name then first select query
execute


otherwise
use 2 as @serarchtype and @cls_name but no give @cls_code then second select query execute



plz help me
Posted
Updated 3-Oct-13 13:12pm
v2
Comments
Ramanathan Kannappan 3-Oct-13 14:37pm    
Do you want this in single query?
Azee 3-Oct-13 14:39pm    
Not clear man, you have got the query, what is the problem?
priyanshbhaliya 3-Oct-13 23:34pm    
yes,i want this in single query..

1 solution

Well, for starters, this modification works:
DECLARE @strNonsense_02 [nvarchar](66)
SET @strNonsense_02 = 'nonesense_02'
DECLARE @searchtype[int]
SET @searchtype = 1
BEGIN
		if (@searchtype = 1)
		BEGIN
		select @strNonsense_01 --cls_code,cls_name from Cls_master where cls_code=@cls_code 
		END
		else
		BEGIN
		select @strNonsense_02 -- from Cls_master where cls_name=@cls_name 
		END
END

Returns:
nonesense_01

And:
BEGIN
		if (@searchtype = 65)
		BEGIN
		select @strNonsense_01 --cls_code,cls_name from Cls_master where cls_code=@cls_code 
		END
		else
		BEGIN
		select @strNonsense_02 -- from Cls_master where cls_name=@cls_name 
		END
END

Returns:
nonsense_02

Not privy to the data, that's all I can venture from the description of the problem ...
 
Share this answer
 
Comments
priyanshbhaliya 3-Oct-13 23:37pm    
thnks but those variable in my stored procedure is given from my website...

in your solution how can @cls_code & @cls_name can be input ?????

i want to input as
Exec 1,1 -- 1 for searchtype and other 1 for cls_id it works but

another is Exec 1,'abc' -- 1 for searchtype and other 'abc' for cls_name but it is not execute because of all 3 variable that defind in stored procedure is not inputed
RedDk 4-Oct-13 0:33am    
Ok,

I see what you're saying. Seems to me then that you have to catch the type. So perhaps a nested IF. You'll run out of types to check for, right? Just keep nesting. There's probably a better way; possibly CASE or COALESCE. Look these up in the BOL. Plenty of examples from which to choose best fit.

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