?
Solved

Fixing Table alias inside a dynamic SQL query

Posted on 2016-10-09
2
Medium Priority
?
62 Views
Last Modified: 2016-10-09
I had this question after viewing Fixing Temp Table inside dynamic query.

How to fix the table alias here:
DECLARE @AsIsTable nvarchar(MAX);
DECLARE @ChangingAsIsColumnsToTheEquivelantValuesSQL varchar (MAX);
SET @ChangingAsIsColumnsToTheEquivelantValuesSQL = N'SELECT
 ID
,(SELECT ServiceTypeID FROM [dbo].[tb_List_ServiceType] WHERE ServiceTypeName = R.ServiceTypeName) As ServiceTypeID
FROM' +@AsIsTable + ' AS R';

EXECUTE(@ChangingAsIsColumnsToTheEquivelantValuesSQL)

Open in new window



Thanks a lot in advance.
Harreni
0
Comment
Question by:Harreni
2 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41835670
"Fixing" implies there is a known problem and you are not referring to temp tables in your dynamic sql. So what is the exact problem?

Also I'm not sure why you are using a "correlated subquery" in your select clause, but you could add an alias to that table, e.g.
DECLARE @AsIsTable nvarchar(MAX);
DECLARE @ChangingAsIsColumnsToTheEquivelantValuesSQL varchar (MAX);
SET @ChangingAsIsColumnsToTheEquivelantValuesSQL = N' SELECT
          R.ID
        , (
                SELECT
                      MAX(N.ServiceTypeID)
                FROM [dbo].[tb_List_ServiceType] AS N
                WHERE N.ServiceTypeName = R.ServiceTypeName
          )
          AS ServiceTypeID
    FROM ' + @AsIsTable +' AS R'

Open in new window

0
 

Author Closing Comment

by:Harreni
ID: 41835737
Thanks a lot PortletPaul for your help and explanation.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question