This is a question about some stored procedures running on MSSQL 8 (I think that's SQL Server 2000).
I have two stored procedures left by my predecessor at the company.
The "Modify" window for the first procedure looks something like this:
ALTER PROCEDURE [dbo].[Proc1]
@ID,
@someBool
AS
BEGIN
SELECT colA, colB, colC
FROM myTable
WHERE colA = @ID AND colB = @someBool
END
The second procedure is very similar, being something along the lines of:
ALTER PROCEDURE [dbo].[Proc2]
@ID
AS
BEGIN
SELECT colA, colB
FROM myTable
WHERE colA = @ID AND colB = FALSE
END
Obviously, the real procedures are more compl开发者_如何学Cicated than this and require a lot more maintenance when changes are made.
Rather than maintaining these two queries separately, which pretty much sucks, I was wondering if there was any way to simply have Proc2 do something along the lines of:
ALTER PROCEDURE [dbo].[Proc2]
@ID
AS
BEGIN
EXEC Proc1(@ID, FALSE)
"drop colC"
"return modified result"
END
Any ideas on the best way to go about this?
Maybe something like this?
CREATE TABLE #Proc1TempResults
(
ColA INT, --OR WHATEVER DATA TYPE
ColB INT,
ColC INT
)
INSERT INTO #Proc1TempResults (ColA, ColB, ColC)
EXEC Proc1 @ID, 0
SELECT ColA, ColB
FROM #Proc1TempResults
The difficulty is that stored procedures are non-relational. Therefore, apart from participating in an Insert statement, they can't be leveraged in other queries; or to borrow some Linq vernacular they are not "composable".
You way want to consider table valued functions (which I believe are available as early as SQL Server 2000))
精彩评论