Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi
here is my query but am getting all records from table.i need perticular id records


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go









ALTER PROCEDURE [dbo].[usp_RptSalesSummary]
--@UserGroupID int,
@CompanyID int=null,
@PartyName varchar(50)=null,
@DateFrom datetime=null,
@DateTo datetime=null
AS BEGIN
if @CompanyID is null 
Begin
SELECT  tblStockDetails.*,tblStockHeader.*,tblCompanyMaster.*
FROM
tblStockDetails
INNER JOIN
tblStockHeader ON tblStockDetails.sdDID = tblStockHeader.shDID AND 
tblStockDetails.sdDocType = tblStockHeader.shDocType

left outer join tblCompanyMaster on tblStockHeader.shPartyID=tblCompanyMaster.cmCompanyID
WHERE (@DateFrom IS NULL OR
tblStockHeader.shDocumentDateTime >= @DateFrom) AND (@DateTo IS NULL OR
tblStockHeader.shDocumentDateTime <= @DateTo)

AND tblStockHeader.shIsDelete=0
ORDER BY tblStockHeader.shDocumentDateTime

End else if @CompanyID is not null 
Begin
SELECT tblStockDetails.*,tblStockHeader.*,tblCompanyMaster.*
FROM
tblStockHeader
INNER JOIN
tblStockDetails ON tblStockHeader.shDID = tblStockDetails.sdDID AND
tblStockHeader.shDocType = tblStockDetails.sdDocType
 left outer join tblCompanyMaster on tblStockHeader.shPartyID=tblCompanyMaster.cmCompanyID 


WHERE 
tblStockHeader.shIsDelete=0
ORDER BY tblStockHeader.shDocumentDateTime

End
END



please suggest what is the issue

thank you
Posted

1 solution

When you say
SQL
SELECT * FROM ...
You are telling SQL to select all the columns from the source, and return them.
You are selecting all rows from all three tables and returning them! So you are getting exactly what you are asking for...

If you want less, then specify less:
SQL
SELECT  tblStockDetails.sdDocType, tblStockHeader.shPartyID, tblStockHeader.shDocumentDateTime FROM ...
 
Share this answer
 
Comments
ythisbug 25-Jan-13 8:53am    
i want some fields not all.thank u
ythisbug 25-Jan-13 8:53am    
i want some fields not all.
OriginalGriff 25-Jan-13 9:05am    
Yes. Did you try it?
List the fields you do want...
ythisbug 25-Jan-13 9:23am    
ya i tried.i want to join three tables to get some details related to perticular id.i mean cmcompanyid
OriginalGriff 25-Jan-13 9:30am    
Yes...and? List the fields you want (prefixed by the table name)

BTW: You can reduce the name length for prefixes:
SELECT a.field, b.otherfield FROM myTable a
JOIN myOtherTable b ON a.tableID = b.otherTableID

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