Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning data from a stored procedure to .Net code 2

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
Wondering if it is better practice to use OUTPUT variables (and am I using them right) to return some values to the calling code

Code:
CREATE PROCEDURE [dbo].[InsertOrUpdateFormPageData]
	@FormPageDataId	int null,
	@FormPageId	int,
	@CreatedBy	int null,
	@PostedJSONData	nvarchar(max),
	@UpdatedBy int null,
	@PageToReturn int,
	@PrevPageId INT OUTPUT,
	@NextPageId INT OUTPUT,
	@HTML varchar(max) OUTPUT,
	@FPId int OUTPUT
AS
BEGIN
	Create table #tmp
	(
		FormPageDataId int,
		FormPageId int,
		CreatedBy int,
		PostedJSONData nvarchar(max),
		UpdatedBy int null
	)

INSERT INTO #tmp(FormPageDataId,FormPageId,CreatedBy,PostedJSONData,UpdatedBy)
VALUES(@FormPageDataId,@FormPageId,@CreatedBy,@PostedJSONData,@UpdatedBy)

MERGE INTO FormPageData As TGT
USING #tmp As SRC
ON TGT.Id = SRC.FormPageDataId
WHEN MATCHED THEN
	UPDATE Set 
	TGT.PostedJSONData = SRC.PostedJSONData, 
	TGT.UpdatedBy = SRC.UpdatedBy, 
	TGT.UpdatedDate = SYSDATETIME()
WHEN NOT MATCHED THEN
INSERT (FormPageId,PostedJSONData,CreatedBy)
VALUES(SRC.FormPageId,SRC.PostedJSONData,SRC.CreatedBy);

SELECT @PrevPageId=fp.PrevPageId, @NextPageId=fp.NextPageId, @HTML=pg.HTML, @FPId = fp.Id
FROM dbo.FormPage fp 
JOIN dbo.[Page] pg 
	ON fp.PageId = pg.Id
WHERE fp.Id = @PageToReturn;

END

or if I should just use a select statement

Code:
ALTER PROCEDURE [dbo].[InsertOrUpdateFormPageData]
	@FormPageDataId	int null,
	@FormPageId	int,
	@CreatedBy	int null,
	@PostedJSONData	nvarchar(max),
	@UpdatedBy int null,
	@PageToReturn int
AS
BEGIN
	Create table #tmp
	(
		FormPageDataId int,
		FormPageId int,
		CreatedBy int,
		PostedJSONData nvarchar(max),
		UpdatedBy int null
	)

INSERT INTO #tmp(FormPageDataId,FormPageId,CreatedBy,PostedJSONData,UpdatedBy)
VALUES(@FormPageDataId,@FormPageId,@CreatedBy,@PostedJSONData,@UpdatedBy)

MERGE INTO FormPageData As TGT
USING #tmp As SRC
ON TGT.Id = SRC.FormPageDataId
WHEN MATCHED THEN
	UPDATE Set 
	TGT.PostedJSONData = SRC.PostedJSONData, 
	TGT.UpdatedBy = SRC.UpdatedBy, 
	TGT.UpdatedDate = SYSDATETIME()
WHEN NOT MATCHED THEN
INSERT (FormPageId,PostedJSONData,CreatedBy)
VALUES(SRC.FormPageId,SRC.PostedJSONData,SRC.CreatedBy);

SELECT fp.PrevPageId, fp.NextPageId, pg.HTML, fp.Id
FROM dbo.FormPage fp 
JOIN dbo.[Page] pg 
	ON fp.PageId = pg.Id
WHERE fp.Id = @PageToReturn;

END

Thanks,
Willie
 
Just use a SELECT statement to pass back the values you need.
Using OUTPUT variables is more complicated to code on the .NET side and there is NO advantage
 
I am not sure how familiar you are with .NET coding, but I want to point out that you can have a stored procedure that passes back more than one result set. You are not limited to just one.
If you have any questions related to this, you can post in this forum: forum855
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top