![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
SQL Anywhere 9.0.2.3228 We often use procedures to return result sets in which the ordered by or where clause is specified by parameters. To do this we have been wrapping the entire select statement in an EXECUTE IMMEDIATE. Is there another easy way to accomplish this? I've provided an example of how we currently accomplish this in ASA 9.0.2 and some other ways I have tried and failed. Thanks Jim CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; SET @SQLStatement = 'SELECT Column1, Colomn2 FROM Table1'; IF @OrderBy IS NOT NULL THEN SET @SQLStatement = @SQLStatement || ' ORDER BY ' || @OrderBy; END IF; EXECUTE IMMEDIATE WITH RESULT SET ON @SQLStatement; END; --- Helper function used below CREATE FUNCTION DBA.SQLOrderBy( IN @OrderBy LONG VARCHAR ) RETURNS LONG VARCHAR BEGIN RETURN ( @OrderBy ); END; -- Attempt 1 CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; IF @OrderBy IS NULL THEN SELECT Column1, Colomn2 FROM Table1; ELSE SELECT Column1, Colomn2 FROM Table1 ORDER BY DBA.SQLOrderBy(@OrderBy); END IF; END; -- Attempt 2 CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; IF @OrderBy IS NULL THEN SELECT Column1, Colomn2 FROM Table1; ELSE SELECT Column1, Colomn2 FROM Table1 ORDER BY !@OrderBy END IF; END; |
#3
| |||
| |||
|
|
You could try and 'hardcode' the actual 'ORDER BY' in your procedure. Advantage of this approach would be that stored procedure once executed would get compiled and cached, so each consequtive run would not require compilation therefore small improvement in performance. Your SQL would also be more readable, therefore easier to debug etc. Disadvantage, it's not so generic and it would require extra maintanance with schema/spec changes. e.g. CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG INTEGER DEFAULT NULL ) RESULT( Column1 INTEGER, Column2 INTEGER ) BEGIN IF @OrderBy IS NULL THEN SELECT Column1, Column2 FROM Table1; ELSEIF @OrderBy = 1 THEN SELECT Column1, Column2 FROM Table1 ORDER BY 1; ELSEIF @OrderBy = 2 THEN SELECT Column1, Column2 FROM Table1 ORDER BY 2; END IF; Lucjan "Jim Diaz" <nospam (AT) emprisecorporation (DOT) com> wrote in message news:4ac390af (AT) forums-1-dub (DOT) .. SQL Anywhere 9.0.2.3228 We often use procedures to return result sets in which the ordered by or where clause is specified by parameters. To do this we have been wrapping the entire select statement in an EXECUTE IMMEDIATE. Is there another easy way to accomplish this? I've provided an example of how we currently accomplish this in ASA 9.0.2 and some other ways I have tried and failed. Thanks Jim CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; SET @SQLStatement = 'SELECT Column1, Colomn2 FROM Table1'; IF @OrderBy IS NOT NULL THEN SET @SQLStatement = @SQLStatement || ' ORDER BY ' || @OrderBy; END IF; EXECUTE IMMEDIATE WITH RESULT SET ON @SQLStatement; END; --- Helper function used below CREATE FUNCTION DBA.SQLOrderBy( IN @OrderBy LONG VARCHAR ) RETURNS LONG VARCHAR BEGIN RETURN ( @OrderBy ); END; -- Attempt 1 CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; IF @OrderBy IS NULL THEN SELECT Column1, Colomn2 FROM Table1; ELSE SELECT Column1, Colomn2 FROM Table1 ORDER BY DBA.SQLOrderBy(@OrderBy); END IF; END; -- Attempt 2 CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; IF @OrderBy IS NULL THEN SELECT Column1, Colomn2 FROM Table1; ELSE SELECT Column1, Colomn2 FROM Table1 ORDER BY !@OrderBy END IF; END; |
#4
| |||
| |||
|
|
You could try and 'hardcode' the actual 'ORDER BY' in your procedure. Advantage of this approach would be that stored procedure once executed would get compiled and cached, so each consequtive run would not require compilation therefore small improvement in performance. Your SQL would also be more readable, therefore easier to debug etc. Disadvantage, it's not so generic and it would require extra maintanance with schema/spec changes. e.g. CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG INTEGER DEFAULT NULL ) RESULT( Column1 INTEGER, Column2 INTEGER ) BEGIN IF @OrderBy IS NULL THEN SELECT Column1, Column2 FROM Table1; ELSEIF @OrderBy = 1 THEN SELECT Column1, Column2 FROM Table1 ORDER BY 1; ELSEIF @OrderBy = 2 THEN SELECT Column1, Column2 FROM Table1 ORDER BY 2; END IF; Lucjan "Jim Diaz" <nospam (AT) emprisecorporation (DOT) com> wrote in message news:4ac390af (AT) forums-1-dub (DOT) .. SQL Anywhere 9.0.2.3228 We often use procedures to return result sets in which the ordered by or where clause is specified by parameters. To do this we have been wrapping the entire select statement in an EXECUTE IMMEDIATE. Is there another easy way to accomplish this? I've provided an example of how we currently accomplish this in ASA 9.0.2 and some other ways I have tried and failed. Thanks Jim CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; SET @SQLStatement = 'SELECT Column1, Colomn2 FROM Table1'; IF @OrderBy IS NOT NULL THEN SET @SQLStatement = @SQLStatement || ' ORDER BY ' || @OrderBy; END IF; EXECUTE IMMEDIATE WITH RESULT SET ON @SQLStatement; END; --- Helper function used below CREATE FUNCTION DBA.SQLOrderBy( IN @OrderBy LONG VARCHAR ) RETURNS LONG VARCHAR BEGIN RETURN ( @OrderBy ); END; -- Attempt 1 CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; IF @OrderBy IS NULL THEN SELECT Column1, Colomn2 FROM Table1; ELSE SELECT Column1, Colomn2 FROM Table1 ORDER BY DBA.SQLOrderBy(@OrderBy); END IF; END; -- Attempt 2 CREATE PROCEDURE DBA.CustomOrderBy( IN @OrderBy LONG VARCHAR DEFAULT NULL ) RESULT( Column1 INTEGER, Colomn2 INTEGER ) BEGIN DECLARE @SQLStatement LONG VARCHAR; IF @OrderBy IS NULL THEN SELECT Column1, Colomn2 FROM Table1; ELSE SELECT Column1, Colomn2 FROM Table1 ORDER BY !@OrderBy END IF; END; |
![]() |
| Thread Tools | |
| Display Modes | |
| |