SQL Stored Procedure Question: How can I combine two stored procedures into one

I have two stored procedures running on Microsoft SQL Server 2000 and 2005. Both of them work fine and I am able to call them from my application.

I would like to combine the two SP into one. My app calls SP1 which returns a recordset. I eveluate the data in the recordset and then, if necessary, call SP2.

I am trying to figure out how to evaluate the output from the query in SP1 and if needed execute the query in SP2...but all from within one SP.

Here are the stored procedures:

SP!:

CREATE PROCEDURE [dbo].[CheckEditVersionStamp]

@strTableName nvarchar(255),
@strIDField nvarchar(255),
@strIDValue nvarchar(255)

AS


DECLARE @SQLStatement varchar(255)

SELECT @SQLStatement = 'SELECT EditVersionStamp, EditVersionTime' +
                                             ' FROM ' + @strTableName +
                                             ' WHERE ' + @strIDField + ' = "' + @strIDValue + '"'

EXEC(@SQLStatement)
GO

**********************************************************
SP2:

CREATE PROCEDURE [dbo].[SaveEditVersionStamp]

@strTableName nvarchar(255),
@UserID nvarchar(255),
@strUserIDField nvarchar(255),
@strUserIDValue nvarchar(255)


AS


DECLARE @Time nvarchar(255)

set @Time = CONVERT(nvarchar, GetDate())

DECLARE @SQLStatement varchar(255)


SELECT @SQLStatement = 'UPDATE ' + @strTableName +
                                             ' SET EditVersionStamp = "' + @UserID + '", EditVersionTime = "' + @Time + '"' +
                                             '  WHERE ' + @strUserIDField + ' = ' + '"' + @strUserIDValue + '"'

EXEC(@SQLStatement)
GO

*************************************************************

Thanks.
Scott KrichoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
how will you do your evaluation



CREATE PROCEDURE [dbo].[SaveEditVersionStamp]

@strTableName nvarchar(255)  = null ,
@UserID nvarchar(255),
@strUserIDField nvarchar(255),
@strUserIDValue nvarchar(255)


AS


DECLARE @Time nvarchar(255)

set @Time = CONVERT(nvarchar, GetDate())

DECLARE @SQLStatement varchar(255)

IF @strTableName IS NULL

SELECT @SQLStatement = 'SELECT EditVersionStamp, EditVersionTime' +
                                             ' FROM ' + @strTableName +
                                             ' WHERE ' + @strIDField + ' = "' + @strIDValue + '"'

ELSE
SELECT @SQLStatement = 'UPDATE ' + @strTableName +
                                             ' SET EditVersionStamp = "' + @UserID + '", EditVersionTime = "' + @Time + '"' +
                                             '  WHERE ' + @strUserIDField + ' = ' + '"' + @strUserIDValue + '"'

EXEC(@SQLStatement)
GO
0
Scott KrichoAuthor Commented:
The first query returns an EditVersionStamp and an EditVersionTime.

If EditVersionStamp is NULL execute the second query.

If EditVersionStamp is NOT NULL and is different than @UserID, compare EditVersionTime to @Time.
     If more than 15 minutes, execute second query.
     If less than 15 minutes do nothing.

If EditVersionStamp is NOT NULL and equals @UserID, do nothing.
0
Scott KrichoAuthor Commented:
I have increased the points to 500 because this is very time-sensitive. I know how to apply the IF...THEN statements to determine if the second query needs to be executed. What I do not know how to do is 'read in' the results of the first query.

The first query is designed to return a single record with two fields: EditVersionStamp and EditVersionTime. I need to know how to place these values in variables so I can evaluate them in the IF...THEN statements.

Thanks.
0
Aneesh RetnakaranDatabase AdministratorCommented:
DECLARE @Time nvarchar(255)

set @Time = CONVERT(nvarchar, GetDate())

DECLARE @SQLStatement varchar(255)


SELECT @SQLStatement = 'SELECT EditVersionStamp, EditVersionTime' +
                                             ' FROM ' + @strTableName +
                                             ' WHERE ' + @strIDField + ' = "' + @strIDValue + '"'

SELECT @SQLStatement = @SQLStatement + '; UPDATE ' + @strTableName +
                                             ' SET EditVersionStamp = "' + @UserID + '", EditVersionTime = "' + @Time + '"' +
                                             '  WHERE ' + @strUserIDField + ' = ' + '"' + @strUserIDValue + '"'
                                             +' AND EditVersionStamp is null '
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott KrichoAuthor Commented:
Thank you aneeshattingal for the response but I'm not sure I understand how to make this work for the logic choices I need to make. I don't know if it can be done with a single SQL statement.

I guess my real question is: is it possible within a stored procedure to execute a SQL statement and place the results into something like a recordset. I would like to execute the first query

SELECT @SQLStatement = 'SELECT EditVersionStamp, EditVersionTime' +
                                             ' FROM ' + @strTableName +
                                             ' WHERE ' + @strIDField + ' = "' + @strIDValue + '"'

and then do something like this:

IF EditVersionStamp IS NULL
     Do this
ELSEIF EditVersionStamp = @UserID
     Do this
ELSE
     Do this
END

Is this possible?

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.