CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2006
    Posts
    9

    How to output what is not in list

    Select * from employee where empnum in ('1','2','3','4')

    I don't have emp 3 and 4 in the table and so i want to output that.
    Basically, instead of outputing what is in the table i want to output what is in the list but not in the table . Is it possible ?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: How to output what is not in list

    Create a temporary table (or table variable depending on the flavor of SQL), then do a left join, looing for RHS = null.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Oct 2006
    Posts
    9

    Re: How to output what is not in list

    hmm..interesting. Not a sql expert. But, can this all be done in query analyzer statements? I am using toad.

    create a temp table like #temp
    Fill in temp with list items
    left or right join with the table i am querying

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to output what is not in list

    Yes it can be done in T-SQL too. After all a stored procedure or a function is also T-SQL. You will need to execute the whole block at once, and not line by line.

  5. #5
    Join Date
    Oct 2006
    Posts
    9

    Re: How to output what is not in list

    Something like this??
    CREATE TABLE #temp (empnum int )

    INSERT INTO #temp(empnum) values ('1','2','3','4')

    select * from #temp left outer join employee e on
    e.empnum=#temp.empnum

    drop table #temp

    But for this don't need to have table create permissions. If i have read only permissions, is there a way to do it?

  6. #6
    Join Date
    Sep 2006
    Posts
    95

    Re: How to output what is not in list

    Don't use a temp table, just declare a temporary table variable.

    Code:
    DECLARE @Temp TABLE { EmpNumber int }
    
    INSERT INTO @Temp VALUES (1)
    INSERT INTO @Temp VALUES (2)
    INSERT INTO @Temp VALUES (3)
    INSERT INTO @Temp VALUES (4)
    
    SELECT T.EmpNumber
    FROM @Temp
    LEFT JOIN Employee E ON E.EmpNum = T.EmpNumber
    WHERE E.EmpNum IS NULL
    On Error Kill(User)

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: How to output what is not in list

    What database are you using?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured