Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Can I do this??

Author  Topic 

tejo_pan
Starting Member

10 Posts

Posted - 2005-07-28 : 13:34:45
[code]
DECLARE @TaxaCliente MONEY

INSERT INTO myTable (Column1, Column2)
(
SELECT
C.CustomerID AS Column1, -- HERE IS GOOD
(EXEC MyStoredProcedure C.CustomerID, @Tax = @CustomerTax
OUTPUT) AS Column2 -- I can't do it!!!
)
[/code]


Is this.. I want to take all the data of a table and insert in other, but, geting the data through a SP with OUTPUT

Thank you guy's!!!

nr
SQLTeam MVY

12543 Posts

Posted - 2005-07-28 : 13:37:56
No.
You can do it with a fuction or by using another connection.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-07-28 : 13:39:54
i think you need a user-defined function that returns your desired datatype here.


Go with the flow & have fun! Else fight the flow
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-07-28 : 13:40:30


haven't seen you in a while nigel...

Go with the flow & have fun! Else fight the flow
Go to Top of Page

tejo_pan
Starting Member

10 Posts

Posted - 2005-07-28 : 13:44:59
For each row of this SELECT I need execute my SP and insert this result in the new table..

Can I do it?? By geting the result of this SP with OUTPUT??
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-07-28 : 13:51:41
no.

sprocs aren't designed to do that.
what is your sproc doing?


Go with the flow & have fun! Else fight the flow
Go to Top of Page

tejo_pan
Starting Member

10 Posts

Posted - 2005-07-28 : 14:22:17
My SP get the value of the transaction (ex 14.50) and set the Transaction_Fee (ex 3.9% + US$0.40)..
ex:
14.50 - (3.9% + 0.40)
14.50 - (0.56 + 0.40)
14.50 - 0.96 = 13.54 TOTAL

But, if the result (value) of the transaction fee is higher then the payment it self, then the result should be the transaction amount one.

EX 2:
Transaction amount $0.35
0.35 - (3.9% + 0.40)
0.35 - (0.01 + 0.40)
0.35 - 0.41 = - 0.06
as the total is lower then the payment it self my SP returns the transaction amount (0.35) (just like paypal)

My output is the result of this operation (0.35)

Do I need to use this query within my select OR could I use the SP where I have this query ready. and how?




0.35 - (3.9% + 0.40)= 0.35 - 0.41 = - 0.06)
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-07-28 : 14:26:24
what part of "use a user-defined function" don't you understand?
are you maybe using sql server 7?

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-07-28 : 14:31:31
You could cursor round the SELECT values calling your SProc for each row, and then do an INSERT with each result.

If your SProc could be modified to take a list of CustomerID, and return a resultset of all of them, that could be inserted into a table (perhaps a tempoary table, and from there into the permanent table JOINed with any other data)

Kristen
Go to Top of Page

tejo_pan
Starting Member

10 Posts

Posted - 2005-07-28 : 14:53:59
Well, this is my code..
If anyone can help me...

Thanks


CREATE PROCEDURE ssp_Transacoes_InsereTransacao_ClienteNovo
(
@ClienteID NUMERIC,
@EmailPrimario VARCHAR(400)
)
AS
SET NOCOUNT OFF;


-- START THE TRANSACTION
BEGIN TRANSACTION


DECLARE
@EmailID NUMERIC,
@TaxaTransacao money


-- SELECT THE EMAILID OF THE CUSTOMER
SELECT @EmailID = EmailID FROM tbEmails WHERE ClienteID = @ClienteID


-- INSERT THE NEW TRANSACTION TO CUSTOMER
INSERT INTO tbTransacoes(
TransacaoID,
ClienteID,
ClienteTransacao,
EmailID,
DebitoCredito,
TipoTransacao,
StatusTransacao,
TipoPagamento,
ValorTransacaoBruto,
TaxaTransacao,
ValorTransacaoLiquido,
Descricao,
IP
) (
SELECT
TransacaoID,
@ClienteID AS ClienteTransacao,
T.ClienteID,
(SELECT EmailID FROM tbEmails E WHERE (E.ClienteID = T.ClienteID) AND (Habilitado = 1) AND (Ativo = 1) AND (Principal = 1) ) AS EmailID,
'C' AS DebitoCredito,
TipoTransacao,
1 AS StatusTransacao,
TipoPagamento,
T.ValorTransacaoBruto * (-1),
TaxaTransacao, -- HERE I WANT USE MY STORED PROCEDURE
ValorTransacaoLiquido * (-1), -- HERE I WILL USE MY STORED PROCEDURE AGAIN ((T.ValorTransacaoBruto * (-1) - TaxaTransacao)
Descricao,
IP
FROM tbTransacoes T
WHERE EmailInexistente = @EmailPrimario
)

-- NOW, UPDATE THE TRANSACTION TO CUSTOMER EMAIL
UPDATE tbTransacoes SET
EmailInexistente = NULL,
ClienteTransacao = @ClienteID,
EmailID = @EmailID
WHERE
(EmailInexistente = @EmailPrimario)

-- UPDATE NOW THE FEE OF THE RECEIVED TRANSACTION
UPDATE tbTransacoes SET
EmailInexistente = NULL,
ClienteTransacao = @ClienteID,
EmailID = @EmailID
WHERE
(EmailInexistente = @EmailPrimario)






IF @@ERROR = 0
BEGIN
COMMIT TRANSACTION
END
ELSE
BEGIN
ROLLBACK TRANSACTION
RETURN
END
GO




CREATE PROCEDURE ssp_Transacoes_CalculaTaxa
(
@ClienteID numeric,
@ValorTransacaoBruto money,
@Taxa money OUTPUT
)
AS
SET NOCOUNT OFF;


-- START THE TRANSACTION
BEGIN TRANSACTION


DECLARE
@BaseCalculo money, -- PERCENT TO FEE
@TaxaFixa money -- FIX FEE BY TRANSACTION


-- SELECT THE FEE
SELECT @BaseCalculo = PorcentagemTaxa
FROM tbExtratoMensal
WHERE ClienteID = @ClienteID
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END


-- SET A VALUE TO THE FIX FEE FOR EACH TRANSACTION
SET @TaxaFixa = 0.95


-- GET THE FEE
SELECT @Taxa = (@ValorTransacaoBruto * @BaseCalculo) + @TaxaFixa
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END


-- VERIFY IF THE FEE VALUE IS BIGGER THAN THE TRANSACTION AMOUNT
IF @Taxa > @ValorTransacaoBruto SELECT @Taxa = @ValorTransacaoBruto
IF @@ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END



IF @@ERROR = 0
BEGIN
COMMIT TRANSACTION
RETURN
END
ELSE
BEGIN
ROLLBACK TRANSACTION
RETURN
END
GO
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-07-29 : 03:58:02
I would move your sub-select to the top, and put the results into a temporary table, which includes an IDENTITY column.

Then I would loop around the temporary table, using the ID column, and call the SProc to get the additional values, which I would UPDATE into the Temporary Table.

Then I would use your original INSERT with the Temporary Table as the source.

Couple of things to note:

In ssp_Transacoes_InsereTransacao_ClienteNovo you make one check for @@ERROR towards the end. Note that this will ONLY check the outcome of the LAST Update statement, if any earlier statements fail it will go unnoticed.

Also, in the event you rollback the transaction you do not appear to be returning any sort of error code - and I think that will mean that a failure will not be detectable by the application that called the Sproc.

Once you have made ssp_Transacoes_InsereTransacao_ClienteNovo call ssp_Transacoes_CalculaTaxa then if ssp_Transacoes_CalculaTaxa does a ROLLBACK it will case SQL to raise a warning message which will probably muck up your application. The ways around this are a PITA, but I recommend that you deliberately create a ROLLBACK situation in [your nested Sproc] ssp_Transacoes_CalculaTaxa, during your testing, to check that the calling application will handle it properly.

Kristen
Go to Top of Page
   

- Advertisement -