Dynamic SQL?

I have this syntax in an SSRS report that creates the dataset.  In scouring the internet it looks like this is Dynamic SQL?  Even after reading about it though I don't understand what this is doing:

exec ('Select sp_LP_BillingAnalysisAtty1.TypeFreq
      , sp_LP_BillingAnalysisAtty1.ProfName
      , sp_LP_BillingAnalysisAtty1.Initials
      , sp_LP_BillingAnalysisAtty1.WorkedUnits
      , Professionals.ProfClass
      , Professionals.QPROFSTATUS
From ' + @spsp_LP_BillingAnalysisAtty1 + ' as sp_LP_BillingAnalysisAtty1
INNER JOIN Prolaw.dbo.Professionals as Professionals on (sp_LP_BillingAnalysisAtty1.Initials = Professionals.Initials and sp_LP_BillingAnalysisAtty1.ProfName = Professionals.ProfName)
Order by sp_LP_BillingAnalysisAtty1.ProfName
      , sp_LP_BillingAnalysisAtty1.TypeFreq')
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
Yes, it is dynamic SQL, as it parses a big string that is a T-SQL statement, then executes it via exec(the string).

>From ' + @spsp_LP_BillingAnalysisAtty1 + ' as sp_LP_BillingAnalysisAtty1
The parameter @spsp_LP_BillingAnalysisAtty1  is the table name, and you can't do this in a typed SQL statement, hence the need to pull this off dynamically.

Since this is SSRS, there is likely a parameter somewhere with the text/selection 'Enter a Billing Analysis Attorney Here', and that value is moved into the parameter @spsp_LP_BillingAnalysisAtty1 and then used in the dynamic SQL statement.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andrei FomitchevCommented:
exec -- executes the string '...' that is in () - dynamic query
Select sp_LP_BillingAnalysisAtty1.TypeFreq -- SELECTs the column TypeFreq from the table sp_LP_BillingAnalysisAtty1
      , sp_LP_BillingAnalysisAtty1.ProfName -- SELECTs the column ProfName from the table sp_LP_BillingAnalysisAtty1
      , sp_LP_BillingAnalysisAtty1.Initials -- SELECTs the column Initials from the table sp_LP_BillingAnalysisAtty1
      , sp_LP_BillingAnalysisAtty1.WorkedUnits -- SELECTs the column WorkedUnits from the table sp_LP_BillingAnalysisAtty1 
      , Professionals.ProfClass -- SELECTs the column ProfClass from the table Professionals
      , Professionals.QPROFSTATUS -- SELECTs the column QPROFSTATUS from the table Professionals
From ' -- FROM starts here
 + @spsp_LP_BillingAnalysisAtty1 + -- adds table name from the variable @spsp_LP_BillingAnalysisAtty1
' as sp_LP_BillingAnalysisAtty1 -- assigns alias sp_LP_BillingAnalysisAtty1 that is used as table name
INNER JOIN Prolaw.dbo.Professionals -- joins the table Professionals from DB Prolaw schema dbo
 as Professionals -- assigns the alias Professionals
on (sp_LP_BillingAnalysisAtty1.Initials = Professionals.Initials and sp_LP_BillingAnalysisAtty1.ProfName = Professionals.ProfName) -- join condition
Order by sp_LP_BillingAnalysisAtty1.ProfName
      , sp_LP_BillingAnalysisAtty1.TypeFreq -- orders result set

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.