jeudi 6 février 2020

IF statement with Varible and Openrowset()

Im stuck in trying to figure out how to use an IF statement with my variables. Might be easier then i think however my query looks like this:

DECLARE @Date VARCHAR(4)
SET @Date = '2019'

DECLARE @SQL VARCHAR(MAX)
SET @SQL =

'SELECT
*       
FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',
  ''Excel 12.0 Xml;HDR=YES
   Database=C:\temp\TEMP.xlsx'',
   ''SELECT * FROM ' + @Date + ''') ' 

EXEC(@SQL)

I would like to use and IF ELSE ... statement if something goes wrong do this else execute the query something like this:

IF @SQL .... (goes wrong) <DO something> 
    ELSE EXEC(@SQL)

Aucun commentaire:

Enregistrer un commentaire