SQL query to see if "NO COUNT" is on or off

I would like to see if "NO COUNT" is turned on in an existing database.  I don't have access to the database properties in the Object Explorer and would like to determine this with a sql statement if possible.
What sql should be used to see if "NO COUNT" is on or off?

As said earlier, this is a client setting.. It has no downside and can improve performance in some cases.
https://msdn.microsoft.com/en-us/library/ms189837.aspx?f=255&MSPPError=-2147217396
"SET NOCOUNT ON prevents the sending of DONE_IN_PROC messages to the client for each statement in a stored procedure. For stored procedures that contain several statements that do not return much actual data, or for procedures that contain Transact-SQL
loops, setting SET NOCOUNT to ON can provide a significant performance boost, because network traffic is greatly reduced."
Regards, Ashwin Menon My Blog - http:\\sqllearnings.com

Similar Messages

  • SQL query to see table names for specific columns

    hi
    i have a database containing more than 100 tables. I want to see all the tables containg a column 'Employee_id'. plz tell me SQL query some other way.
    Thanks

    Time to learn data dictionary basics
    SELECT DISTINCT table_name
    FROM all_tab_cols
    WHERE column_name ='EMPLOYEE_ID';

  • SQL query to see what the db/schema is doing

    My apologies in advance as I am sure this has been covered. However, my google foo is failing me. :(
    Oracle 11g on Centos 5
    I am running a script against the database from my local pc through command prompt. I am wanting to run a query against the db to see what part of the script it is running and if it's possible I would like to be able to see if it is doing read / writes. Similar to an onstat on informix.

    Welcome to the forum!
    >
    I am running a script against the database from my local pc through command prompt. I am wanting to run a query against the db to see what part of the script it is running and if it's possible I would like to be able to see if it is doing read / writes. Similar to an onstat on informix.
    >
    Oracle has no equivalent to onstat - http://www.oninit.com/onstat/
    Oracle also has no knowledge of your 'script' contents.
    As SB mentioned you can get a list of sessions and their identifiers. Then you can query the V$SESSION_LONGOPS system view to get information about long running operations.
    See V$SESSION_LONGOPS in the Database Reference. The same doc (see index) has information about V$SESSION and V$SQL
    http://docs.oracle.com/cd/B19306_01/server.102/b14237/dynviews_2092.htm#REFRN30227
    >
    V$SESSION_LONGOPS
    This view displays the status of various operations that run for longer than 6 seconds (in absolute time). These operations currently include many backup and recovery functions, statistics gathering, and query execution, and more operations are added for every Oracle release.
    To monitor query execution progress, you must be using the cost-based optimizer and you must:
    •Set the TIMED_STATISTICS or SQL_TRACE parameter to true
    •Gather statistics for your objects with the ANALYZE statement or the DBMS_STATS package
    You can add information to this view about application-specific long-running operations by using the DBMS_APPLICATION_INFO.SET_SESSION_LONGOPS procedure.
    >
    If you provide more information about what commands are in your script, what the script does and what information you are trying to obtain maybe we can be of more help.

  • JDBC:Count Array. SQL query works but fails in JDBC

    Hi,
    I'm using the following SQL query
    "SELECT red.id, name, count(green.id) FROM red, green WHERE area = 'northeast' AND red.id = zid GROUP BY name"
    It executes perfectly from the DB command line giving me the results.
    However when I call it from within JDBC i am getting null values.
    My JDBC call ia as follows( Where lst is the above query)
    rs = st.executeQuery(lst);
    while(rs.next()){
    nidV.addElement(rs.getString("red.id"));
    nnmV.addElement(rs.getString("name"));
    nnumV.addElement(rs.getString("count(green.id)"));
    I do realize that many folks use Integer for count queries but I pull them with as Strings all the time so that's not it.
    The one diffrence between this and my other count queries is that this one returns an array (Vector) of counts.
    Any ideas?

    the problem is probably:
    SELECT red.id, name, count(green.id) ...
    nnumV.addElement(rs.getString("count(green.id)"));you can either use an alias for the column;
    SELECT red.id, name, count(green.id) COUNT_VAR
    nnumV.addElement(rs.getObject("COUNT_VAR"));or use getObject(3) as suggested by drclap.

  • How do I modify the WHERE clause in my SQL query?

    This seems like such a straight-forward part of the report design, but I'm fairly new to Crystal Reports and I only have experience with modifying reports someone else has already written.  In this particular case, I just need to modify the WHERE clause of the SQL query.  I can select Show SQL Query and see what statement is being used to select data, but I can't find where to modify it since it's grayed out.  I see how to change the selection criteria, parameters, grouping, etc...just not the WHERE clause.  The report is linked to a database used for reporting with a table created and populated by a stored procedure.  I don't need to modify the stored procedure because the data I want to filter by is currently in the table--I just don't know how to filter by what I need.  Here's part of the query:
    SELECT "rpt_dist"."startdate", "rpt_dist"."transtype", "rpt_dist"."laborcode", "rpt_dist"."crewid", "rpt_dist"."regularhrs" FROM   "Reporting"."dbo"."rpt_dist" "rpt_dist"
    WHERE  (rpt_dist."transtype" <> 'WORK' AND rpt_dist."transtype" <> 'WMATL') AND rpt_dist."laborcode" LIKE 'S%' AND (rpt_dist."crewid" = 'HOUS' OR rpt_dist."crewid" = 'HOUS2' ...
    I would like to add another crewid to the WHERE clause.  Thanks for any input.

    1.Open the report in the crystal designer
    2.Go to the field explorer(if hidden go to view menu->field explorer)
    3.Rt. click on the database fields->choose database expert
    4.Now you will see 2 columns-Available DataSource  and Selected Tables
    5.Rt. click on the object(ex.command) available in the Selected Tables column->Choose Edit command
    6.A new Modify Command window will appear,here you can edit your SQL Query
    I get to step 4 and I see the two columns including my database and the report table, but there is no command object available.  If I right-click on my table, I can just view the Properties. ??
    As for the other tip to modify the record selection:  I don't see anywhere the other crewid values are set and if I add the one I'm missing, it doesn't modify the existing SQL Query and when I preview the report it throws off the results so that no data displays??
    I'm using Crystal Reports 11.5 if that makes a difference.  Thanks again.

  • MultiProvider: How to decipher the underlying SQL query

    Hello Guys,
    I have joined 2 cubes using MultiProvider. I created a query which takes Key Figure values from both cubes. When I go to "Display SQl Query", I see 2 separare queries for each cube separately. I don't see a join between them.
    If I have to write the same query directly in ABAP, then how should I go about it?
    Also any idea what does the statement below means..I see this in every MultiProvider query
    STAR_TRANSFORMATION FACT( &TABLE& ) &SUBSTITUTE LITERALS&

    Hello:
    - You could never write a BI query in ABAP - that is not a real possibility, don't bother trying this, it's not reasonable (sorry).
    - MultiProvider is a Union operation, not a Join.  Queries against to a MultiProvider are split into sub-queries, where joins are performed on the tables of a cube, then the subqueries are merged in a Union operation.
    - You can see more information about the SQL in RSRT (including a MultiProvider explain)
    - STAR_TRANSFORMATION is a feature of the Oracle RDBMS, where the optimizer recognizes that the join operation against the tables in a star schema (InfoCube) can be more efficiently performed using a special technique, as opposed to ordinary join operations (like you might see in an OLTP system).  The optimizer automatically uses STAR_TRANSFORMATION to ensure that efficient joins are executed.
    Thanks for any points you choose to assign (the way of saying thanks in SDN).
    Best Regards -
    Ron Silberstein
    SAP

  • I am not able to see the trace files for SQL query

    Hello, I am using windows vista OS.
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
    PL/SQL Release 10.2.0.3.0 - Production
    CORE 10.2.0.3.0 Production
    TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production
    SQL>
    I enabled the trace as below
    alter system set timed_statistics=TRUE
    alter system set sql_trace=TRUE
    After this, I ran sql query from scott as below.
    select count(*) from emp;
    After this, i checked in user dump directory. The trace files are generating... But this sql code is not appearing in the trace files...
    Am i missing anything... Any help is appreciated.
    Thanks

    This may happen when the trace file is not completely closed.You may try again doing like this,
    alter session set sql_trace=true;
    select * from emp;
    alter session set sql_trace=false;After this,disconnect and exit from your session as well. This should close the file completely and you should be able to see your command in it.
    HTH
    Aman....

  • Sql query to get distinct count

    Hi
    I use SQL Server Management Studio
    can I have a sql query to get count as shown below against  each month column and name column to get distinct count.
    for example if there is two rows with the same date period and same name then the count should be one in first row and zero in the next row of the same data.
    Table Name: Table1
    Column: Month, Name
    Month
    Name
    Count
    12/1/2012 0:00
    AK
    1
    12/1/2012 0:00
    AK
    0
    12/1/2012 0:00
    AB
    1
    1/1/2013 0:00
    AK
    1
    1/1/2013 0:00
    AK
    0
    1/1/2013 0:00
    AB
    1
    3/1/2013 0:00
    AA
    1
    3/1/2013 0:00
    AK
    1
    3/1/2013 0:00
    AK
    0
    6/1/2013 0:00
    AA
    1
    6/1/2013 0:00
    AK
    1
    6/1/2013 0:00
    AK
    0
    9/1/2013 0:00
    AA
    1
    9/1/2013 0:00
    AK
    1
    9/13/2013 0:00
    AK
    1
    10/1/2013 0:00
    AA
    1
    10/1/2013 0:00
    AK
    1
    10/1/2013 0:00
    AK
    0

    Hi,
    Thanks for the query but this query gives the total count like shown below
    if see the second row in the below table AK for 2012-12-1 gives total count as 2 but need the query to show the first row as 1 and there after 0
    query result
    Month name cnt
    2012-12-01 00:00:00.000 AB 1
    2012-12-01 00:00:00.000 AK 2
    2012-12-01 00:00:00.000 AK 2
    2013-01-01 00:00:00.000 AB 1
    2013-01-01 00:00:00.000 AK 2
    2013-01-01 00:00:00.000 AK 2
    2013-03-01 00:00:00.000 AA 1
    2013-03-01 00:00:00.000 AK 2
    2013-03-01 00:00:00.000 AK 2
    2013-06-01 00:00:00.000 AA 1
    2013-06-01 00:00:00.000 AK 2
    2013-06-01 00:00:00.000 AK 2
    2013-09-01 00:00:00.000 AA 1
    2013-09-01 00:00:00.000 AK 1
    2013-09-13 00:00:00.000 AK 1
    2013-10-01 00:00:00.000 AA 1
    2013-10-01 00:00:00.000 AK 2
    2013-10-01 00:00:00.000 AK 2

  • Showing column value as counter in sql query - report

    I created a classic report with search bar based on an sql query. I would like to show the "notes" column in this query using some sort of counter and as a hyperlink to another section on the page (same idea as footnotes in a book). So if I have 10 rows and 5 have "notes". I should show 1,2,3,4,5 in the notes column for those rows and the number gets displayed as a link to another section listing the notes 1 through 5.
    I was thinking of creating a hidden page item as the counter with value of 0 and then in my query doing counter+1 but i'm not sure how to do this or if i can do that...
    If anyone can help or have any other ideas I would really appreciate it!!
    Thanks,
    Hindy

    Well, I'm doing this in VB and the subquery is dynamic. I'm passing an ADO recordset to a routine that sets up a listview to display the results. The number of columns selected can vary. My idea was to size the columns appropriately based on the size of the data. Within the display routine I could:
    sql = "SELECT "
    for i = 0 to oRS.Fields.Count - 1
    sql = sql & "MAX(LENGTH(" & i & ")), "
    next 'i
    sql = sql & "FROM (" & oRS.Source & ")"

  • See sql query from crystal report without crystal report

    see sql query from crystal report without crystal report 

    Hi,
    Depends on datasource type but you could have a look at ODBC trace or if you have access to the SQL Server you could use profiler to monitor the session.
    Regards,
    Craig
    And this will only be of use if you know which Server/Insstance/Database the Report is connecting to...
    Please click "Mark As Answer" if my post helped. Tony C.

  • Can users see the query plan of a SQL query in Oracle?

    Hi,
    I wonder for a given sql query, after the system optimization, can I see the query plan in oracle? If yes, how to do that? thank you.
    Xing

    You can use explain plan in SQLPlus
    SQL>  explain plan for select * from user_tables;
    Explained.
    Elapsed: 00:00:01.63
    SQL> select * from table(dbms_xplan.display());
    PLAN_TABLE_OUTPUT
    Plan hash value: 806004009
    | Id  | Operation                       | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT                |          |  2014 |  1123K|   507   (6)| 00:00:07 |
    |*  1 |  HASH JOIN RIGHT OUTER          |          |  2014 |  1123K|   507   (6)| 00:00:07 |
    |   2 |   TABLE ACCESS FULL             | SEG$     |  4809 |   206K|    34   (3)| 00:00:01 |
    |*  3 |   HASH JOIN RIGHT OUTER         |          |  1697 |   873K|   472   (6)| 00:00:06 |
    |   4 |    TABLE ACCESS FULL            | USER$    |    74 |  1036 |     3   (0)| 00:00:01 |
    |*  5 |    HASH JOIN OUTER              |          |  1697 |   850K|   468   (6)| 00:00:06 |
    |   6 |     NESTED LOOPS OUTER          |          |  1697 |   836K|   315   (6)| 00:00:04 |
    |*  7 |      HASH JOIN                  |          |  1697 |   787K|   226   (8)| 00:00:03 |
    |   8 |       TABLE ACCESS FULL         | TS$      |    13 |   221 |     5   (0)| 00:00:01 |
    |   9 |       NESTED LOOPS              |          |  1697 |   759K|   221   (8)| 00:00:03 |
    |  10 |        MERGE JOIN CARTESIAN     |          |  1697 |   599K|   162  (10)| 00:00:02 |
    |* 11 |         HASH JOIN               |          |     1 |   326 |     1 (100)| 00:00:01 |
    |* 12 |          FIXED TABLE FULL       | X$KSPPI  |     1 |    55 |     0   (0)| 00:00:01 |
    |  13 |          FIXED TABLE FULL       | X$KSPPCV |   100 | 27100 |     0   (0)| 00:00:01 |
    |  14 |         BUFFER SORT             |          |  1697 | 61092 |   162  (10)| 00:00:02 |
    |* 15 |          TABLE ACCESS FULL      | OBJ$     |  1697 | 61092 |   161  (10)| 00:00:02 |
    |* 16 |        TABLE ACCESS CLUSTER     | TAB$     |     1 |    96 |     1   (0)| 00:00:01 |
    |* 17 |         INDEX UNIQUE SCAN       | I_OBJ#   |     1 |       |     0   (0)| 00:00:01 |
    |  18 |      TABLE ACCESS BY INDEX ROWID| OBJ$     |     1 |    30 |     1   (0)| 00:00:01 |
    |* 19 |       INDEX UNIQUE SCAN         | I_OBJ1   |     1 |       |     0   (0)| 00:00:01 |
    |  20 |     TABLE ACCESS FULL           | OBJ$     | 52728 |   411K|   151   (4)| 00:00:02 |
    Predicate Information (identified by operation id):
       1 - access("T"."FILE#"="S"."FILE#"(+) AND "T"."BLOCK#"="S"."BLOCK#"(+) AND
                  "T"."TS#"="S"."TS#"(+))
       3 - access("CX"."OWNER#"="CU"."USER#"(+))
       5 - access("T"."DATAOBJ#"="CX"."OBJ#"(+))
       7 - access("T"."TS#"="TS"."TS#")
      11 - access("KSPPI"."INDX"="KSPPCV"."INDX")
      12 - filter("KSPPI"."KSPPINM"='_dml_monitoring_enabled')
      15 - filter("O"."OWNER#"=USERENV('SCHEMAID') AND BITAND("O"."FLAGS",128)=0)
      16 - filter(BITAND("T"."PROPERTY",1)=0)
      17 - access("O"."OBJ#"="T"."OBJ#")
      19 - access("T"."BOBJ#"="CO"."OBJ#"(+))
    42 rows selected.
    Elapsed: 00:00:03.61
    SQL> If your plan table does not exist, execute the script $ORACLE_HOME/RDBMS/ADMIN/utlxplan.sql to create the table.

  • SQL query to count number of VMs

    In OVM 2.2 I could run a SQL query against the OVM database to count the number of VMs that I had created, for automated reporting purposes.
    I've looked at the 3.1 OVM database and it looks like most of the data is kept in BLOB columns. So does anyone know how I might query the 3.1 database to count the number of VMs?
    Thanks.

    Hi,
    Combine the relevant data from the three tables (using UNION ALL) in a sub-query. without the GROUP BY.
    Your main query can select from the sub-query's result set, as if it were a table. Do the GROUP BY in the main query.
    That is:
    WITH     u     AS
         SELECT     TO_CHAR (emp_start, 'YYYY') AS yr     FROM     Adminstaff
        UNION ALL
         SELECT     TO_CHAR (emp_start, 'YYYY') AS yr     FROM     Clerk
        UNION ALL
         SELECT     TO_CHAR (emp_start, 'YYYY') AS yr     FROM     ITstaff
    SELECT     COUNT (*)      AS cnt
    ,     yr
    FROM     u
    GROUP BY     yr
    ORDER BY     cnt     DESC
    ;Since empno is the primary key, it's easier not to mention empno at all in the sub-query, and use "COUNT (*)" instead of "COUNT (empno)" in the main query. The results are the same.
    It might be a little more efficient to GROUP BY in all three branches of the sub-query, and then to SUM the results in another GROUP BY in the main query. That means more code, of course, which I'll leave as an exercise for you.
    Why do you have three separate tables? I can understand that nobody else wants to be near the IT Staff, but it seems like lots of things (including this job) would be a lot simpler if all employees were all in one table, with a column to designate to which group (Adminstaff, Clerk or ITStaff) each one belongs.

  • SQL Query - SCOM2012R2 Performance Counter

    Need some assistance with a SQL query against my SCOM DW. I want this to do a select within a select. The inner select must collect the raw data for a performance counter, for the last 5 minutes. But SCOM will only give me counters that collected in the
    last 5 minutes. What i want is the last value for every managed instance. - 1500 servers. 
    The outer SELECT must then filter this by doing a count on how many servers it collected a value for that are above 80% - and then divide that number by the total managed servers to give me a percentage of servers above 80% memory used. Eg - it counts 10
    servers are above 80%, but theres 100 servers - then gives me a final figure of 90%. 
    select (count(abc.DaValue)) from (
      select
        vManagedEntity.Path
        ,vPerformanceRule.ObjectName
        ,vPerformanceRule.CounterName
        ,AVG(vPerfRaw.SampleValue) AS DaValue
      from vPerfRaw
        join vPerformanceRuleInstance on vPerformanceRuleInstance.PerformanceRuleInstanceRowid = vPerfRaw.PerformanceRuleInstanceRowid
        join vPerformanceRule on vPerformanceRule.RuleRowId = vPerformanceRuleInstance.RuleRowId
        join vManagedEntity on vManagedEntity.ManagedEntityRowid = vPerfRaw.ManagedEntityRowId
      where vPerfRaw.Datetime > DATEADD(minute, -20, GETUTCDATE())
      and vPerformanceRule.ObjectName = 'Memory'
      and vPerformanceRule.CounterName = 'PercentMemoryUsed'
      GROUP BY Path, ObjectName, CounterName, Name
      Order by DaValue DESC) 
      as abc
    where abc.DaValue >= 80
    This statement gives me the result
    Msg 1033, Level 15, State 1, Line 15
    The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
    Any ideas ?

    Latest update - 
    SELECT count(DISTINCT abc.server) as NoOfServers,
    count(case when abc.DaValue >= 90 then abc.DaValue end) 'MemoryAbove90%'
    FROM( select TOP 100 PERCENT 
    vManagedEntity.Path as Server ,vPerformanceRule.ObjectName 
    ,vPerformanceRule.CounterName ,AVG(vPerfRaw.SampleValue) AS DaValue
    from Perf.vPerfRaw join vPerformanceRuleInstance on vPerformanceRuleInstance.PerformanceRuleInstanceRowid = vPerfRaw.PerformanceRuleInstanceRowid
    join vPerformanceRule on vPerformanceRule.RuleRowId = vPerformanceRuleInstance.RuleRowId
    join vManagedEntity on vManagedEntity.ManagedEntityRowid = vPerfRaw.ManagedEntityRowId
    where vPerfRaw.Datetime > DATEADD(minute, -10, GETUTCDATE())
    and vPerformanceRule.ObjectName = 'Memory' and vPerformanceRule.CounterName = 'PercentMemoryUsed'
    GROUP BY Path, ObjectName, CounterName, Name
    Order by DaValue DESC)  as abc
    This gives me Total Servers collected from, and Total Servers found to have AVG collections over 90% for Memory Util. 
    My KPI states that I must only display the number of servers that are above 90%, for more than 10minutes.
    The problem with this is that when i run it over a 10 minute period, I get collections for about 40% of my managed servers only.
    I want to know if anyone has a better way of collecting the perf data over a 10 minute period ? 
    What i was thinking of doing is  running this query with time interval on 11 minutes, populating a temp table with the data, running the query
    again with time interval on 1 minute ago, and comparing the results. IF it finds a match, I know that the server has been over 90% memory util for 10 minutes - then i can count it ? Its probably not entirely accurate, but Im out of ideas. From here, i want
    to export the Values for Total Servers and TotalServersAbove90% into Excel, and build a guage off it that will reflect a % of Servers within SLA -Memory Util. 
    Im not entirely familiar with SCOM collection intervals, IF I run the query for the last 1 minute - It seems to skip alot of servers and only returns
    data from about 50, is that because its not cycling on those at that moment ? In which case my idea above wont work.
    UPDATE 2 - i think this will do it
    Our OPS guy just told me that we sample every 10min. So immediately im thinking, perhaps i can run the query for the last 11 minutes - IF more than
    1 sample is above 90%, the server is above 90% for more than 10min.
    But, when i run this query 
    select TOP 100 Percent Path, AVG(SampleValue) AS Value, DateTime  from Perf.vPerfRaw pvpr  inner join vManagedEntity vme on pvpr.ManagedEntityRowId
    = vme.ManagedEntityRowId  inner join vPerformanceRuleInstance vpri on pvpr.PerformanceRuleInstanceRowId = vpri.PerformanceRuleInstanceRowId  inner join vPerformanceRule vpr on vpr.RuleRowId = vpri.RuleRowId WHERE ObjectName = 'Memory' AND CounterName
    = 'PercentMemoryUSed' AND Datetime > DATEADD(minute, -1, GETUTCDATE()) AND SampleValue >= 90 GROUP BY Path, SampleValue, DateTime ORDER BY DateTime DESC
    I get a few servers, but some have more than 1 entry in the last 1 minute.  Which means that i cant compare 2 samples.
    Perhaps i could say if ALL samples collected per server in that 11 minute period are > 90, 
    then add 1 to MemoryAbove90% 

  • List the count of each schema objects.. schema wise sql query needed

    Hi Friends,
    i need a sql query which has to list the schema name along with the count of schema objects like tables,views,triggers.... order by schemaname
    Regards,
    DB

    Hi
    You can try this option if you use 11g .
    Get all the object types in your db.
    SELECT DISTINCT object_type
                 FROM dba_objects;Then include all the object types in to the below query.
    select *
      from (select owner, object_type, 1 CNT
              from dba_objects ) e
            pivot( sum(CNT) for object_type in
              ( 'INDEX','TYPE','VIEW','LIBRARY','TRIGGER','DIRECTORY','PACKAGE','QUEUE','PACKAGE BODY','TABLE PARTITION','PROCEDURE',
                'WINDOW','CLUSTER','LOB','FUNCTION','CONSUMER GROUP','CONTEXT','RULE','XML SCHEMA','SEQUENCE','INDEX PARTITION','OPERATOR',
                'EVALUATION CONTEXT','SCHEDULE','JOB','SCHEDULER GROUP','LOB PARTITION','JOB CLASS','INDEXTYPE','TABLE','TYPE BODY','RESOURCE PLAN',
                'TABLE SUBPARTITION','UNDEFINED','DESTINATION','SYNONYM','EDITION','PROGRAM','RULE SET' ) )       
    order by owner;Cheers
    Kanchana

  • How to write sql query for counting pairs from below table??

    Below is my SQL table structure.
    user_id | Name | join_side | left_leg | right_leg | Parent_id
    100001 Tinku Left 100002 100003 0
    100002 Harish Left 100004 100005 100001
    100003 Gorav Right 100006 100007 100001
    100004 Prince Left 100008 NULL 100002
    100005 Ajay Right NULL NULL 100002
    100006 Simran Left NULL NULL 100003
    100007 Raman Right NULL NULL 100003
    100008 Vijay Left NULL NULL 100004
    It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
    I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains  both left_leg and right_leg id, then it is called pair.)
    I know there are three pairs under id "100001" :-
    1.  100002 and 100003
    2.  100004 and 100005
    3.  100006 and 100007
        100008 will not be counted as pair because it does not have right leg..
     But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
    Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005

    Sounds like this to me
    DECLARE @ID int
    SET @ID = 100001--your passed value
    SELECT left_leg,right_leg
    FROM table
    WHERE (user_id = @ID
    OR parent_id = @ID)
    AND left_leg IS NOT NULL
    AND right_leg IS NOT NULL
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Maybe you are looking for