开发者

What is a better way to write this SQL Stored Procedure?

开发者 https://www.devze.com 2023-02-24 10:19 出处:网络
I am updating a Trades Transactions Log using a SQL Stored Procedure, and I am updating Current Order Table at the same time with the same sproc.

I am updating a Trades Transactions Log using a SQL Stored Procedure, and I am updating Current Order Table at the same time with the same sproc.

Because I had a serious problem where the Log table did not update and the Current Order table did... I added a (3rd) routine to the bottom which checks to see if the Log Table was updated referencing an ID (ClientID), then entering an error if not present to error table.

I am asking... how badly written is this sproc ?? Help or advice appreciated.

ALTER PROCEDURE dbo.sprocVT4_addTradeLong
@seqno varchar(35) = NULL,
@exctyp varchar(35) = NULL,
@ordstat varchar(35) = NULL,
@clid varchar(35) = NULL,
@exid varchar(35) = NULL,
@type varchar(35) = NULL,
@side varchar(35) = NULL,
@exch varchar(35) = NULL,
@sym varchar(35) = NULL,
@lstqty varchar(35) = NULL,
@lstpri varchar(35) = NULL,
@text varchar(35) = NULL,
@cumqty varchar(35) = NULL,
@lftqty varchar(35) = NULL,
@now varchar(35) = NULL
AS
BEGIN
-- NO EXISTS ------------
Declare @RC int

SELECT [Symbol] FROM TradesLongForex T WHERE T.ExecId = @exid

SELECT @RC = @@ROWCOUNT
IF @RC <= 0

INSERT INTO TradesLongForex ([SeqNo], [ExecType], [Status], [ClientId], [ExecId], [Type], [Side], [Exchange], [Symbol], [LastQty], [LastPrice], [Text开发者_JAVA技巧], [CummQty], [LeftQty], [Date])

VALUES (@seqno, @exctyp, @ordstat, @clid, @exid, @type, @side, @exch, @sym, @lstqty, @lstpri, @text, @cumqty, @lftqty, @now)

UPDATE OrdersIdHoldForex SET [OrdExcType] = @exctyp, [OrdStatus] = @ordstat, [OrdType] = @type, [OrdSide] = @side, [OrdPrice] = @lstpri, [OrdQty] = @cumqty, [OrdRemain] = @lftqty

WHERE [Ticker] = @sym

DECLARE @RC2 int

SELECT @RC2 = @@ROWCOUNT

SELECT [ClientId] FROM TradesLongForex WHERE [ClientId] = @clid
if @RC2 <=0

INSERT INTO ERRLOG ([Date], [Message])

VALUES (GETDATE(), 'ERROR INSERTING TRADESLONGFOREX CLID = ' + CONVERT(varchar(10),@CLID))

END


Phil makes a good point about transactions. This concept is called "Atomicity" and basically means each transaction/process is atomic and self contained.

The general syntax for transactions in SQL server would be something like:

BEGIN TRY
BEGIN TRANSACTION
...

your code here

...

COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 1 ROLLBACK

... error reporting code ...

END CATCH

The gist of this is, use TRY/CATCH blocks to trap the errors, and only commit the transaction if you get through the whole TRY block without issues. Any errors send you to the CATCH block, which rolls back the open transaction.

Here's a primer on error handling.


I'm not 100% sure what you are asking, but it seems that you need to read up a bit on database transactions. Essentially you can wrap the set of queries in a transaction, and it will ensure that either all of the operations are completed, or none of them are. So if an error occurs, the entire operation will be rolled back.

http://en.wikipedia.org/wiki/Database_transaction

0

精彩评论

暂无评论...
验证码 换一张
取 消