Wednesday, March 28, 2012

Is this SQL stored prodcedure is valid

what i want to achive is the proc sh'd return a master-detail value in one go.
master value should be returned with Out Parameter and detail value as a recordset.

will it return the recordset of detail table as below...


Create Procedure ProductDetail
(
@.ProductID int,
@.ProductCode varchar(15) OUTPUT,
@.ProductName varchar(60) OUTPUT,
@.CategoryID int OUTPUT,
@.CategoryName varchar(60) OUTPUT,
@.Image1 varchar(256) OUTPUT,
@.Image2 varchar(256) OUTPUT,
@.UnitPrice smallmoney OUTPUT,
@.UOMValue numeric(9) OUTPUT,
@.UOMName varchar(10) OUTPUT,
@.ShippingWeight numeric(9) OUTPUT,
@.Directions varchar(1500) OUTPUT,
@.Ingrediants varchar(1500) OUTPUT,
@.Warnings varchar(1500) OUTPUT,
@.ShortDescription varchar(1000) OUTPUT,
@.LongDescription varchar(2000) OUTPUT,
@.NutritionFacts varchar(1000) OUTPUT,
@.SearchKeywords varchar(500) OUTPUT,
@.IsTaxable varchar(15) OUTPUT,
@.CreatedBy varchar(60) OUTPUT,
@.CreatedOn varchar(15) OUTPUT,
@.UpdatedBy varchar(60) OUTPUT,
@.UpdatedOn varchar(15) OUTPUT,
@.Status int OUTPUT
)
AS

SELECT
@.ProductCode = ProductCode,
@.ProductName = ProductName,
@.CategoryID = CategoryID,
@.CategoryName = (select CategoryName from mCategory where CategoryID=a.CategoryID),
@.Image1 = isnull(Image1,''),
@.Image2 = isnull(Image1,''),
@.UnitPrice = isnull(UnitPrice,0),
@.UOMValue = isnull(UOMValue,0),
@.UOMName = isnull(UOMName,''),
@.ShippingWeight = isnull(ShippingWeight,0),
@.Directions = isnull(Directions,''),
@.Ingrediants = isnull(Ingrediants,''),
@.Warnings = isnull(Warnings,''),
@.ShortDescription = isnull(ShortDesc,''),
@.LongDescription = isnull(LongDesc,''),
@.NutritionFacts = isnull(NutritionFacts,''),
@.SearchKeywords = isnull(SearchKeywords,''),
@.IsTaxable = case when isnull(IsTaxable,0)=0 then 'No' else 'Yes' End,
@.CreatedBy = isnull((select LName + ',' + FName from mUser where UserID=InsertedBy),''),
@.CreatedOn = InsertedOn,
@.UpdatedBy = isnull((select LName + ',' + FName from mUser where UserID=UpdatedBy),''),
@.UpdatedOn = UpdatedOn,
@.Status = Convert(int,isnull(Status,0))
FROM
mProduct a
WHERE
ProductID = @.ProductID

SELECT
ID as PricingDetailID,
isnull(PricingFromQnty,0) as PricingFromQnty,
isnull(PricingToQnty,0) as PricingToQnty,
isnull(RangePrice,0) as RangePrice,
Convert(int,isnull(Status,0))as Status
FROM
dProduct
WHERE
ProductID = @.CategoryID

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


Regards,
BhairavI believe the way you are doing it is possible, but why not return two recordsets back to a dataset? Then you would have a master datatable and detail datatable. I believe this would work:

Create Procedure ProductDetail

(

@.ProductID int
)

AS

SELECT
ProductCode,
ProductName,
CategoryID,
(select CategoryName from mCategory where CategoryID=a.CategoryID),
isnull(Image1,''),
isnull(Image1,''),
isnull(UnitPrice,0),
...

FROM

mProduct a

WHERE

ProductID = @.ProductID

SELECT

ID as PricingDetailID,

isnull(PricingFromQnty,0) as PricingFromQnty,

isnull(PricingToQnty,0) as PricingToQnty,

isnull(RangePrice,0) as RangePrice,

Convert(int,isnull(Status,0))as Status

FROM

dProduct

WHERE

ProductID = @.CategoryID

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

HTH|||thks for u'r suggesion
but can u plz explain me in more detail ...
how my dataset code will look like when a single strore proc return more than one recordset.

proc must not be called more than once for that...

Regards,
Bhairav|||Sure,
Just follow the code (I'm using Microsoft Data Access Application Blocks to call):


SqlParameter [] arParms = new SqlParameter[1];
arParms[0] = new SqlParameter("@.ProductID", SqlDbType.Int);
arParms[0].Value = 1;

DataSet myDS = SQLHelper.ExecuteDataset("connectiion", StoredProcedure, "ProductDetail", arParms);
DataTable myTable1 = myDS.Tables[0];
DataTable myTable2 = myDS.Tables[1];

That should give you an example of the calling code. There are other ways to manipulate the dataset data. If you are unfamiliar, just hollar and we can give you some direction, or search the archives of the data access forms. HTH|||thks..
its really the nice way to code
thks again

Regards,
Bhairav

No comments:

Post a Comment