Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6937609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:23:09+00:00 2026-05-27T12:23:09+00:00

I am trying to adapt a stored procedure into a view and I’ve run

  • 0

I am trying to adapt a stored procedure into a view and I’ve run into what appears to be a table scoping issue in SQL Server 2008 R2.

In the sproc, there was a parameter that specified the companyId. Now we want to retrieve the same data but not limit it to a specific companyId but rather to group by that companyId.

However, my attempt broadening it has failed.

Select Distinct [T].[TruckID], [T].[VIN], [T].[Make], [T].[Model], 
    [T].[ModelYear], [T].[RFIDNo], [SQCT].[VendorCode], [SQCT].[UserLabel], 
    [T].[IsActive], [T].[IsFleetTruck], [SQDA].[DriverAssociations], 
    dbo.GetCurrentPlate([T].[TruckID]) [Plate], 
    [TCT].[CompanyId] -- // <- Now we're including it

From [dbo].[Truck] as [T]
    Inner Join [dbo].[TruckerCompanyTruck] as [TCT] 
        On [T].[TruckID] = [TCT].[TruckId]
    Left Outer Join (
        Select [CT].[CompanyTruckId], [CT].[CompanyId], 
            [CT].[TruckId], [CT].[VendorCode], [CT].[UserLabel], 
            [CT].[CreateDate], [CT].[CreateBy], [CT].[UpdateDate], 
            [CT].[UpdateBy], [CT].[Version]
        From [dbo].[CompanyTruck] as [CT]
    ) as [SQCT] 
        On [TCT].[CompanyId] = [SQCT].[CompanyId]
            and [TCT].[TruckId] = [SQCT].[TruckId]
    Left Join (
        Select Distinct [TCTC].[TruckId], Count(*) [DriverAssociations]
        From [TruckerCompanyTruck] [TCTC]
            Inner Join [Trucker] [D]
                On [TCTC].[TruckerId] = [D].[TruckerId]
            Inner Join [TruckerContract] [TC]
                On [TCTC].[TruckerId] = [TC].[TruckerId]
        Where [TCTC].[CompanyId] = [TCT].[CompanyId] -- // Error!
            and [TC].[CompanyId] = [TCT].[CompanyId] -- // Error!
        Group By [TCTC].[TruckId]
    ) as [SQDA]
        On [SQDA].[TruckId] = [T].[TruckId]

The “Error!” lines throw “The multi-part identifier “TCT.CompanyId” could not be bound.”

The ultimate goal is that running this query will produce multiple rows for a truck that has multiple company associations and return the correct driver count.

In the end, this query of the view…

Select *
From [FlattenedTruck]
Where [CompanyId] = 28
Order By [VIN]

…should produce the same output as this.

Exec [GetTrucksForGridWithAssociationCounts] @companyId = 28

But the differ. The sproc returns 1 driver association for companyId 28 and the view returns 1246 driver associations which is all of the associations for that truck regardless of the company. (It’s also different because the sproc does not return the companyId because it’s passed in as a parameter.)

TruckID -> VIN ->  Make -> Model -> ModelYear -> RFIDNo -> VendorCode -> UserLabel -> IsActive -> IsFleetTruck -> DriverAssociations -> Plate ->        CompanyId
26 ->      NULL -> NULL -> NULL ->  NULL ->      NULL ->   NULL ->       NULL ->      1 ->        0 ->            1246 ->               US-WA-9D68812   28
26 ->      NULL -> NULL -> NULL ->  NULL ->      NULL ->   NULL ->       NULL  ->     1 ->        0 ->            1 ->                  US-WA-9D68812
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:23:10+00:00Added an answer on May 27, 2026 at 12:23 pm

    Can you try OUTER APPLY instead of the LEFT JOIN to the SQDA subquery? You’ll want to add the condition of [TCTC].[TruckId] = [T].[TruckId] to the WHERE clause within the subquery, as the APPLY doesn’t take an ON condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to adapt a simple WPF application to use the Model-View-ViewModel pattern.
I am trying to adapt my simple socket server so that it can have
I'm trying to adapt some old build scripts to run with an RPM installation
I am trying to adapt an existing code to a 64 bit machine. The
I'm trying to adapt this answer How do I tokenize a string in C++?
I am trying to adapt a perl script that is used to generate a
I'm trying to adapt this DES encrypting example to AES, so I made the
So I'm trying to adapt this Dropdown menu on Joomla the styles work great
Trying to setup an SSH server on Windows Server 2003. What are some good
I am trying to adapt my tablet web app for use on cell phones.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.