views:

62

answers:

1

Hi Guys,

A simple question about Stored Procedures.

I have one stored procedure collecting a whole bunch of data in a table. I then call this procedure from within another stored procedure. I can copy the data into a new table created in the calling procedure but as far as I can see the tables have to be identical.

Is this right? Or is there a way to insert only the data I want?

For example....

I have one procedure which returns this:

SELECT @batch as Batch, @Count as Qty, pd.Location, cast(pd.GL as decimal(10,3)) as [Length], cast(pd.GW as decimal(10,3)) as Width, cast(pd.GT as decimal(10,3)) as Thickness FROM propertydata pd GROUP BY pd.Location, pd.GL, pd.GW, pd.GT

I then call this procedure but only want the following data:

DECLARE @BatchTable TABLE ( Batch varchar(50), [Length] decimal(10,3), Width decimal(10,3), Thickness decimal(10,3), )

INSERT @BatchTable (Batch, [Length], Width, Thickness) EXEC dbo.batch_drawings_NEW @batch

So in the second command I don't want the Qty and Location values.

However the code above keeps returning the error:

"Insert Error: Column name or number of supplied values does not match table"

+1  A: 

The tables don't have to be identical, the results of the query from the first stored proc need to match the columns defined in the second, and they don't.

You need to change the first stored procedure to not return Qty and Location.

Select Batch, [Length], Width, Thickness FROM (
SELECT @batch as Batch, @Count as Qty, pd.Location, cast(pd.GL as decimal(10,3)) as [Length], cast(pd.GW as decimal(10,3)) as Width, cast(pd.GT as decimal(10,3)) as Thickness 
FROM propertydata pd GROUP BY pd.Location, pd.GL, pd.GW, pd.GT) 
Matthew Flynn
Couldn't you SELECT the columns for the insert from the output of the first stored procedure? I can't remember the syntax but something like `INSERT @BatchTable (Batch, [Length], Width, Thickness) (Select Batch, [Length], Width, Thickness From EXEC dbo.batch_drawings_NEW) @batch` or `SELECT Batch, [Length], Width, Thickness INTO @BatchTable FROM EXEC dbo.batch_drawings_NEW) @batch` ?
Lazarus
I believe you could do that too. I'm not sure how consistent that is between DBs.
Matthew Flynn
Sorry I should have been more clear.I want to SELECT all those properties in the first procedure because I want to use it in other procedures that may need the extra data. It's just that the calling procedure in this example doesn't require some of them. SO I thought rather than insert all into a new table, can I insert some?I'll have a go at SELECTing the columns for the INSERT though.Cheers.
SumGuy
@SumGuy - I think you're going to run into some inefficiency here, using a query that selects data that you really don't need in one procedure and then filtering it in the secound. Icould be wrong, but it seems like you'd be better off in the long run having more procedures that return exactly what is needed.
Matthew Flynn