INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Returning data from a stored procedure to .Net code

Returning data from a stored procedure to .Net code

(OP)
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

RE: Returning data from a stored procedure to .Net code

2
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

RE: Returning data from a stored procedure to .Net code

(OP)
ok, thanks!

RE: Returning data from a stored procedure to .Net code

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: Microsoft: ASP.NET

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close