1

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.

5
  • 1
    Cannot find where '@hdoc' is defined. Commented May 6, 2013 at 8:34
  • hdoc is declared at the start of the procedure. Its not shown here but is present Commented May 6, 2013 at 8:42
  • outside the cursor it is working fine. Commented May 6, 2013 at 8:42
  • Asking just to confirm : Does "Dynamic SQL" work outside cursor and not just the normal SQL statement ? Commented May 6, 2013 at 8:49
  • Have you added PRINT @querry just before the EXEC to make sure that the dynamic SQL is really what you expect? Commented May 6, 2013 at 14:26

3 Answers 3

1

In cursor you have to again execute your xml file , with xml output declaration.

DECLARE @id VARCHAR(25)

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 = 'DECLARE @hdoc INT;           
    EXEC sp_xml_preparedocument @hdoc OUTPUT,'''+ @info+'''
        Select Statement
        Insert Statement exec (@query)      

    FETCH NEXT FROM myCursor INTO @id       
END

CLOSE myCursor
DEALLOCATE myCursor
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

DECLARE @ParmDefinition nvarchar(500);

/* Build the SQL string one time.*/
set @query = 
'SELECT * FROM OPENXML(@Temphdoc, '''+@loc+''', 3) WITH (code_db_key int)';
SET @ParmDefinition = N'@Temphdoc varchar(1000)';

/* This can be in cursor loop */

EXECUTE sp_executesql @query, @ParmDefinition,
                      @Temphdoc = @hdoc;

Comments

0

Change

set @query = 'SELECT * FROM OPENXML(@hdoc, '+@loc+', 3) WITH (code_db_key int)'

to

set @query = 'SELECT * FROM OPENXML(@hdoc, '+@loc+', 3) WITH (code_db_key int);'

--

Ok, try this,

set @query = CONCAT('SELECT * FROM OPENXML(@hdoc,',+@loc+,', 3) WITH (code_db_key int);')

6 Comments

Im trying different things atm.
And like Ravi says, there is no declare for @hdoc
There is also no declare for @tempcolumnname
now its throwing other exception Msg 102, Level 15, State 3, Line 171 Incorrect syntax near ')'.
all declare statement are there at the start of procedure . Its a long procedure so I have not copied everything.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.