Showing posts with label ss2k5. Show all posts
Showing posts with label ss2k5. Show all posts

Tuesday, March 20, 2012

[SS2K5] : Populate a CTE from stored procedure result set

Hi,

I'd like to know if it's possible to populate/load a CTE from a stored procedure result set ?

Kind of :

USE MyDB;
GO
WITH MyCTE (Col1, Col2, Col3)
AS
(

EXECUTE myStoredProc

)
SELECT *
FROM MyCTE
GO

Thanks for your help.

Cheers,

Bertrand

A couple of options include loading your stored procedure output to a temp table:

INSERT NTO #aTempTable
EXEC yourStoredProcedure

and also converting your procedure into an function or perhaps a view and then you can join to the function or view. If the primary objective of the stored procedure is to be used as you describe it might be best from the outset to consider converting it into a function.

But also keep in mind that in many cases the temp table / stored procedure option will outperform the function.

|||

CTE in SQL Server 2005 is just syntactic sugar. It is similar to view in that the query expression of the CTE is parsed into the original query, compiled, optimized and executed. So there is no special optimization in terms of storing intermediate results in case of multiple references to the same CTE and so on. So you have few options:

1. Convert the SP to inline TVF - best performance

2. Convert the SP logic to a view

3. Convert the SP to non-inline TVF

4. Use a temporary table to store results from SP and reuse it

|||

Well in fact I have to perform a row by row operation on the sp result set. Which means using a cursor. And I know that using a cursor with a temporay table is not the best in term of performance so I thought that CTE was the best option...

But your first option could fits for my problem.

Thanks for your guidance.

|||What kind of row by row operation are you performing?|||

Well in fact I received a result set from one stored procedure. Each column in this result set become input parameters for an other stored proc I have to call.

It could be more "easy" if I had the right to modifiy the both of these stored proc but I'm not allowed to and I just can to plug my logic between these sp ....

[SS2K5] : Dynamically construct a FROM statement with current SCHEMA

Hi,

I've got a DB with some schemas.

There's same tables under these schemas as for example :

MySchema1.MyTable1

MySchema2.MyTable1

I wrote CRUD stored procedures for these tables.

I'd like to dynamically create the FROM statement inside the stored procedures by getting the schema name from the current context.

Kind of :

FROM SCHEMA_NAME().MyTable1

So I'll have one generic stored procedure for MyTable1.

This stored proc will be under the DB level and will not have to be replicate under all the schemas.

Is it possible ?

Thanks for any help.

BERTRANDR

Sure, sp_executesql is such a utility to run dynamic sql. Basically you construct a SQL string(NVARCHAR) with parameters first, then use sp_executesql to execute it. Look at Book Online for details.|||Why do you want to use a generic SP? Using dynamic SQL has lot of issues - complexity, performance (it depends on the case), security risks, permissions management, debugging pain, ownership chaining issues in your case etc. You should create a SP in each schema that handles the modifications for each table. This is much more robust, simpler to manage, debug, code etc.|||

Well my point is that I don t want to duplicate objects that have the same purposes and do exactly the same things for the same table but under differents schemas.

I thought it was the best thing to do .

But maybe am I wrong ..

Monday, March 19, 2012

[SS2K5] : Dynamically construct a FROM statement with current SCHEMA

Hi,

I've got a DB with some schemas.

There's same tables under these schemas as for example :

MySchema1.MyTable1

MySchema2.MyTable1

I wrote CRUD stored procedures for these tables.

I'd like to dynamically create the FROM statement inside the stored procedures by getting the schema name from the current context.

Kind of :

FROM SCHEMA_NAME().MyTable1

So I'll have one generic stored procedure for MyTable1.

This stored proc will be under the DB level and will not have to be replicate under all the schemas.

Is it possible ?

Thanks for any help.

BERTRANDR

Sure, sp_executesql is such a utility to run dynamic sql. Basically you construct a SQL string(NVARCHAR) with parameters first, then use sp_executesql to execute it. Look at Book Online for details.|||Why do you want to use a generic SP? Using dynamic SQL has lot of issues - complexity, performance (it depends on the case), security risks, permissions management, debugging pain, ownership chaining issues in your case etc. You should create a SP in each schema that handles the modifications for each table. This is much more robust, simpler to manage, debug, code etc.|||

Well my point is that I don t want to duplicate objects that have the same purposes and do exactly the same things for the same table but under differents schemas.

I thought it was the best thing to do .

But maybe am I wrong ..