Solved

Fixing Table alias inside a dynamic SQL query

Posted on 2016-10-09
2
37 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 48

Accepted Solution

by:
PortletPaul earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add different cell to otherwise similiar row 4 37
Oracle DB monitor SW 21 47
Help Required 2 29
SQL - Use results of SELECT DISTINCT in a JOIN 4 14
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

786 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