Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to have a IF statement in a nested select

Author  Topic 

madlo
Starting Member

41 Posts

Posted - 2015-05-04 : 05:46:05
I need a query to always give output irrespective if the table exists or not. If it doesn't exists it just returns no rows but does return the columns.
The query will be used in SQL Server Analysis Services as a fact table

I have the following query simplified

IF object_id('MyTable', 'U') is not NULL
BEGIN

Select ColA,ColB,ColC from MyTable
ELSE
BEGIN
CREATE TABLE #MyTempTable](
[ColA] [int] ,
[ColB] [nvarchar](30),
[ColC] [nvarchar](30) ,
)
Select * from MyTempTable
END

This query runs perfect outside analysis services.

However when I put it in analysis services fact table is equivalent to making the above query a nested select of
Select * from
(
//If select
)MyTable

I then get an error
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'IF'.

because it doesn't like the if statement in the nested select.

Any possible way to put a nested if statement inside a subquery?

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-05-04 : 12:41:50
within a query, CASE is used instead of IF

Gerald Britton, MCSA
Toronto PASS Chapter
Go to Top of Page
   

- Advertisement -