Lists in stored procedure

Hi,
I am trying to write a stored procedure that would do
something like this
create proc test
@category int,
@itemList varchar(100)
as
begin
set @itemList='select itemID from table.items where
category=@category'
EXEC sp_executesql @itemlist
select * from table.itemdesc
where itemId in (@itemlist)
end
go
so the idea, is to get all the rows in the table that meet a
certain constraint, pass the id's of those rows to another query
that would get the info for the specific item.
I also need to find out how to make the query loop over a
list of inputs and return the relevant data,
thanks

why bother w/a list & all that:
SELECT *
FROM table.itemdesc
WHERE itemId IN
SELECT itemID
FROM someTable.items
WHERE category=@category
or do a JOIN or use EXISTS

Similar Messages

  • List of Stored Procedures...

    Hi All,
    Please, help me on getting the list of stored procedures using in the db.
    Thanks in advance.

    Hi,
         You can find the Stored Procedure in object explorer window expand the Database -> Programmability ->Stored Procedures
    or check the link
    http://blog.sqlauthority.com/2007/11/27/sql-server-2005-list-all-stored-procedure-in-database/
    Regards,
    Priya

  • How to add records or how to maintain list in stored Procedure

    Hi,
    I am facing an issue in the addition of record in the type
    create or replace
    TYPE               "PROD_SEARCH_COUNT_TBL"                                          AS TABLE OF PROD_SEARCH_COUNT_OBJ;
    create or replace
    TYPE               "PROD_SEARCH_COUNT_OBJ"                                          AS OBJECT
    ( /* TODO enter attribute and method declarations here */
        V_Name Varchar2(500 Byte),
        v_Value Varchar2(500 Byte),
        v_count Number
    I want to add records to the type I tried following way
    prod_wcf_rec PROD_SEARCH_COUNT_TBL;
      SELECT PROD_SEARCH_COUNT_OBJ('Name1','Value1',1) BULK COLLECT INTO prod_wcf_rec FROM DUAL;
        SELECT PROD_SEARCH_COUNT_OBJ('Name2','Value2',2) BULK COLLECT INTO prod_wcf_rec FROM DUAL;
        SELECT PROD_SEARCH_COUNT_OBJ('Name3','Value3',3) BULK COLLECT INTO prod_wcf_rec FROM DUAL;
        SELECT PROD_SEARCH_COUNT_OBJ('Name4','Value4',4) BULK COLLECT INTO PROD_WCF_REC FROM DUAL;
    DBMS_OUTPUT.PUT_LINE ('-----------------------------Looping----------------------------');
    FOR i in 1 .. prod_wcf_rec.COUNT
    LOOP         
    DBMS_OUTPUT.PUT_LINE (prod_wcf_rec(i).V_Name||' - '||prod_wcf_rec(i).v_Value||' - '||prod_wcf_rec(i).v_count);     
       END LOOP;
       DBMS_OUTPUT.PUT_LINE ('-----------------------------Looping----------------------------');
    In printing I am getting last records only .So is there any way to add records to PROD_SEARCH_COUNT_TBL
    How to add records or how to maintain list in stored Procedure

    Every time you BULK COLLECT into a collection type the value is overwirtten. So you need to try something like this.
    SQL> create or replace type prod_search_count_obj as object(v_name varchar2(500 byte), v_value varchar2(500 byte), v_count number)
      2  /
    Type created.
    SQL> create or replace type prod_search_count_tbl as table of prod_search_count_obj
      2  /
    Type created.
    SQL> declare
      2    prod_wcf_rec prod_search_count_tbl;
      3  begin
      4    select obj_val
      5      bulk collect into prod_wcf_rec
      6      from (
      7            select prod_search_count_obj('name1','value1',1) obj_val from dual
      8            union all
      9            select prod_search_count_obj('name2','value2',2) from dual
    10            union all
    11            select prod_search_count_obj('name3','value3',3) from dual
    12            union all
    13            select prod_search_count_obj('name4','value4',4) from dual
    14           );
    15
    16    dbms_output.put_line ('-----------------------------looping----------------------------');
    17    for i in 1 .. prod_wcf_rec.count
    18    loop
    19      dbms_output.put_line (prod_wcf_rec(i).v_name||' - '||prod_wcf_rec(i).v_value||' - '||prod_wcf_rec(i).v_count);
    20    end loop;
    21    dbms_output.put_line ('-----------------------------looping----------------------------');
    22  end;
    23  /
    -----------------------------looping----------------------------
    name1 - value1 - 1
    name2 - value2 - 2
    name3 - value3 - 3
    name4 - value4 - 4
    -----------------------------looping----------------------------
    PL/SQL procedure successfully completed.
    SQL>

  • How to find the list of unused stored procedures in SQL Server 2005?

    Hi,
    I need to find out the list of stored procedures which are not in use.
    I found there is something called "sys.dm_exec_procedure_stats " for SQL server 2008.
    Can you please suggest your ides here to do the same job for SQL server 2005.
    Many Thanks.

    In SQL 2005 there is, sort of. This is query lists the last execution
    time for all SQL modules in a database:
       SELECT object_name(m.object_id), MAX(qs.last_execution_time)
       FROM   sys.sql_modules m
       LEFT   JOIN (sys.dm_exec_query_stats qs
                    CROSS APPLY sys.dm_exec_sql_text (qs.sql_handle) st) 
              ON m.object_id = st.objectid
             AND st.dbid = db_id()
       GROUP  BY object_name(m.object_id)
    But there are tons of caveats. The starting point of this query is
    the dynamic management view dm_exec_query_stats, and the contents is
    per *query plan*. If a stored procedure contains several queries, 
    there are more than one entry for the procedure in dm_exec_query_stats.
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Why stored procedure are listed in back quotes in universe

    Hi,
    We intend to create Universe based upon the Stored Procedures.  In the dialog box where it displays the list of stored procedures available in the database, few procedures are displayed correctly but most of them are displayed in Back Quotes like  `SP_NAME`.
    Can you provide me with some insight why some procedures are displayed in Back Quotes and others are not and will it create some issues if we use these procedures in the universe.
    We are using Business Objects XI release 3.1 and our database is Sybase ASE 12.5

    Hi,
    Thanks for your reply.
    We do not have any special characters in the stored procedure name or in the resultset field name that are returned by the procedures.
    Thanks,

  • How do i execute a stored procedure?

    1. how can i see the list of stored procedures i might have in an oracle database from sqlplus?
    2. say i could see a stored procedure called my_procedure, how can i see the code in this procedure?
    3. how can i execute it from sqlplus?
    thanks u very much.
    - asker

    I ignore your statements fully. I have a joke here.
    There is one fools' gang in a city and they want to prove that they are not fools. One of them says " If we can find the most intelligent man in the city, then we can no more be called fools." Then on one fine morning, one of the fools sets out on a search. He goes to a taxi-driver and asks him to take him to the most intelligent man in the city. Taking advantage, the taxi fellow roams around the entire city for the whole day and end of the day brings him to the same spot where he met him in the morning.
    Taxi driver says, "Sir! I've not seen anyone who is more intelligent than me in this whole city. To prove this, I ask you a question and you answer me. If you can't answer me, then I'm the most intelligent in the city." Even the fool agrees on this and the Taxi driver continues saying, "We are four in our house. My mother, my wife, my son and who is the fourth one?". The fool couldn't answer that and the taxi driver says "The fourth one is me". the fool accepts his ignorance and sets off from the place to their fools-community gathering.
    In the gathering, this folls explains other fools about his achievement and says that the taxi-driver is the most intelligent in the city. When other persons insist on how he had come that conclusion, he asks the same question to his community as says, "We are four in our house. My mother, my wife, my son and who is the fourth one?". The other fools fail to answer and ask for the answer excitedly. This fools replies, "The taxi driver".
    In the same way as the fool, if I give direct answer to the question, the OP doesn't know the logic behind the answer and then just blindly remembers the answer rather than learning. That is the reason I insisted on doing some search before giving a solution.
    Have a nice day ahead.
    And yeah! Neither the number of posts not you can judge my skills. Rest of the community knows.
    Cheers
    sarma.
    Added the bolded text.
    Message was edited by:
    Radhakrishna Sarma

  • How can I Use a Stored Procedure from Microsoft SQL Server?

    Hi All,
    Would like to use stored procedure as my data service in Visual Composer.
    Our version is VC7.0 SP20.
    Is stored procedure feasible? What is the system that i need to create  in Enterprise Portal? Currently i have BI JDBC System which only allows me to search for tables from SQL Server.
    Much appreciate your help.
    Thanks & Regards,
    Sarah

    Hi Skif,
    Referring to my post: JDBC System Connection VS BI JDBC System Connection
    I do not even able to view list of stored procedure. I need help too..maybe you can show me how do you set the connection? What are the mandatory connection properties? I have filled up:
    - Connection URL
    - Driver class name
    - User Administration: User, Admin (I did user mapping too)
    The error message that i got is:
    com.sap.guimachine.portalconnector.commandhandler.CommandException: Failed to connect to backend system. Check your system definition and user privileges.#
    I had assigned full control to the DB user, and map my portal user to that DB user.
    Many Thanks,
    Sarah

  • Where are User Defined Functions and Stored Procedures kept in SQL Server?

    Hi,
    I have a growing list of Stored Procedures and User-Defined Functions in SQL Server, and would like to be able to list all my user SP and UDF easily.
    Is it possible to write a query to list all SP and UDF?
    I saw the following specimen code in an SQL book, but am not sure this is what I need because I could not make it work.
    SELECT *
        FROM INFORMATION_SCHEMA.ROUTINES
    WHERE SPECIFIC_SCHEMA = N'CustomerDetails'
        AND SPECIFIC_NAME = N'apf_CusBalances'
    I tried:
    SELECT *
        FROM INFORMATION_SCHEMA.ROUTINES
    but it does not work.
    Suppose all my SP are named following this pattern:
        dbo.usp_Storeproc1
    How would I modify the above code? or is there a better code?
    Thanks
    Leon Lai

    Hi ,
    try this to get list of all stored procedures:
    SELECT *
    FROM sys.procedures where name like 'dbo.usp%'
    Thanks,
    Neetu

  • How to find Packaged Stored Procedure?

    Hi,
    Is there any API routine to find out if a particular StoredProcedure belongs to a package or not?? Or in the other way around , can i get to know about a list of Stored procedures a particular database package has?
    ( P.S :- getProcedures method in DatabaseMetadata gives just a list of all Standalone and Packaged procedures as one list. )
    Narendran.

    Hi,
    I have the same problem to call a stored Procedure in a Package.
    If my procedure is not in a package, everything is right.
    And I musn't indicate all the name, because I musn't put '.' in the name (of the procedure), in my DT on XI.
    It would be so nice to have an answer.
    Rémi

  • Extracting the DDL(Code) of only stored procedures

    DB Version:11g
    I have a list of Stored procedures (about 250) whose code ie. CREATE OR REPLACE STORED PROCEDURE statements i want to extract to a file.
    Is there an option in imp or impdp where i can extract only the code of these stored procedures?
    Apart from imp/impdp , is there any other way to do this?

    Is there an option in imp or impdp where i can extract only the code of these stored procedures?impdp, Yes. Write the contents to a file using, SQLFILE option.
    DUMPFILE = exp.dmp
    LOGFILE = exp.log
    SCHEMAS=TEST
    INCLUDE= PROCEDURE:"IN ('SP')"
    SQLFILE=sp..sql
    Or use dbms_metadata.get_ddl
    set long 99999
    set pagesize 0
    SELECT DBMS_METADATA.GET_DDL(object_type, object_name, owner) FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'PROCEDURE' and OWNER='OWNER';HTH
    -Anantha

  • Grant execute to all custom stored procedures, not quite working

    I would like to grant execute permissions on all custom stored procedures to a group and have tried using this:
    USE myDB
    select 'GRANT EXECUTE ON ['+name+'] TO [mydomain\mydb] '  from sys.objects  where type ='P' and is_ms_shipped = 0
    It seems to execute without error, I see the full list of stored procedures I've created listed like this:
    GRANT EXECUTE ON [procUserInsert] TO [mydomain\mydb]
    However the stored procedure I added earlier, which didn't have any explicit rights set for it, does not show that the execute permission has been granted. Other stored procedures, where I had already granted execute to the group, remain as they were (which
    is expected). I'm viewing permissions via ssms, selecting the new stored procedure, then properties, permissions. If the grant failed there ought to be an error message...I don't get it. This is sql server 2005.

    It seems like you were only printing the statement (SELECT), but it is unclear if you executed them.
    You could try granting EXECUTE permission at either the DB scope or SCHEMA scope. That would grant EXECUTE on all modules (i.e. SPs, UDFs, etc.) at the chosen scope, including modules created in the future. For example:
    -- Grant EXECUTE permission on all DB
    GRANT EXECUTE TO [myDomain\mydbGroup1]
    go
    -- Grant EXECUTE permission on schema mySchema
    GRANT EXECUTE ON SCHEMA::[mySchema] TO [myDomain\mydbGroup2]
    go
    I hope this helps.
    BTW. The way you were creating the dynamic SQL for granting permissions to SPs individually is subject to 2<sup>nd</sup> order injection attacks. You should use quotename(name) in order to escape object names.
    -Raul Garcia
     SQL Server Security
    This posting is provided "AS IS" with no warranties, and confers no rights.

  • Dynamic Execution of Stored Procedure

    Hi Everybody!
    I have two questions for you. All my questions are pertaining PL/SQL programming with Oracle 8i. But before that, I would like to introduce a bit about the background.
    We have .NET based application, which calls some 80 odd Oracle stored procedures one after one. The input parameters for all these stored procedure are same i.e. two IN parameters of Integer type and a OUT parameter of cursor type. The name of these stored procedures are listed in table (let say tblSPTable). We use to get the list of stored procedures from this table and execute them one after one.
    Sooner or later we realized that, this way of calling the stored procedures is causing a performance issue. So, we thought of moving the call to all these stored procedures to a new stored procedure. We thought of giving a call to this new stored procedure, which will in turn execute all these stored procedures one after one (by using the tblSPTable), and return us the all the cursors at one shot. But here is where we got stuck:
    How can I declare a OUT parameter for a list of cursors? Because I need to store the output of all the 80 odd calls in different cursors and have to get it back. I have tried to declare VARRAY of cursors or TABLE of cursors but it is not supported. One way of doing this is to declare all the 80 cursors as OUT parameters in the new stored procedure, but that is absolutely a bad programming practice. Apart from that, in future if we want to modify the order of the stored procedure, OR if we want to add or remove few stored procedures listed in tblSPTable, we have to modify this new procedure every time. My question is how can I declare or use a variable which can hold the list of cursors, which I can use from a .NET base application.
    Secondly, I will get the name of all the stored procedure by querying the tblSPTable, and will execute them dynamically. I have tried out something like this but not succeeded
    declare
    cur_result some_package.some_cursor;
    var_spname varchar;
    begin
    var_spname:=’pr_some_procedure’;
    execute immediate 'begin ‘ || var_spname || ‘(:2); end;' using out cur_result;
    end;
    Bur, I am getting an error saying “Error while trying to retrieve text for error ORA-03113”. I have scanned through few docs available over net, but none of them are really helpful. My question is how can I dynamically execute a stored procedure which has a cursor as a OUT parameter.
    Please help me out if you people have any idea regarding this. Please let me know whether my approach is correct or not. I am waiting for your valuable suggestions.
    Thanking you
    Ayan Mitra
    Message was edited by:
    user588628

    your solution will work out only in case all the functions returning you a cursor which holds same number of columnNot so. It is unfortunate that my example does not make that clear.
    The UNION ALL is of a single column which is of type weak refcursor.
    By way of example the below changes the p_dept procedure so it returns two columns rather than three.
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
    SQL> CRE ATE OR REPLACE PROCEDURE p_dept (
      2     p_deptno IN dept.deptno%type,
      3     p_resultset OUT SYS_REFCURSOR)
      4  IS
      5  BEGIN
      6     OPEN p_resultset FOR
      7        SELECT d.deptno, d.dname
      8        FROM   dept d
      9        WHERE  d.deptno = p_deptno;
    10  END p_dept;
    11  /
    Procedure created.
    SQL> VARIABLE p_resultset REFCURSOR;
    SQL> BEGIN
      2     :p_resultset := f_all (
      3        p_deptno => 30,
      4        p_functions => varchar2_table ('F_DEPT', 'F_EMP'));
      5  END;
      6  /
    PL/SQL procedure successfully completed.
    SQL> PRINT p_resultset;
            RN FN     RS
             1 F_DEPT CURSOR STATEMENT : 3
    CURSOR STATEMENT : 3
        DEPTNO DNAME
            30 SALES
             2 F_EMP  CURSOR STATEMENT : 3
    CURSOR STATEMENT : 3
         EMPNO ENAME      JOB
          7499 ALLEN      SALESMAN
          7521 WARD       SALESMAN
          7654 MARTIN     SALESMAN
          7698 BLAKE      MANAGER
          7844 TURNER     SALESMAN
          7900 JAMES      CLERK
    6 rows selected.
    SQL>[pre]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Using SQL stored procedures How to get the list of .rar files from e:\Tempbackup directories from the different remote desktop windows server and delete the .rar files which contains the current month and date

    Concept:
    Every month i need to find the list of .rar files from the
    E:/TempBackup directory from the different environments (remote desktop servers) and i need to delete the current month
    .rar files alone from the respective (E:/TempBackup) directory. below is example files structure inside the
    E:/TempBackup.
    example:
    zDROP_2014_08_31_Backups.rar
    zDROP_2014_09_31_Backups.rar
    zDROP_2014_10_31_Backups.rar
    from the above list i need to delete the zDROP_2014_08_31_Backups.rar(current month) file alone and also logs should be capture for this deletion.
    key words for this are zDROP and Current month and date.
    i need a stored procedure for this concept. could you please help on this.

    Hello,
    You can schedule an operating system task (Control Panel -> Schedule task) to either call a batch file or a PowerShell script.
    Please read the following resources for examples:
    http://social.technet.microsoft.com/Forums/windowsserver/en-US/61373719-fffb-4d94-bdbe-7d8ed8620b44/delete-filesfolders-on-remote-servers-using-powershell?forum=winserverpowershell
    http://www.networknet.nl/apps/wp/archives/943
    http://jeffwouters.nl/index.php/2011/10/powershell-script-to-delete-files-older-that-a-week/
    Hope this helps.
    Regards,
    Alberto Morillo
    SQLCoffee.com

  • SQL query statement  for stored procedure / function listing ...

    Hi everyone,
    Is there a SQL query to list all the stored procedures and functions of an user in an Oracle 8 database?
    I have this idea:
    select * from USER_SOURCE where TYPE = 'PROCEDURE' or TYPE = 'FUNCTION'
    but I am not too sure whether this is correct.
    Thanks in advance,
    Eric

    Yeah
    I agree with you Garcia , my above posting was a correction to the query mentioned in the question only.
    you are correct
    If you only want the name of the object,
    SELECT Object_Name from User_Objects where object_type in ( 'PROCEDURE' ,'FUNCTION');
    is much faster than Selecting (distinct) from User_Source.

  • Can't see my Oracle stored procedure in the avalable data source list

    Post Author: Moori
    CA Forum: Data Connectivity and SQL
    Hello All,
    I've created an Oracle stored procedure which I need to use it's returned data in my crystal report but I can't find it in the data source list.
    I'm using the same schema & database and user as my sp was created on.
    Please advise.
    Moori

    Jim (also Geoff and Hal)-
    Still haven't solved this. Here's another try at a screenshot, showing just below this text for me, of my Library interface showing the C drive listed in the left panel and not showing the C drive contents. Only, as you said is intended, what is already in Lightroom.
    I thought if I was not in "Catalog" I had been able, in the past, to select from anything in my C crive simply by opening that folder in the panel.
    When I click import it is usually - from memory - to start the import of the already selected items. For a time that's what was happening from the above screen if I selected Import. It would apparently start re-importing the selected items and without presenting the usual familiar dialog asking about file naming, etc. Now, however, clicking Import bring up this:
    Never saw this black horizontal dialog box before this situation arose. I was able to import but not as usual and I would like to get back the square (moreso at least) "white" dialog box. No clue what's going on or how to get bact to familiar ground. Ideas?

Maybe you are looking for