SQL Statement as Parameter for Stored Proc

This works fine in SQL script, but doesn't work when converting the script to a sp.  It doesn't like how I've defined the @PeriodEndDate parameter.  How do I set the variable to the results of a SQL statement?
CREATE PROCEDURE usp_ARAging 
-- Add the parameters for the stored procedure here
@FiscalYear int = 2014, 
@FiscalPeriod int = 5,
@PeriodEndDate date = (SELECT MAX(JEDate) FROM ERPSQL01.EpicorNamg905.Accounting.Dim_JEDate
WHERE FiscalYear = @FiscalYear
AND FiscalMonth = @FiscalPeriod)
Kirk P.

Let's start over.  The default value of a procedure argument is a "suggestion" - a value to be used when it is not provided by the caller.  So is your goal to allow the user to supply any date, not just the one that coincides with the
fiscal year and period values?  IF not, then it should not be a parameter and the value should be determined by the code inside the stored procedure. 
However, if your goal is to allow any value, then you must follow the rules for specifying an appropriate default and those rules are very restrictive.  You cannot use a query nor can you refer to other parameters.  The typical approach is to use
a "marker" value which the caller uses to indicate that it wants to use the true "default" value which you would then determine inside your stored procedure.  Often a value of NULL is used; in your procedure the code checks this parameter
for a null value and, if found, assigns it the value you used in your query above.
And while we're at it, you should code defensively and check that the set of values supplied as parameters are consistent with each other and with the logic that depends on them.  For example, presumably the period argument should be between 1 and 12
(and not null).  I suggest you reconsider the use of integers since the domain of these values FAR EXCEEDS that allowed for years and months for most business purposes.

Similar Messages

  • URGENT: CLOB as output parameter for stored proc

    HELP
    I am unable to execute a stored procedure which has a CLOB defined as an output parameter from my Java code. It executes just fine from the SQL Plus promt.
    When I try to run it through JDBC I get a PLS-00306 error.
    I am using the Thin JDBC driver. When I try to compile using the OCI driver, I get class not found compile errors trying to import oracle.sql.*.
    Thanks!
    Donna J. Polk

    The oracle.sql.* classes are part of the Oracle JDBC Driver.
    Update your CLASSPATH variable to include $ORACLE_HOME/jdbc/lib/classes12_01.zip for JDK 1.2 or $ORACLE_HOME/jdbc/lib/classes111.zip for JDK 1.1.x

  • Is it possible to pass array of strings as input parameters for stored proc

    Dear All,
    I wrote a Stored Procedure for my crystal report. now i got a modification.
    one of the parameters 'profit_center' should be modified so that it is capable to take multiple values.
    now when i run report crystal report collects more than one values for parameter profit_center and sends it as input parameter to stored procedure all at a time.
    the only way to handle this situation is the input parameter for stored procedure 'profit_center' should be able to take array of values and i have a filter gl.anal_to = '{?profit_center}'. this filter should also be modified to be good for array of values.
    Please Help.

    Or you can use sys.ODCIVarchar2List
    SQL> create or replace procedure print_name( In_Array sys.ODCIVarchar2List)
        is
        begin
                for c in ( select * from table(In_Array) )
                loop
                        dbms_output.put_line(c.column_value);
                end loop ;
        end ;
    Procedure created.
    SQL>
    SQL> exec print_name(sys.ODCIVarchar2List('ALLEN','RICHARD','KING')) ;
    ALLEN
    RICHARD
    KING
    PL/SQL procedure successfully completed.SS

  • Using Statement rather than CallableStatement for stored proc execution

    Cleary, if you need to extract output parameters from a stored procedure, then you must use a CallableStatement to execute it. However, if your stored procedure just returns a basic ResultSet, or performs a database update, is there any penalty to using a regular Statement to execute the stored procedure?
    For example -
    String sql = "exec testprocedure 'param1' 'param2'";
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery( sql );
    vs.
    CallableStatement cs = conn.prepareCall( "{call testprocedure(?,?)}" );
    cs.setString( 1, "param1" );
    cs.setString( 2, "param2" );
    ResultSet rs = cs.executeQuery();
    Any thoughts?

    Any thoughts?You would have to look at the actual driver code for a sure answer.
    But for any real database (which a stored proc suggests) I doubt there would be any difference at all. It would resolve to the same call.

  • Execute Stored Procedure SQL Statement with Parameter from Cell

    I would like to know if there is a way to pass the parameters to SQL Statement of Power Query. For example, I have a SQL Statement as follows:
    EXEC [dbo].[spReportBuilder]
    @Report = N'Revenue Summary',
    @Year = N'2014',
    @Period = N'11'
    I would like to know if it is possible to pass the @Report, @Year, @Period values from cells in the workbook, preferably without vba.
    Thanks

    rtisserand, 
    Here is the M code: 
    let
    IDValue = Excel.CurrentWorkbook(){[Name="YearTable"]}[Content],
    Source = Sql.Database("localhost", "AdventureWorks2012", [Query="EXEC [dbo].[spReportBuilder] @Report = N'Revenue Summary', @Year = N'" & Number.ToText(IDValue[ID]{0}) & "', @Period = N'11'"])
    in
    Source
    Some notes:
    You have to reference the excel query from another step in the query or you will get the following error: 
    Formula.Firewall: Query <>something<> references other queries or steps and so may not directly access a data source. Please rebuild this data combination.
    I only did the parameter for one value.
    Hope this helps.
    Reeves
    Denver, CO

  • Omiting parameters for stored procs

    Post Author: lihaze
    CA Forum: Data Connectivity and SQL
    Hi - I hope that this is a straightforward one..  I have a number of params in a shared stored proc (they do default to NULL, but the app that I am using to call Crystal firstly cannot pass in a NULL value(!!!), and also always asks for the same no. of params that are in the creport. So, I am using Crystal 8.5, I dont even want these params to be requested. How can I make sure that they arent requested in Crystal?thanks

    Post Author: yangster
    CA Forum: Data Connectivity and SQL
    to actually pass a null value using crystal reports you will have to use crystal 2008as this isn't what you are after the alternative would be to in your report edit the parameter from the stored procedureput in a value, set the default value to that value and change allow custom value to falsethis will push the default value to the parameter every single time the report is run so you will not be prompted for the parameter value anymore

  • Sender JDBC adapter -- Update SQL statement NOT work for the last record

    I'm trying to use SAP XI to send records from Oracle database to As/400 using JDBC adapter.  I've defined the communication channel for sender
    (1)  The "Query SQL statement"  = select  a_bgn_dt,  a_end_dt from  PX_PXXD WHERE NOT CU_ACTION_CD='P' 
    (2)   The "Update SQL statement"  = update PX_PXXD set CU_ACTION_CD='P'  WHERE NOT CU_ACTION_CD='P' 
    Supposed that 3 records were retrieved from (1) and successfully updated to AS/400 but only the first 2 records in Oracle database are updated according to (2)
    Any advise.
    Pansy

    Hi Pansy,
    You select and update query is looking like wrong
    kindly check below query,If you are using oracle
    (1) The "Query SQL statement" = select a_bgn_dt, a_end_dt from PX_PXXD WHERE CU_ACTION_CD !='P'
    (2) The "Update SQL statement" = update PX_PXXD set CU_ACTION_CD='P' WHERE  CU_ACTION_CD !='P'
    Thank you
    Sateesh

  • BPC 7 MS: Which SQL statements are created for writing with input schedule?

    Hi,
    I wanted to know which SQL statements are created and executed if a user submits values using an Excel input schedule to an application.
    When I check the correspoinding MS SQL server log files, I see that data is read from the three partitions belonging to the application and put into a temporary table, but I can't find anything about writing back to the application (presumably the WB partiton...) in the log.
    There are some cryptic entries in the log file as well, but they are not human-readale... are there any BPC logfiles that could tell me which SQL statements are created and executed to write back the new values to the application? Thanks!

    Hi,
    As far as i know, when a user send data entry from an excel schedule, it will be written in the WB table of the application (for each application, you have 3 Data tables : WB, Fact2 and Fact).
    I presume that the SQL statement may be an INSERT or UPDATE statement.
    Technically, the update is done by the send governor service (hosted on your BPC application server).
    There is no log that will show you the SQL Statement besides a SQL trace that you have to setup in SQL Server 2005 Manager Studio.
    btw, the data are written in the Relationnal database but are read from the OLAP cube. The olap Cube is split in 3 partitions (ROLAP on WB table / and MOLAP on fact and fat2). Wich mean that every new entry in WB will be automatically "updated" in the cube.
    Some DM packages can directly write data in fact2 table. In this case you need to reprocess the cube to get it loaded.

  • SQL statement not working for an application

    Hello,
    Here is a sql statement which i am using to populate Department ID using another row in the table. Using these I am getting the values called department alias from different table and schema.
    UPDATE tablename_4127 PT
    SET "Deptid" = (select distinct(deptid) from datastore.get_department_alias
    where upper(ltrim(rtrim(deptalias))) = upper(ltrim(rtrim(PT."Dept Descr")))
    AND "Colid" = PT.COLID)
    Please anyone help me with this.
    Thank you

    HBUA wrote:
    Hello,
    Here is a sql statement which i am using to populate Department ID using another row in the table. Using these I am getting the values called department alias from different table and schema.
    UPDATE tablename_4127 PT
    SET "Deptid" = (select distinct(deptid) from datastore.get_department_alias
    where upper(ltrim(rtrim(deptalias))) = upper(ltrim(rtrim(PT."Dept Descr")))
    AND "Colid" = PT.COLID)
    Please anyone help me with this.
    Thank youhelp you how, since we don't have your table, data or requirements?
    How do I ask a question on the forums?
    SQL and PL/SQL FAQ

  • Select from sys.all_ind_columns works in sql*plus but not in stored proc

    I'm writing a query to find duplicate rows in a temporary table which has no unique index defined before I insert the data into the real table. I'm trying to query the sys.all_ind_columns view for the target table which belongs to a different schema:
    SELECT column_name colnm
    FROM sys.all_ind_columns i
    WHERE i.table_owner = 'SUSDB'
    AND i.index_name LIKE '%LUK'
    AND i.table_name = &tbnm
    ORDER BY i.column_position;
    This works from my GUI interface and returns five rows but when I put it into a stored procedure I get now data returned. I have granted my userid SELECT ANY DICTIONARY authority. What else do I need?

    CREATE OR REPLACE FUNCTION bog_elsa2_leslie.diag_msg_1000_cursor(i_rept_inst IN VARCHAR2,
    i_rept_time_frame IN VARCHAR2,
    i_table_name IN VARCHAR2,
    i_column_name IN VARCHAR2,
    i_domain_table IN VARCHAR2,
    i_domain_column IN VARCHAR2,
    i_domain_code IN VARCHAR2,
    i_trace_flg IN NUMBER := 0)
    RETURN VARCHAR2 IS
    cursor_stg VARCHAR2(4000);
    || File name: diag_msg_1000_cursor.fnc
    || Created by: ELSA.LESLIE
    || Created on: 20071215 4:08:20 AM
    || Purpose: Build the string that will retrieve PRIKEY and CONTENTS
    || from the target table, university, time frame, submission
    || and table passed as parameters for the 1000 dignostic.
    || Diagnostic 1000:
    || Validate that the table's logical unique key constraint
    || is not violated.
    || In: ***
    || *** ALL INPUT VARIABLES MUST BE DECLARED EVEN IF THEY ARE
    || *** NOT USED IN THIS PARTICULAR FUNCTION BECAUSE THE
    || *** EDIT_ANY_TABLE PROCEDURE CALLS THE FUNCTION IN AN
    || *** EXECUTE IMMEDIATE STATEMENT THAT REQUIRES THE 'USING'
    || *** PHRASE BE PART OF THE CALLING PROGRAM RATHER THAN BEING
    || *** INCLUDED IN THE RETURNED CURSOR_STG.
    || ***
    || i_rept_inst = reporting institution (eg. UF, FSU)
    || i_rept_time_frame = reporting time frame (eg., 200608, 20062007)
    || i_table_name = target table to be edited (eg. BUILDINGS, ENROLLMENTS)
    || i_trace_flg is optional and is used for debugging (values: TRUE/FALSE)
    || [not used] i_column_name = eg. ALTER_YR, BASE_YR
    || [not used] i_domain_table = the name of the domain table containing valid values
    || [not used] (eg. DOMAIN_MAIN_VALUES, DOMAIN_UNIV)
    || [not used] i_domain_column = the name of the column in the domain table that
    || [not used] contains the valid values (eg. CODE, OPEID_CD)
    || [not used] i_domain_code = the 5-digit domain code (eg. 10021, 01045)
    || Out: cursor_stg = executable SQL to return error row information
    || Dependencies: Logical Unique Key for tables must be have the string 'LUK'
    || postpended to it's name.
    || Exceptions: none
    || Copyright: BOG 2007
    || ***************************************************
    || Modifications:
    || Userid - Date - Modification description
    -- Constants and special assignments
    rept_inst VARCHAR2(4) := upper(i_rept_inst);
    rept_time_frame VARCHAR2(8) := i_rept_time_frame;
    table_name VARCHAR2(30) := upper(i_table_name);
    column_name VARCHAR2(30) := upper(i_column_name);
    domain_table VARCHAR2(30) := upper(i_domain_table);
    domain_column VARCHAR2(30) := upper(i_domain_column);
    domain_code VARCHAR2(5) := upper(i_domain_code);
    trace_flg NUMBER := i_trace_flg;
    file_name VARCHAR2(100) := 'diag_msg_1000_cursor: ';
    unq_cols VARCHAR2(1000) := NULL;
    unq_contents VARCHAR2(4000) := NULL;
    unq_where VARCHAR2(4000) := NULL;
    -- Accounting of process n/a all contained in the for loop
    row_num NUMBER := 0;
    -- Object types - n/a
    -- Cursors
    CURSOR ix_cols_curr(tbnm VARCHAR2 := table_name) IS
    SELECT column_name colnm
    FROM sys.all_ind_columns i
    WHERE i.table_owner = 'SUSDB'
    AND i.index_name LIKE '%LUK'
    AND i.table_name = tbnm
    ORDER BY i.column_position;
    -- Cursor variables n/a
    -- errors and exceptions
    sql_code NUMBER;
    sql_errm VARCHAR2(255);
    BEGIN
    -- output trace information if requested
    IF trace_flg = 1
    THEN
    dbms_output.put_line(file_name || 'rept_inst = ' || rept_inst);
    dbms_output.put_line(file_name || 'rept_time_frame = ' || rept_time_frame);
    dbms_output.put_line(file_name || 'table_name = ' || table_name);
    dbms_output.put_line(file_name || 'column_name = ' || column_name);
    dbms_output.put_line(file_name || 'domain_table = ' || domain_table);
    dbms_output.put_line(file_name || 'domain_column = ' || domain_column);
    dbms_output.put_line(file_name || 'domain_code = ' || domain_code);
    END IF;
    -- ix_cols_loop builds a string of the columns contain in the
    -- logical unique key which is then later incorporated into the
    -- select cursor returned to the calling program.
    FOR this_col IN ix_cols_curr(table_name)
    LOOP
    <<ix_cols_loop>>
    -- output trace information if requested
    IF trace_flg = 1
    THEN
    row_num := ix_cols_curr%ROWCOUNT;
    dbms_output.put_line(file_name || 'row=' || row_num ||
    'column_name=' || this_col.colnm);
    END IF;
    IF unq_cols IS NOT NULL
    THEN
    unq_cols := unq_cols || ', ';
    unq_contents := unq_contents || ', ';
    END IF;
    unq_cols := unq_cols || this_col.colnm;
    unq_contents := unq_contents || q'[']' || this_col.colnm ||
    q'['=]' || this_col.colnm;
    unq_where := unq_where || 'AND tbl.' || this_col.colnm ||
    ' = dups.' || this_col.colnm;
    END LOOP ix_cols_loop;
    IF trace_flg = 1
    THEN
    dbms_output.put_line(file_name || 'unq_cols =' || unq_cols);
    dbms_output.put_line(file_name || 'unq_contents =' || unq_contents);
    dbms_output.put_line(file_name || 'unq_where =' || unq_where);
    END IF;
    cursor_stg := 'SELECT prikey, ' || unq_contents || ' AS contents ' ||
    'FROM univdb.' || table_name || ' tbl, (SELECT ' ||
    unq_cols || ', COUNT(*) FROM univdb.' || table_name ||
    q'[ WHERE rept_inst = ']' || rept_inst || q'[']' ||
    q'[AND rept_time_frame = ']' || rept_time_frame || q'[']' ||
    ' GROUP BY ' || unq_cols || ' HAVING COUNT(*) > 1) dups' ||
    q'[ WHERE tbl.rept_inst = ']' || rept_inst || q'[']' ||
    q'[ AND tbl.rept_time_frame = ']' || rept_time_frame ||
    q'[']' || unq_where;
    -- output trace information if requested
    IF trace_flg = 1
    THEN
    dbms_output.put_line(file_name || 'cursor_stg = ' || cursor_stg);
    END IF;
    RETURN(cursor_stg);
    EXCEPTION
    -- block exception
    WHEN OTHERS THEN
    sql_code := SQLCODE;
    sql_errm := SQLERRM;
    IF trace_flg = 1
    THEN
    dbms_output.put_line(file_name || ' ROW NUM ' || row_num || ' sql code ' ||
    sql_code || ' sql message ' || sql_errm);
    END IF;
    END diag_msg_1000_cursor;

  • Help: Trying to pass a parameter to stored proc from FORMS

    I am working on a FORMS based on stored procedure with 7 parameters. All parameters work except p_sort for ORDER BY CLAUSE.
    Any suggestions?
    Thank you in advance.
    Jimmy

    pls post some more details .

  • Execute stored-prozedure in sql-statement in jdbc-adapter for sender

    Hello,
    in the sql-statement i call a stored procedure which makes following:
    - doing a selcet
    - update one field
    - give a recordset back
    In the update-field i write:
    But in the adapter monitoring i become a sql-exception:
    Error: SQLException during query 'EXECUTE st_enum_EAI_lokation': java.sql.SQLException: [SQLServer 2000 Driver for JDBC]No ResultSet set was produced.
    I´m using sql-server.
    So there is no resultset coming back...
    The reason for the stored-procedure is that it is not allowed to make changes in a productive data-base directly by an update.
    What´s the problem?
    Thanks in advance,
    Frank

    Currently, the jdbc sender adapter cannot execute Oracle's stored procedure.  Oracle's stored procedure cannot return a resultset, which is required by the jdbc sender adapter.
    An adapter user-module is available for a sender adapter to execute Oracle's stored procedure.
    If you are interested, I can send it to you.
    Regards

  • BizTalk Stored Proce-passing XML as one of the Input parameter and String as another parameter

    I have a requirement in BizTalk that
    - I will receive XML from Source and i need to submit this XML data and two other string parameters in  SQL storeprocedure  as a parameters and submit data
    Ex: My_SP(myID Integer INPUT,myXML xml Input,mystring OUT)
    Could you please help me how call storeprocedure and submit multiple parameters in BizTalk.

    you can execute stored procedure by generating schemas from WCF-SQL Adapter.
    for passing parameters you will have to do the mapping to the Generated schema for Stored proc.
    I would suggest to do this in Message Assignment shape, there you can easily assign all the parameters.
    Integer and String parameters can be assigned from normal variables and XML parameter can be inserted as suggested by Abhishek-
    xmldoc=requestMsg;
    varOuterstring=xmldoc.Outerxml.Tostring();
    Please refer the below article.
    https://www.packtpub.com/books/content/new-soa-capabilities-biztalk-server-2009-wcf-sql-server-adapter
    http://msdn.microsoft.com/en-us/library/dd787968.aspx
    Thanks,
    Prashant
    Please mark this post accordingly if it answers your query or is helpful.

  • Batch executing SQL Statements stored in a file in SAP DB

    hi,
    I am using SAP DB as database in my j2ee application. As a part of the application purpose, i need to insert a large no. of records in to the database directly. I have created the SQL file which contains the insert statements for the records
    Now i need to execute this file sothat the insert statement which are inside the file get executed. I am using SQL Studio to access to the Database.
    I would like to know whether any options are available in SQL Studio to for executing a SQL File.
    Kindly help
    Best regards,
    Sudheesh...

    Hi,
    I believe that the solution to your problem lies in creating sql studio objects.Please refer to the link below.Select the link "sql dialog" on the page.
    The following options are available for sql dialog.
    The following options are available in the SQL dialog:
    ·        Creating and executing SQL statements
    ·        Setting parameters for SQL statements
    ·        Importing and exporting SQL statements
    Select the "Importing and exporting sql statements" link on this page to know more about it.This will tell you how to import sql statements from ascii files.
    Basically,what you need to do is From the context menu for the SQL dialog, choose Import File or Export File.
    The link I have provided,gives a lot of other information as well.
    http://help.sap.com/saphelp_nw04s/helpdata/en/ee/1c5bdfeba711d4aa2800a0c9430730/frameset.htm
    Hope this helps.
    Regards,
    Harish
    (Please award points for helpful answers)
    Message was edited by: HARISH SUBRAMANIAN

  • How to execute multiple sql statements in parallel ?

    Hi There,
    I have 10 stored procedures, each one will take approximately 5 seconds to run individually.
    Now I need to combine the results of these 10 stored procedures into single result set.
    So I have created one more stored procedure "proc_AllSPs" to get the single result set.
    The procedure "proc_AllSPs" is taking one minute to run because all the procedures inside the "proc_AllSPs" are running
    one after the other and taking 5 seconds each.
    If there is any other way that all the procedures run parallelly, then the procedure "proc_AllSPs" can run in 5 seconds time.
    Sorry if I am not being elaborated.
    PLease get back to me if any piece of information is needed.
    Any kind of help is truly thankful.
    Thanks,
    SequelBug

    Could you tell me how to execute two stored procedures or two sql statements in parallel
    For eg:
    First procedure name is proc_sp1 which takes 1 minute to run
    Second procedure name is proc_sp2 which takes 1 minute to run
    I want to accomplish the below query in 1 minute by executing statement1 and statement2 parallelly.
    go
    select * from openrowswt('sqloledb','server','exec proc_sp1')    --statement1
    union
    select * from openrowset('sqloledb','server','exec proc_sp2')     --statement2
    go
    OR
    go
    Select * into #Temp1  from openrowswt('sqloledb','server','exec proc_sp1') --statement1
    select * into #Temp2 from openrowset('sqloledb','server','exec proc_sp2') --statement2
    Select * from #Temp1
    UNION ALL
    Select * from #Temp2
    go
    Thanks,
    SequelBug

Maybe you are looking for