conditional table selection

  • how to select particular table after condition is true?

    i want to select table only if it has rows more than two else i want to print less number of rows ?

    how can i solve this problem ?

    Thank you

  • you could try this

    IF (SELECT count(column1,) from dbo.table) >2

    BEGIN

    SELECT column1,column2

    FROM dbo.table

    END

    ELSE

    BEGIN

    PRINT 'not enough rows'

    END

  • That's an easy one: get the rowcount of the table and use this in a IF statement

    -- change the table name (currently: #temp) on two places in the code below

    if (select count(*) from #temp) > 2

    select * from #temp

    else

    select 'not enough rows'

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Try this..

    SelectDISTINCT Object_Name(Object_Id) AS TABLE_NAME

    , case when Row_Count <= 2 then 'NOT ENOUGH ROWS' ELSE CAST(row_count AS VARCHAR) END NUM_ROWS

    From sys.dm_db_partition_stats

    order by Object_Name(Object_Id)

    -RP
  • Bajrang (7/11/2013)


    Try this..

    SelectDISTINCT Object_Name(Object_Id) AS TABLE_NAME

    , case when Row_Count <= 2 then 'NOT ENOUGH ROWS' ELSE CAST(row_count AS VARCHAR) END NUM_ROWS

    From sys.dm_db_partition_stats

    order by Object_Name(Object_Id)

    That does not do what the OP asked for. This will only return a single row that indicates if there is more than 1 row. This does not return the actual data from the table if there is more than 1 row.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • problem have not solved yet

    i tried this

    1)

    IF (SELECT COUNT(*) FROM Table_name) > 2

    BEGIN

    SELECT * FROM Table_name

    END

    ELSE

    BEGIN

    Print 'Min Row Cond not valid'

    END

    then i got Error Code : 1064

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF (SELECT COUNT(*) FROM t4) > 2

    BEgin

    SELECT * from t4

    END

    ELSE

    BEGIn' at line 1

    2)

    SelectDISTINCT Object_Name(Object_Id) AS TABLE_NAME

    , case when Row_Count =0 then 'NOT ENOUGH ROWS' ELSE CAST(row_count AS VARCHAR) END NUM_ROWS

    From sys.dm_db_partition_stats

    order by Object_Name(Object_Id)

    when tried this i am getting no table for more than one value but

    i want only msg that "not enough rows" or"zero rows" when zero rows in table

    kindly provide me solution

    i am using SQLyog for doing my work

    and thank you very much to all of u

  • This is a SQL Server forum. There may be someone here who knows MySQL, but you'd probably be better posting your question somewhere else.

    John

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply