Am trying to check whether a file exists, and store the result in a variable. Am expecting the variable @file_exists to be either true or false, but am getting the error:
Conversion failed when converting the nvarchar value 'xp_fileexist C:\ABC\myfile_20200521.xls, ' to data type int
What is it that am doing wrong?
DECLARE @filepath VARCHAR(MAX), @file_exists INT = 0, @sql VARCHAR(MAX); SET @filepath = (SELECT CONCAT('C:\ABC\myfile_',convert(varchar,getdate(),112),'.xls')); SET @sql = N'xp_fileexist '+@filepath+ N', '+@file_exists+ N' OUT' EXEC sp_executesql @sql IF(@file_exists = 1) PRINT 'File exists'