Как я могу проанализировать динамический SQL перед выполнением в T-SQL?

Я заставил его работать с файлом руля следующим образом:

<ul>
    {{#each dataList}}
    <li>{{name}}: {{value}}</li>
    {{/each}}
</ul>

Основная проблема, казалось, заключалась в том, что вы ссылались на param, но массив, который вы просматривали в руле, фактически назывался [ 112]. Тег списка также должен быть закрыт с помощью </li>, а this оказался ненужным.

5
задан GEOCHET 8 May 2009 в 16:13
поделиться

3 ответа

This might be possible with some sp_executesql trickery:

-- The query you would like to parse
declare @sql nvarchar(max)
set @sql = 'select 1'

declare @testsql nvarchar(max)
declare @result int
set @testsql = N'set parseonly on; ' + @sql
exec @result = sp_executesql @testsql

-- If it worked, execute it
if @result = 0
    begin
    exec sp_executesql @sql
    end

If I use an incorrect query, like 'salact 1', the @result value is nonzero.

The TRY/CATCH does not seem to play well with sp_executesql, so I'm checking the return value instead.

7
ответ дан 18 December 2019 в 14:51
поделиться

What are you trying to accomplish?

As stated before, T-SQL won't compile unless the query is parseable.

If you just want to have a way to verify that the query is fine (ex. to verify that you didn't forget the where statement or so) then maybe the showplan will help you

set showplan_xml on

this will tell the sql server to just parse the query (the query itself is never executed) and return the execution plan for it. It is mostly used for performance issues, but can also be used as a pointer in case something is really incorrect with the query.

3
ответ дан 18 December 2019 в 14:51
поделиться

Попробуйте использовать SET FMTONLY ON и SET FMTONLY OFF.

SET PARSEONLY ON для вашего кода Однако SET PARSEONLY OFF должен работать в большинстве случаев.

MSDN FMTONLY

3
ответ дан 18 December 2019 в 14:51
поделиться
Другие вопросы по тегам:

Похожие вопросы: