4

I am trying to pass a variable into a SELECT statement in OPENROWSET but I keep getting an error

DECLARE @dDateTIME DATE

SET @dDateTIME = (SELECT SalesDate FROM dbo.SalesDate)

INSERT INTO dbo.tblSales
SELECT * FROM OPENROWSET('MSDASQL', 'dsn=mydsn;uid=myid;pwd=mypwd;',
    'SELECT 
        ID,
        TranDate,
        ProductID,
        CostValue,
        SalesValue,
        QtySold,
    FROM tblSales WHERE TranDate='' + @dDateTIME + ''')

DECLARE @dDateTIME DATE
SET @dDateTIME = (SELECT SalesDate FROM dbo.SalesDate)

DECLARE @SQL NVARCHAR(1024) = 
 'SELECT 
        ID,
        TranDate,
        ProductID,
        CostValue,
        SalesValue,
        QtySold,
    FROM tblSales WHERE TranDate=''' + CAST(@dDateTIME AS VARCHAR(64)) + ''''

DECLARE @RunSQL NVARCHAR(max) 
SET @RunSQL=
    'SELECT * FROM OPENROWSET (''MSDASQL'', ''dsn=mydsn;uid=myid;pwd=mypwd;'',''EXEC @SQL'')'`

What syntax do I use to apply to @SQL?

The error I get is:

The error is :OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Pervasive][ODBC Client Interface][LNA][Pervasive][ODBC Engine Interface]Error in predicate: TranDate = '(SELECT @dDateTIME)' "

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2393602
  • 51
  • 1
  • 1
  • 3
  • 3
    ***WHAT*** is the error you're getting !?!?!?!?!? We can't read your screen - nor your mind.... you're going to have to **tell us** .... and while you're at it - please also tell us ***what database*** you're using (and which version); SQL is just the query language - not a database product.... – marc_s May 17 '13 at 11:07
  • The database I am using is SQL Server 2008 r2 The database the dsn is connected to is Pervasive I know the error is being caused by the syntax I am using when passing the varible. If I type the datevalue into the WHERE statement it works ok. The error is :OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Pervasive][ODBC Client Interface][LNA][Pervasive][ODBC Engine Interface]Error in predicate: TranDate = '(SELECT @dDateTIME)' ". – user2393602 May 17 '13 at 11:21

2 Answers2

1

Your variable is not being concatenated to the string (its ''' to close a string with a ') to correct this (and perform the necessary type conversion):

DECLARE @SQL NVARCHAR(1024) = 
 'SELECT 
        ID,
        TranDate,
        ProductID,
        CostValue,
        SalesValue,
        QtySold,
    FROM tblSales WHERE TranDate=''' + CAST(@dDateTIME AS VARCHAR(64)) + ''''

Additionally you cannot use an expression or variable with OPENROWSET so you are going to need to call it via EXEC()/sp_executeSQL, see; Using a Variable in OPENROWSET Query

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • `DECLARE @dDateTIME DATE SET @dDateTIME = (SELECT SalesDate FROM dbo.SalesDate) DECLARE @SQL NVARCHAR(1024) = 'SELECT ID, TranDate, ProductID, CostValue, SalesValue, QtySold, FROM tblSales WHERE TranDate=''' + CAST(@dDateTIME AS VARCHAR(64)) + '''' DECLARE @RunSQL NVARCHAR(max) SET @RunSQL= 'SELECT * FROM OPENROWSET (''MSDASQL'', ''dsn=mydsn;uid=myid;pwd=mypwd;'',''EXEC @SQL'')'` What syntax do I use to apply @SQL??? – user2393602 May 17 '13 at 12:05
1

You can dynamically create SQL statement and then run that command.

DECLARE @dDateTIME DATE,
        @RunSQL NVARCHAR(max) 

SET @dDateTIME = (SELECT SalesDate FROM dbo.SalesDate)

SELECT @RunSQL =
  'INSERT INTO dbo.tblSales
   SELECT * FROM OPENROWSET(''MSDASQL'', ''dsn=mydsn;uid=myid;pwd=mypwd;'',
    ''SELECT 
        ID,
        TranDate,
        ProductID,
        CostValue,
        SalesValue,
        QtySold,
    FROM tblSales WHERE TranDate=''''' + CONVERT(nvarchar, @dDateTIME, 112) + ''''''')'
--PRINT @RunSQL    
EXEC sp_executesql @RunSQL 
Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44