In my experience, many investment banks use Sybase as their database management system.
Q. How would you go about creating a new stored procedure in Sybase?
A. The basic steps involved are
1. Check if the procedure already exists?
2. If it already exists drop the procedure
3. create a new procedure with input arguments
4. declare any additional variables to be used in the procedure
5. assign values to these variables
6. create temporary tables and load some data
7. join other tables with temp tables to process data
8. handle error scenarios
9. Keep track of the row counts
10. add logging statements where required.
Here is a very simplified example, covering a number of key aspects of a stored procedure.
-- select the schema to use
use my_db_schema
go
IF OBJECT_ID('dbo.my_stored_proc') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.my_stored_proc
IF OBJECT_ID('dbo.my_stored_proc') IS NOT NULL
PRINT '<<< FAILED DROPPING PROCEDURE dbo.my_stored_proc >>>'
ELSE
PRINT '<<< DROPPED PROCEDURE dbo.my_stored_proc >>>'
END
go
CREATE PROC dbo.my_stored_proc
(
@ParentName varchar(30) = null,
@ParentBatchJobId int = null
)
AS
--declare variables
DECLARE @error int,
@rowcount int,
@error_message varchar(240),
@error_number int,
@NumberOfDays int,
@DeleteDate datetime,
@TotalRows int,
@proc_name varchar(30),
@now datetime
--assign values to your variables
SELECT @proc_name = object_name(@@procid),
@now = GETDATE(),
@rowcount = 0,
@TotalRows = 0
select @NumberOfDays = NumberValue,
@DeleteDate = DateValue
from ConfigTable
where type = 'basic' and name='purge'
-- error handling from last select
SELECT @Error = @@Error
IF @Error <> 0
BEGIN
SELECT @error_number = 50010
SELECT @error_message = "Failed: could not get number of days parameter.(" + CONVERT(varchar(10), @Error) + ") "
GOTO Error_Exit
END
--Create a temporary table select the parant ids that need to be deleted
CREATE TABLE #tmp_parent_ids
(
ParentId numeric(12,0) not null
)
insert into #tmp_parent_ids
select distinct(parent_id) from dbo.parent_table where createddttm < @DeleteDate
if @NumberOfDays > 0
BEGIN
-- execute another stored proc and get an output to be used in this stored proc
EXEC another_stored_proc "BatchJob", @NewNum = @ChildBatchJobId Output
SELECT @Error = @@Error
IF @Error <> 0 OR @ChildBatchJobId IS NULL
BEGIN
SELECT @error_number = 50050
SELECT @error_message = "Failed: could not get the next BatchJobId from the SequenceNumber table.(" + CONVERT(varchar(10), @Error) + ") "
GOTO Error_Exit
END
//do som logic like purging rows
BEGIN
SET ROWCOUNT @PurgeRows
BEGIN TRANSACTION
DELETE child_table
FROM child_table ct, #tmp_parent_ids tmp
WHERE ct.parent_id = tmp.ParentId
SELECT @Error = @@Error, @Rows = @@Rowcount
IF @Error != 0
BEGIN
SELECT @error_number = 50100
SELECT @error_message = "Failed: (" + CONVERT(varchar(10), @Error) + ") could delete rows"
ROLLBACK TRANSACTION
GOTO Error_Exit
END
END
success_exit:
RETURN (0)
Error_Exit:
SELECT @error_message = "Error processing the proc" + " " + + @error_message;
RAISERROR @error_number @error_message
RETURN (1)
go
IF OBJECT_ID('dbo.my_stored_proc') IS NOT NULL
EXEC sp_procxmode 'dbo.my_stored_proc','unchained'
go
IF OBJECT_ID('dbo.my_stored_proc') IS NOT NULL
PRINT '<<< CREATED PROCEDURE dbo.my_stored_proc >>>'
ELSE
PRINT '<<< FAILED CREATING PROCEDURE dbo.my_stored_proc >>>'
go
IF OBJECT_ID('dbo.my_stored_proc') IS NOT NULL
GRANT EXECUTE ON dbo.my_stored_proc TO user_admin
go
@@variable_name like @@Error, @@procid are Sybase variables. @variable_name like @Error are user defined stored proc variables.