Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. a SQL Statement Error

a SQL Statement Error

Scheduled Pinned Locked Moved Database
asp-netdatabasehelpquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Majid Shahabfar

    .
    .
    .
    SELECT PD_Users.FirstName as FirstName, PD_Users.LastName as LastName, PD_Users.UserName as UserName, PD_Users.CityID as CityID,
    PD_Cities.CityName as CityName, aspnet_Membership.CreateDate as CreateDate,
    (SELECT COUNT(*) FROM PD_Articles WHERE (PD_Users.UserName = UserName)) AS ArticleCount,
    (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 1)) AS NewsCount,
    (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 0)) AS ITNewsCount,
    (SELECT COUNT(*) FROM PD_ForumMessage WHERE (PD_Users.UserName = UserName)) AS MessageCount,
    CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end AS IsOnline,
    ROW_NUMBER() OVER (ORDER BY aspnet_Membership.CreateDate ) AS RowNumber

    FROM	aspnet\_Users INNER JOIN aspnet\_Membership ON aspnet\_Users.UserId = aspnet\_Membership.UserId INNER JOIN
                      PD\_Users INNER JOIN PD\_Cities ON PD\_Users.CityID = PD\_Cities.CityId ON aspnet\_Users.UserName = PD\_Users.UserName
    WHERE (@LastName = PD\_Users.LastName  OR (@LastName = '' AND 1=1))    
          AND (@UserName = PD\_Users.UserName OR (@UserName = '' AND 1=1))
          AND (@CityID = PD\_Users.CityID OR (@CityID = -1 AND 1=1))
          AND (@UserStatus = IsOnline OR (@UserStatus = 0 AND 1=1))   
    

    .
    .
    .

    Error: Invalid name column 'IsOnline' (in Where statement) How could I solve it? thanks

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #2

    Obviously IsOnline is not a column in the table. Find out what the column name should be


    I know the language. I've read a book. - _Madmatt

    M 1 Reply Last reply
    0
    • M Majid Shahabfar

      .
      .
      .
      SELECT PD_Users.FirstName as FirstName, PD_Users.LastName as LastName, PD_Users.UserName as UserName, PD_Users.CityID as CityID,
      PD_Cities.CityName as CityName, aspnet_Membership.CreateDate as CreateDate,
      (SELECT COUNT(*) FROM PD_Articles WHERE (PD_Users.UserName = UserName)) AS ArticleCount,
      (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 1)) AS NewsCount,
      (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 0)) AS ITNewsCount,
      (SELECT COUNT(*) FROM PD_ForumMessage WHERE (PD_Users.UserName = UserName)) AS MessageCount,
      CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end AS IsOnline,
      ROW_NUMBER() OVER (ORDER BY aspnet_Membership.CreateDate ) AS RowNumber

      FROM	aspnet\_Users INNER JOIN aspnet\_Membership ON aspnet\_Users.UserId = aspnet\_Membership.UserId INNER JOIN
                        PD\_Users INNER JOIN PD\_Cities ON PD\_Users.CityID = PD\_Cities.CityId ON aspnet\_Users.UserName = PD\_Users.UserName
      WHERE (@LastName = PD\_Users.LastName  OR (@LastName = '' AND 1=1))    
            AND (@UserName = PD\_Users.UserName OR (@UserName = '' AND 1=1))
            AND (@CityID = PD\_Users.CityID OR (@CityID = -1 AND 1=1))
            AND (@UserStatus = IsOnline OR (@UserStatus = 0 AND 1=1))   
      

      .
      .
      .

      Error: Invalid name column 'IsOnline' (in Where statement) How could I solve it? thanks

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #3

      Is the UserStatus a string ? Then the IsOnline should be 'IsOnline' (in quotes). Or is IsOnline a SQL Server function that you have written ? Maybe it needs a parameter.

      M 1 Reply Last reply
      0
      • N Not Active

        Obviously IsOnline is not a column in the table. Find out what the column name should be


        I know the language. I've read a book. - _Madmatt

        M Offline
        M Offline
        Majid Shahabfar
        wrote on last edited by
        #4

        Yes I know and there is not the same column name in the table but I have to compare it with @UserStatus

        N 1 Reply Last reply
        0
        • D David Mujica

          Is the UserStatus a string ? Then the IsOnline should be 'IsOnline' (in quotes). Or is IsOnline a SQL Server function that you have written ? Maybe it needs a parameter.

          M Offline
          M Offline
          Majid Shahabfar
          wrote on last edited by
          #5

          @UserStatus is declared as bit and IsOnline is not a SQL function.

          1 Reply Last reply
          0
          • M Majid Shahabfar

            Yes I know and there is not the same column name in the table but I have to compare it with @UserStatus

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #6

            Try this[^]


            I know the language. I've read a book. - _Madmatt

            1 Reply Last reply
            0
            • M Majid Shahabfar

              .
              .
              .
              SELECT PD_Users.FirstName as FirstName, PD_Users.LastName as LastName, PD_Users.UserName as UserName, PD_Users.CityID as CityID,
              PD_Cities.CityName as CityName, aspnet_Membership.CreateDate as CreateDate,
              (SELECT COUNT(*) FROM PD_Articles WHERE (PD_Users.UserName = UserName)) AS ArticleCount,
              (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 1)) AS NewsCount,
              (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 0)) AS ITNewsCount,
              (SELECT COUNT(*) FROM PD_ForumMessage WHERE (PD_Users.UserName = UserName)) AS MessageCount,
              CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end AS IsOnline,
              ROW_NUMBER() OVER (ORDER BY aspnet_Membership.CreateDate ) AS RowNumber

              FROM	aspnet\_Users INNER JOIN aspnet\_Membership ON aspnet\_Users.UserId = aspnet\_Membership.UserId INNER JOIN
                                PD\_Users INNER JOIN PD\_Cities ON PD\_Users.CityID = PD\_Cities.CityId ON aspnet\_Users.UserName = PD\_Users.UserName
              WHERE (@LastName = PD\_Users.LastName  OR (@LastName = '' AND 1=1))    
                    AND (@UserName = PD\_Users.UserName OR (@UserName = '' AND 1=1))
                    AND (@CityID = PD\_Users.CityID OR (@CityID = -1 AND 1=1))
                    AND (@UserStatus = IsOnline OR (@UserStatus = 0 AND 1=1))   
              

              .
              .
              .

              Error: Invalid name column 'IsOnline' (in Where statement) How could I solve it? thanks

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              It's not legal to filter on a column that doesn't exist before the selection

              select 1 as bla
              where bla <> 1

              This will generate an exception. You'd need to change the condition in your where-clause. Alternatively, you could use a view without the filter, and filter that using a second query. I'm thinking that IsOnline would be a good candidate to become a computed column.

              I are Troll :suss:

              M 1 Reply Last reply
              0
              • L Lost User

                It's not legal to filter on a column that doesn't exist before the selection

                select 1 as bla
                where bla <> 1

                This will generate an exception. You'd need to change the condition in your where-clause. Alternatively, you could use a view without the filter, and filter that using a second query. I'm thinking that IsOnline would be a good candidate to become a computed column.

                I are Troll :suss:

                M Offline
                M Offline
                Majid Shahabfar
                wrote on last edited by
                #8

                this is the full SQL

                @PageNumber int,
                @PageSize int,
                @MinutesSinceLastInActive int,
                @CurrentTimeUtc datetime,
                @LastName nvarchar(64),
                @UserName nvarchar(256),
                @CityID int,
                @UserStatus bit

                AS
                BEGIN

                DECLARE @FirstRow INT,
                @LastRow INT,
                @TotalRecords INT,
                @DateActive datetime
                SELECT @DateActive = DATEADD(minute, -(@MinutesSinceLastInActive), @CurrentTimeUtc)

                SELECT @FirstRow = 1 + (@PageNumber * @PageSize),
                @LastRow = @PageSize + @FirstRow - 1 ;

                SELECT @TotalRecords = (SELECT COUNT(*) FROM PD_Users WHERE @LastName = PD_Users.LastName OR (@LastName = '' AND 1=1));

                WITH Members AS
                (
                SELECT PD_Users.FirstName as FirstName, PD_Users.LastName as LastName, PD_Users.UserName as UserName, PD_Users.CityID as CityID,
                PD_Cities.CityName as CityName, aspnet_Membership.CreateDate as CreateDate,
                (SELECT COUNT(*) FROM PD_Articles WHERE (PD_Users.UserName = UserName)) AS ArticleCount,
                (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 1)) AS NewsCount,
                (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 0)) AS ITNewsCount,
                (SELECT COUNT(*) FROM PD_ForumMessage WHERE (PD_Users.UserName = UserName)) AS MessageCount,
                CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end AS IsOnline,
                ROW_NUMBER() OVER (ORDER BY aspnet_Membership.CreateDate ) AS RowNumber

                FROM	aspnet\_Users INNER JOIN aspnet\_Membership ON aspnet\_Users.UserId = aspnet\_Membership.UserId INNER JOIN
                                  PD\_Users INNER JOIN PD\_Cities ON PD\_Users.CityID = PD\_Cities.CityId ON aspnet\_Users.UserName = PD\_Users.UserName
                WHERE (@LastName = PD\_Users.LastName  OR (@LastName = '' AND 1=1))    
                      AND (@UserName = PD\_Users.UserName OR (@UserName = '' AND 1=1))
                      AND (@CityID = PD\_Users.CityID OR (@CityID = -1 AND 1=1))
                      AND (@UserStatus = IsOnline OR (@UserStatus = 0 AND 1=1))               
                

                )

                SELECT RowNumber, FirstName, LastName, UserName, CityID, CityName, CreateDate,
                ArticleCount,NewsCount, ITNewsCount, MessageCount, IsOnline
                FROM Members
                WHERE RowNumber BETWEEN @FirstRow AND @LastRow AND (@UserStatus = IsOnline)
                ORDER BY RowNumber ASC

                RETURN @TotalRecords
                END

                because I use this stored procedure for paging so I need to use IsOnline in first Select statement.

                L 1 Reply Last reply
                0
                • M Majid Shahabfar

                  .
                  .
                  .
                  SELECT PD_Users.FirstName as FirstName, PD_Users.LastName as LastName, PD_Users.UserName as UserName, PD_Users.CityID as CityID,
                  PD_Cities.CityName as CityName, aspnet_Membership.CreateDate as CreateDate,
                  (SELECT COUNT(*) FROM PD_Articles WHERE (PD_Users.UserName = UserName)) AS ArticleCount,
                  (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 1)) AS NewsCount,
                  (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 0)) AS ITNewsCount,
                  (SELECT COUNT(*) FROM PD_ForumMessage WHERE (PD_Users.UserName = UserName)) AS MessageCount,
                  CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end AS IsOnline,
                  ROW_NUMBER() OVER (ORDER BY aspnet_Membership.CreateDate ) AS RowNumber

                  FROM	aspnet\_Users INNER JOIN aspnet\_Membership ON aspnet\_Users.UserId = aspnet\_Membership.UserId INNER JOIN
                                    PD\_Users INNER JOIN PD\_Cities ON PD\_Users.CityID = PD\_Cities.CityId ON aspnet\_Users.UserName = PD\_Users.UserName
                  WHERE (@LastName = PD\_Users.LastName  OR (@LastName = '' AND 1=1))    
                        AND (@UserName = PD\_Users.UserName OR (@UserName = '' AND 1=1))
                        AND (@CityID = PD\_Users.CityID OR (@CityID = -1 AND 1=1))
                        AND (@UserStatus = IsOnline OR (@UserStatus = 0 AND 1=1))   
                  

                  .
                  .
                  .

                  Error: Invalid name column 'IsOnline' (in Where statement) How could I solve it? thanks

                  N Offline
                  N Offline
                  Niladri_Biswas
                  wrote on last edited by
                  #9

                  As far as Sql Server is concern, this you cannot do but the same can be done in Oracle. What you do is like

                  AND (@UserStatus = (CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end) OR (@UserStatus = 0 AND 1=1))

                  Hope this helps :)

                  Niladri Biswas

                  M 1 Reply Last reply
                  0
                  • M Majid Shahabfar

                    this is the full SQL

                    @PageNumber int,
                    @PageSize int,
                    @MinutesSinceLastInActive int,
                    @CurrentTimeUtc datetime,
                    @LastName nvarchar(64),
                    @UserName nvarchar(256),
                    @CityID int,
                    @UserStatus bit

                    AS
                    BEGIN

                    DECLARE @FirstRow INT,
                    @LastRow INT,
                    @TotalRecords INT,
                    @DateActive datetime
                    SELECT @DateActive = DATEADD(minute, -(@MinutesSinceLastInActive), @CurrentTimeUtc)

                    SELECT @FirstRow = 1 + (@PageNumber * @PageSize),
                    @LastRow = @PageSize + @FirstRow - 1 ;

                    SELECT @TotalRecords = (SELECT COUNT(*) FROM PD_Users WHERE @LastName = PD_Users.LastName OR (@LastName = '' AND 1=1));

                    WITH Members AS
                    (
                    SELECT PD_Users.FirstName as FirstName, PD_Users.LastName as LastName, PD_Users.UserName as UserName, PD_Users.CityID as CityID,
                    PD_Cities.CityName as CityName, aspnet_Membership.CreateDate as CreateDate,
                    (SELECT COUNT(*) FROM PD_Articles WHERE (PD_Users.UserName = UserName)) AS ArticleCount,
                    (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 1)) AS NewsCount,
                    (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 0)) AS ITNewsCount,
                    (SELECT COUNT(*) FROM PD_ForumMessage WHERE (PD_Users.UserName = UserName)) AS MessageCount,
                    CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end AS IsOnline,
                    ROW_NUMBER() OVER (ORDER BY aspnet_Membership.CreateDate ) AS RowNumber

                    FROM	aspnet\_Users INNER JOIN aspnet\_Membership ON aspnet\_Users.UserId = aspnet\_Membership.UserId INNER JOIN
                                      PD\_Users INNER JOIN PD\_Cities ON PD\_Users.CityID = PD\_Cities.CityId ON aspnet\_Users.UserName = PD\_Users.UserName
                    WHERE (@LastName = PD\_Users.LastName  OR (@LastName = '' AND 1=1))    
                          AND (@UserName = PD\_Users.UserName OR (@UserName = '' AND 1=1))
                          AND (@CityID = PD\_Users.CityID OR (@CityID = -1 AND 1=1))
                          AND (@UserStatus = IsOnline OR (@UserStatus = 0 AND 1=1))               
                    

                    )

                    SELECT RowNumber, FirstName, LastName, UserName, CityID, CityName, CreateDate,
                    ArticleCount,NewsCount, ITNewsCount, MessageCount, IsOnline
                    FROM Members
                    WHERE RowNumber BETWEEN @FirstRow AND @LastRow AND (@UserStatus = IsOnline)
                    ORDER BY RowNumber ASC

                    RETURN @TotalRecords
                    END

                    because I use this stored procedure for paging so I need to use IsOnline in first Select statement.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    A bit dirty, but turning it into a subquery should work too;

                    SELECT @FirstRow = 1 + (@PageNumber * @PageSize),
                    @LastRow = @PageSize + @FirstRow - 1 ;

                    SELECT @TotalRecords = (SELECT COUNT(*) FROM PD_Users WHERE @LastName = PD_Users.LastName OR (@LastName = '' AND 1=1));

                    WITH Members AS
                    (
                    select * from
                    (
                    SELECT PD_Users.FirstName as FirstName, PD_Users.LastName as LastName, PD_Users.UserName as UserName, PD_Users.CityID as CityID,
                    PD_Cities.CityName as CityName, aspnet_Membership.CreateDate as CreateDate,
                    (SELECT COUNT(*) FROM PD_Articles WHERE (PD_Users.UserName = UserName)) AS ArticleCount,
                    (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 1)) AS NewsCount,
                    (SELECT COUNT(*) FROM PD_News WHERE (PD_Users.UserName = SenderName AND IsDevNews = 0)) AS ITNewsCount,
                    (SELECT COUNT(*) FROM PD_ForumMessage WHERE (PD_Users.UserName = UserName)) AS MessageCount,
                    CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end AS IsOnline,
                    ROW_NUMBER() OVER (ORDER BY aspnet_Membership.CreateDate ) AS RowNumber

                    FROM	aspnet\_Users INNER JOIN aspnet\_Membership ON aspnet\_Users.UserId = aspnet\_Membership.UserId INNER JOIN
                                      PD\_Users INNER JOIN PD\_Cities ON PD\_Users.CityID = PD\_Cities.CityId ON aspnet\_Users.UserName = PD\_Users.UserName
                    

                    ) as TempCTE
                    WHERE (@LastName = PD_Users.LastName OR (@LastName = '' AND 1=1))
                    AND (@UserName = PD_Users.UserName OR (@UserName = '' AND 1=1))
                    AND (@CityID = PD_Users.CityID OR (@CityID = -1 AND 1=1))
                    AND (@UserStatus = IsOnline OR (@UserStatus = 0 AND 1=1))
                    )

                    SELECT RowNumber, FirstName, LastName, UserName, CityID, CityName, CreateDate,
                    ArticleCount,NewsCount, ITNewsCount, MessageCount, IsOnline
                    FROM Members
                    WHERE RowNumber BETWEEN @FirstRow AND @LastRow AND (@UserStatus = IsOnline)
                    ORDER BY RowNumber ASC

                    I are Troll :suss:

                    1 Reply Last reply
                    0
                    • N Niladri_Biswas

                      As far as Sql Server is concern, this you cannot do but the same can be done in Oracle. What you do is like

                      AND (@UserStatus = (CASE WHEN aspnet_Users.LastActivityDate > @DateActive THEN cast(1 as bit) ELSE cast(0 as bit) end) OR (@UserStatus = 0 AND 1=1))

                      Hope this helps :)

                      Niladri Biswas

                      M Offline
                      M Offline
                      Majid Shahabfar
                      wrote on last edited by
                      #11

                      Thank you Niladri it helped

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups