Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables as follows

1.ItemCode(string),ItemName(string),and some more field called ItemMaster.
2.ItemCode(string),BranchName(string),Qty(int) and .... called Stocks

I want to retrieve the data from second table if the Branch and "ItemName" are correct.

Am not passing "ItemCode" field my search field is ItemName. how I can achieve this. please help me to find solution

in itemcode case I write the storedproc
SQL
Alter Procedure ItemShow 
 @BranchName varchar(20) = NULL,
 @ItemCode varchar(20) = NULL
 As
 Begin
SELECT Distinct StockInBranches.ItemCode,StockInBranches.BranchName,StockInBranches.Price,StockInBranches.Qty
		
	FROM StockInBranches 
Inner Join ItemMaster On
   StockInBranches.ItemCode= @ItemCode
 WHERE StockInBranches.BranchName=@BranchName 
 End


how to write in itemname case? please check.
Posted

1 solution

try
SQL
ELECT Distinct StockInBranches.ItemCode,StockInBranches.BranchName,StockInBranches.Price,StockInBranches.Qty

    FROM StockInBranches
Inner Join ItemMaster On
   StockInBranches.ItemCode= ItemMaster.ItemCode 
 WHERE StockInBranches.BranchName=@BranchName and (@ItemCode is null or StockInBranches.ItemCode = @ItemCode)


if you only need to pass BranchName only then
SQL
ELECT Distinct StockInBranches.ItemCode,StockInBranches.BranchName,StockInBranches.Price,StockInBranches.Qty

    FROM StockInBranches
Inner Join ItemMaster On
   StockInBranches.ItemCode= ItemMaster.ItemCode 
 WHERE StockInBranches.BranchName=@BranchName
 
Share this answer
 
v2
Comments
tastini 10-Dec-14 2:55am    
Here I am going to pass the ItemCode value for @ItemCode?
DamithSL 10-Dec-14 2:56am    
you can pass null value if you don't have value for ItemCode
tastini 10-Dec-14 2:57am    
But I want to pass ItemName value instead of ItemCode.
DamithSL 10-Dec-14 2:59am    
check my update
tastini 10-Dec-14 3:03am    
still , please check my doubt , I have two tables, first table having itemcode and itemname and second table having only itemcode and branch. I want to retrieve data from second table if the search matches "itemname and branch" here am not passing itemcode. what I write that's not correct procedure.

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