StealthRT StealthRT - 3 months ago 7
SQL Question

SQL Server : query if table exist

I have the following query:

SELECT
COUNT(*) over () as countNum,
[F1] AS STANDARDandOBJ,
[F2] AS CLUSTER,
[F3] AS OBJECTIVE,
[F4] AS EXTRA0
IF COL_LENGTH([tmpExcelDB].[dbo].['Blahsing$'], [F5]) IS NOT NULL
BEGIN
print 'exists'
END
ELSE
BEGIN
print 'Nope'
END,
CONCAT([F1], [F2]) AS combined
FROM
[tmpExcelDB].[dbo].['Blahsing$']
WHERE
LOWER(F3) NOT LIKE 'course tools-%'


But it seems that I have an error of:


Incorrect syntax near ','.


Which is pointing to row:

,CONCAT([F1], [F2]) AS combined


How does this need to be formatted in order to work?

Answer

You can't use IF inside a SELECT, you need a CASE expression. Also, it doesn't make sense to use PRINT inside a column:

SELECT 
     COUNT(*) over () as countNum 
     ,[F1] AS STANDARDandOBJ 
     ,[F2] AS CLUSTER 
     ,[F3] AS OBJECTIVE 
     ,[F4] AS EXTRA0 
     ,CASE 
        WHEN COL_LENGTH('[tmpExcelDB].[dbo].[''Blahsing$'']', '[F5]') IS NOT NULL 
        THEN 'exists'
        ELSE 'Nope'
      END
     ,CONCAT([F1], [F2]) AS combined 
  FROM [tmpExcelDB].[dbo].['Blahsing$'] 
  WHERE 
     LOWER(F3) NOT LIKE 'course tools-%';
Comments