use results of view in where statement

  • I have a sql server 2008 stored procedure where I want to use the results of a view in a where statement. I want to check to see if the results from the view is not null.

    The sql looks like the following currently:

    select top 5 from cust_table

    where view value is not null.

    Would you show me the t-sql that I would need to use to see if the results from the view is null?

  • wendy elizabeth (9/13/2013)


    I have a sql server 2008 stored procedure where I want to use the results of a view in a where statement. I want to check to see if the results from the view is not null.

    The sql looks like the following currently:

    select top 5 from cust_table

    where view value is not null.

    Would you show me the t-sql that I would need to use to see if the results from the view is null?

    A view is just like a table. Not sure what you mean by the value is not null. Do you mean if the view returns no rows?

    select top 5 {Columns}

    from cust_table

    where exists (select * from view)

    Or do you mean you need to join to the view and only return rows where a certain row is null???

    _______________________________________________________________

    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/

  • I mean when the view retruns data. I do not want null values returned.

  • Sean's syntax would allow you to check to see if the view returns anything. If you're looking to find out if specific columns are not null, you'd have to adjust the EXISTS syntax to look at those columns:

    select top 5 {Columns}

    from cust_table

    where exists (select * from view where TheColumnICareAbout is not null)

    Are the table and the view's records related somehow? I'm having a hard time envisioning what exactly you're hoping to do.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

Viewing 4 posts - 1 through 3 (of 3 total)

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