Link to home
Start Free TrialLog in
Avatar of SQLM_M
SQLM_MFlag for India

asked on

Insert Into OpenRowSet Syntax

Hi,

I am trying to insert a row into a table  through openrowset. I get syntax  Line 5: Incorrect syntax near '+'.


DECLARE @MaxDate_cdr DateTime
SET @MaxDate_cdr = (select MAX(CreatedDate) from Month10.dbo.cdr(nolock))
INSERT INTO OPENROWSET('SQLNCLI','Server=192.168.150.147;Trusted_Connection=True;',
     'master.dbo.configtable(Country,Type,LastUpdateTime)Values(''India'',''cdr'','+@MaxDate_cdr+')')

Open in new window

Avatar of JestersGrind
JestersGrind
Flag of United States of America image

Try this.  You have to use dynamic SQL to do that.

Greg


DECLARE @MaxDate_cdr DateTime, @SQL NVARCHAR(MAX)

SET @MaxDate_cdr = (select MAX(CreatedDate) from Month10.dbo.cdr(nolock))

SET @SQL = 
'INSERT INTO OPENROWSET(''SQLNCLI'',''Server=192.168.150.147;Trusted_Connection=True;'',
 ''master.dbo.configtable(Country,Type,LastUpdateTime)Values(''India'',''cdr'',''' + @MaxDate_cdr + ''')'')'

EXECUTE sp_executesql @SQL

Open in new window

SOLUTION
Avatar of SQLM_M
SQLM_M
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SQLM_M

ASKER

It works
Avatar of SQLM_M

ASKER

it works