Results 1 to 34 of 34

Thread: [2005] Sql statement Q

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    [2005] Sql statement Q

    How do i change this statement that filters what we show on our datagrid?

    Code:
     commEmployee.CommandText = "SELECT EmployeeID 'Employee ID', EmployeeFName 'First Name', EmployeeLName 'Last Name' FROM EmployeeProfile;"
    what we want to do is,

    SELECT only the EMPLOYEES that does not have a similar EmployeeID on another table..

    ex.
    Employee table has an EmployeeID field
    Attendees table has an employeeID field
    what we want to select are the Employees that do not have a similar employeeID on the attendees table? is this possible on an SQL statement?

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    Select EmployeeID As [Employee ID], EmployeeFName As [First Name], EmployeeLName As [Last Name] FROM EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SecondTable)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] Sql statement Q

    Try
    Code:
    SELECT employeeid FROM employee emp
    WHERE employeeid = '123456' 
    AND emp.employeeid NOT IN (SELECT employeeid FROM attendees)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    Code:
    "SELECT EmployeeID 'Employee ID', EmployeeFName 'First Name', EmployeeLName 'Last Name' FROM EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SeminarActivites);"
    ok this is my statement
    when i ran the program the datagrid didnt show anything

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    Hi Hack what is the emp for? sorry not really good thanks

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    this is my code that shows whats to be seen in the datagrid

    bt cant get it to work

    Code:
      Private Sub GetEmployeeRecords()
    
            'Construct SELECT Command
            commEmployee = New SqlCommand
            Commemployee.Connection = connseminars
            Commemployee.CommandType = CommandType.Text
            'commEmployee.CommandText = "SELECT * FROM EmployeeProfile;"
            commEmployee.CommandText = "SELECT EmployeeID 'Employee ID', EmployeeFName 'First Name', EmployeeLName 'Last Name' FROM EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SeminarActivites);"
    
            'Assign Commands to DataAdapter
            daEmployee = New SqlDataAdapter
            daEmployee.SelectCommand = Commemployee
    
            'Fill the Dataset
            dtsEmployee = New DataSet
            daEmployee.Fill(dtsEmployee, "EmployeeProfile")
            dtEmployee = New DataTable
            daEmployee.Fill(dtEmployee)
            Me.EmployeeListDataGridView.DataSource = dtEmployee
    
            connseminars.Close()
    
            dtviewattendees = dtEmployee.Clone()
    
        End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    Sir Gary can you help me identify why the grid is not showing anything with the new statement? thnx

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    Is there an error or just no data? I have a problem with the SQL statement if alaising column names I would normally us the As Keyword an place the alias in square brackest (Select EmployeeID As [Employee ID],FirstName As {First Name].....
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    theres an error when i start the form

    Code:
    daEmployee.Fill(dtsEmployee, "EmployeeProfile")
    invalid object name 'SeminarActivities' is the error tnx

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] Sql statement Q

    Quote Originally Posted by aerialz666
    Hi Hack what is the emp for? sorry not really good thanks
    It is an alias for the name of the employee table.

    There is a very good tutorial on SQL in our FAQ section. It would be worth checking out.

  11. #11
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    That is the table name in the sub select statement for the Where clause. So what is the actual table name that you want to query there?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    yes im also currenty reading sql faqs but cant get this issue thank you

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    The actual table name is really SeminarActivities, can you check the statement i wrote above? mb something i did was wrong

  14. #14
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    What is the Database?

    Code:
    SELECT EmployeeID AS [Employee ID], EmployeeFName As [First Name], EmployeeLName As [Last Name] FROM EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SeminarActivites)"
    Can this be run in the database system query area on its own? Do it return errors there?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    database is MS SQL 2005

    this is the statement i ran on the query of the db

    Code:
    SELECT EmployeeID 'Employee ID', EmployeeFName 'First Name', EmployeeLName 'Last Name' FROM EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SeminarActivites)
    i got this error there

    Msg 208, Level 16, State 1, Line 1
    Invalid object name 'EmployeeProfile'.

    thanks

  16. #16
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    Look at the statement I posted, notice any difference? there are some. Try my statement and let me know the results then.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    invalid object name 'SeminarActivities' sir is the error i still get (

  18. #18
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    Please post the exact SQL statement the is being run. The error that is being told to us means that the table SeminarActivities does not exsist or you do not have access rights to it.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    sir i entered the whole code to view the employees in the grid, and i insert the statement there i tried making the statement more simple and this is it..

    Code:
    commEmployee.CommandText = "SELECT * FROM EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SeminarActivites);"

  20. #20
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    No set a variable named strSQL as a string. Set the commEmployee.CommandText to that varable and Post the strSQL contents here and also the error number and Description.

    Code:
    Dim oCmd As System.Data.SqlClient.SqlCommand
    Dim strSQL As String = "Select * From EmployeeProfile Where EmployeeID Not In (Select Distinct(EmployeeID) From SeminarActivites)"
    
    Try
      Dim oDA As System.Data.SqlClient.SqlDataAdapter
      Dim oDS As New DataSet
      oCmd.CommandType = CommandType.Text
      oCmd.CommandText = strSQL
      oDA = New System.Data.SqlClient.SqlDataAdapter(oCmd)
    
      oDA.Fill(oDS)
      oDA.Dispose()  
    Catch ex As Exeception
       MessageBox.Show(ex)
    End Try
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    sir i get this invalid object name 'SeminarActivities'
    Attached Images Attached Images  

  22. #22
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    Once more it is telling you there is a problem with the table name SeminarActvities. Did you do what I showed and post the actuall SQL generalted text?


    As this query shows the idea works.

    Code:
    strSQL = "Select PersonnelPK,LastName,FirstName,MiddleName From Personnel Where PersonnelPK Not In ("
            strSQL &= "Select SubordinatePK From WorksFor Where SupervisorPK = " & intSuper.ToString() & ") And PersonnelPK <> " & intSuper.ToString()
            strSQL &= "And PersonnelPK > 1 Order By LastName,FirstName,MiddleName"
    I use similar statement in many places in a database application. We are still at the point of the Sub Query table not being recognised. That is an issue either with the table name or permission on the table
    Last edited by GaryMazzone; Aug 28th, 2007 at 12:07 PM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    Yes sir i made a new form and put those on the load of the form and that was what popped out.. what i do nxt? thank youu

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    sir im confused ive read some articles and also the code hack posted..
    that i should do somthing like this but cant convert it to the sql statement i want..

    here is what i want to accomplish..

    Code:
    SELECT * FROM EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SeminarActivites
    and

    here is what im using as guide .

    Code:
    SELECT Ord.OrderID, Ord.OrderDate, 
    MAX(OrdDet.UnitPrice) AS maxUnitPrice
    FROM Northwind.dbo.[Order Details] AS OrdDet 
    INNER JOIN 
    Northwind.dbo.Orders AS Ord
    ON Ord.OrderID = OrdDet.OrderID
    GROUP BY Ord.OrderID, Ord.OrderDate
    but i cant get my sql statement to work tnxxx

  25. #25
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    Why would you be using an INNER JOIN statement as an example? That example returns results from the Orders table And the OrdersDetail table where there is a match on Orders.OrderID and OrderDetails.OrderID. That is nothing like what you are trying to do.


    All right let's start from the begining.
    1) What is the database (I believe that you saild it is SQL Server)?
    2) What are all table names involved?
    3) What is the structure of all table involved? (Fields and FieldType Specs).

    Post the code that built the tables.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    hi

    1. yes
    2. EmployeeProfile -> EmployeeID is only important 1
    Seminars -> EmployeeID and SeminarID both are FK

    im trying to copy the way he called 2 different tables in 1 SQL statement basically thanks

  27. #27
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    "Select * From EmployeeProfile Where EmployeeID NOT IN (Select EmployeeID From Seminars Where SeminarID = " & intSeminarID.ToString() & ")"

    Now where does this SeminarID come from? Where is the SemiarActivities table you kept refering to?
    Last edited by GaryMazzone; Aug 28th, 2007 at 01:19 PM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    hi sir thanks for the help i have achieved the initial thing i want.. by having this SQLstatement

    Code:
    SELECT Emp.EmployeeID, Emp.EmployeeFName, Emp.EmployeeLName FROM FinalDB.dbo.[EmployeeProfile] AS Emp where Emp.EmployeeID NOT IN (Select Sem.EmployeeID From FinalDB.dbo.[SeminarActivities] as Sem
    what i dont get now is how to add this to the sub selectstatement..

    if SeminarID IN seminar activities is = textbox

    it should be something like

    Code:
    (Select Sem.EmployeeID From FinalDB.dbo.[SeminarActivities] as Sem where SeminarID <> '" & txtSeminarID.Text & "' );"
    for my selectstatement .. thanks

  29. #29
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    Again I'm counfused. What is SeminarActivities? How does this relate to the other two tables? What is in the textbox that is SeminarIDs, a bunch of number? a bunch of names? Is this a query with a subquery as the where clause with an other sub query as the Where clause of the first?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    its a table containg FK of EmployeeID and SeminarID basically the next step and last step i need to do is

    add this statement...

    Code:
    if SeminarID IN seminar activities is <> txtSeminarID.text
    basically what i want to accomplish is that it will only read the rows that have a seminarID not equal to the one in the SeminarID.text

    to...

    Code:
    SELECT Emp.EmployeeID, Emp.EmployeeFName, Emp.EmployeeLName FROM FinalDB.dbo.[EmployeeProfile] AS Emp where Emp.EmployeeID NOT IN (Select Sem.EmployeeID From FinalDB.dbo.[SeminarActivities] as Sem

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    ok rephrased..

    what is the sql statement for tis

    select EmployeeID from SeminarActivities where SeminarID <> SeminarID.text

  32. #32
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    "Select EmployeeID,EmployeeName From EmployeeProfile Where EmployeeID Not IN (Select EmployeeID From SeminarActivities Where SeminarID Not In (" & txtSeminarID.text & "))"

    Now this assumes that you have listed a bunch of number in the txtSeminarID textbox sepperated by commas.

    This return a list of Employees how have not taken the seminars listed.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  33. #33
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Sql statement Q

    If you want a list of Employees who have not taken a specified Seminar (only on here).

    "Select EmployeeID,EmployeeName From EmployeeProfile Where EmployeeID NOT IN (Select EmployeeID From SeminarActivities Where SeminalID <> " & txtSeminarID.Text.ToString() & ")"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  34. #34

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] Sql statement Q

    thanks man figured it out got another problem

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