Showing posts with label pretty. Show all posts
Showing posts with label pretty. Show all posts

Monday, March 26, 2012

Exec procedures inside other procedures

This should be pretty simple but I am not getting an luck getting what I need by googling.

I am executing a few stored procedures inside of another stored procedure. One of stored procedures returns a value that the other 2 procedures will use how do I get that get and use that variable?

Example

ALTERPROCEDURE [dbo].[ins_mainproc_sp]

AS

(

parameters

)

BEGIN

this procedure returns an ID

exec ins_procedure1_sp 'Value', 'Value'

these 2 procedures will use the value returned by the above procedure.

exec ins_procedure2_sp id,'Value'

exec ins_procedure3_sp id,'Value'

END

I was going to break this into 2 different procedures. Run ins_procedure1_sp get the value then pass it into ins_mainproc_sp but I was hoping to not make 2 calls.

You can use the OUTPUT parameter here.

Code Snippet

ALTER PROCEDURE [dbo].[ins_mainproc_sp]

AS

(

parameters

)

Begin

Declare @.Id as int

exec ins_procedure1_sp 'Value', 'Value', @.Id OUTPUT

..

...

....

--Reuse the @.Id on the SP calls

End

Go

Alter Proc ins_procedure1_sp

(

other parameters,

@.Id int OUTPUT

)

as

Begin

Insert ......

Set @.Id = <inserted Value>;

End

Friday, March 23, 2012

Exclusive selection

Maybe it is really simple but right now it's pretty late and I don't have a clue:
Basicly I have two tables with two columns (first one is numeric)

Table A
1:A
2:A
3:B
5:NULL
7:NULL
8:C

Table B
1:A
2:NULL
3:F
5:F
7:NULL
8:NULL

The result should be:

Result Table
1:A
2:A
3:NULL
5:F
7:NULL
8:C

I tried a variaty of joins, subselects and whatever, but failed.
I would appreciate any help.

Kindest regards,
kromoyou will have to explain what you want

the results do not give a clue

for example, how do you get NULL from 3:B and 3:F ??

what are you trying to do?|||Suppose your tables are created as
CREATE TABLE taba (rb number, val varchar2(1));
CREATE TABLE tabb (rb number, val varchar2(1));
and populated as in your example.

Would this do the job?

SELECT rb, MAX(result)
FROM (
SELECT
a.rb,
DECODE(a.val, b.val, b.val, NULL, DECODE(b.val, NULL, NULL, b.val)) result
FROM TABA a, TABB b
WHERE a.rb = b.rb
UNION
SELECT
b.rb,
DECODE(b.val, a.val, a.val, NULL, DECODE(a.val, NULL, NULL, a.val)) result
FROM TABA a, TABB b
WHERE a.rb = b.rb
)
GROUP BY rb
;|||Sorry I didn't explain it further. Say the first column is named ID and the second VALUE.
The result should be for (A.ID = B.ID) and sort of XOR for VALUES.

IF (A.VALUE = B.VALUE)
A.VALUE [OR B.VALUE, it doesn't matter]

IF ( (A.VALUE IS NOT NULL) AND (B.VALUE IS NULL) )
A.VALUE

IF ( (A.VALUE IS NULL) AND (B.VALUE IS NOT NULL) )
B.VALUE

IF ( (A.VALUE IS NOTNULL) AND (B.VALUE IS NOT NULL) AND (A.VALUE <> B.VALUE))
NULL

I am working with Oracle 9.2 (right now), if there is a special thing for Oracle I'll take it, if there is an general solution I would prefer that one.

Thank you.

Kindest regards,
kromo|||Select ta.id, (CASE WHEN ta.value = tb.value THEN ...)
from tableA ta
INNER JOIN
tableB tb ON
ta.id = tb.id|||Thank you all very much.

I tried the "CASE" approach and it worked like a charm.

You saved my day.

Kindest regards,
kromo