I have to dynamically create a query inside cursor
DECLARE @id VARCHAR(10)
declare @loc varchar(25)
set @loc = '/MainItem/SubItem';
declare @query varchar(max)
DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT * FROM @tempcolumnname
OPEN myCursor
FETCH NEXT FROM myCursor INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
set @query = 'SELECT * FROM OPENXML(@hdoc, '+@loc+', 3) WITH (code_db_key int)'
exec (@query)
FETCH NEXT FROM myCursor INTO @id
END
but executing this throws an exception
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@hdoc"Msg 319, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'with'.If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement
must be terminated with a semicolon.`
But when I am executing the same query outside the cursor, it is working fine.
Try this :
In cursor you have to again execute your xml file , with xml output declaration.
Change
to
--
Ok, try this,