我想在条件变量上创建一个游标,以下是一个简短的片段:
DECLARE
@my_var VARCHAR(8)
SET @my_var = (some value is obtained and set for @myvar)
DECLARE my_cursor CURSOR FOR
SELECT
my_cols
FROM
my_table
WHERE
some_col < @my_var
在声明游标之前,我需要从变量中获得的条件@my_var
,但 Sybase 抱怨DECLARE CURSOR
必须在它自己的批处理中。但是,如果我在GO
之前放置 a ,DECLARE CURSOR
则该变量@my_var
将超出范围。
如何解决此范围界定问题?
导致问题的部分代码:
DECLARE
@threshold_date VARCHAR(8),
@retain_period INT
SET @retain_period =
(
SELECT
RETAIN_PERIOD
FROM
ARCHIVE_RETAIN_PERIOD
)
IF(@retain_period > 0)
SET @retain_period = @retain_period * -1
SET @threshold_date = CONVERT(VARCHAR(8), DATEADD(DAY, @retain_period, GETDATE()), 112)
DECLARE archive_cursor CURSOR FOR
SELECT
TIMESTAMP,
TIMESTAMP_GRP,
CL_ORDER_ID,
CL_ORDER_GROUP_ID,
CL_PARENT_CHILD,
CL_PARENT_ORDER_ID,
CL_NUM_CHILDREN,
CL_PRIMARY_STATE,
CL_SECONDARY_STATE,
CL_PENDING_CHANGE,
CL_QUANTITY_FILLED,
CL_QUANTITY_FILLED_HOUSE,
CL_QUANTITY_FILLED_BROKER,
CL_QUANTITY_FILLED_CLIENT,
CL_QUANTITY_REMAINING,
CL_NUM_EXECUTIONS,
CL_CHILD_QUANTITY,
CL_CHILD_QUANTITY_REMAINING,
CL_GROSS_AVG_PRICE,
CL_GROSS_AVG_PRICE_HOUSE,
CL_GROSS_AVG_PRICE_BROKER,
CL_QUANTITY_BOOKED,
CL_BOOKING_PRICE,
COMPLETION_REASON,
COMMISSION,
SALES_CREDIT,
MARKUP,
ROUTED_COUNTERPARTY_ID,
CURRENT_EXECUTOR_ID,
LAST_EXECUTOR_ID,
RETAINED_EXECUTOR_ID,
ROUTED_TO,
RETAINED_SERVICE_ID,
UPDATE_COUNT,
UPDATE_USER,
UPDATE_DATE,
TRADER_MANAGED
FROM
my_db
WHERE
TIMESTAMP < @threshold_date
AND
CL_PRIMARY_STATE = "C"
GO