Partilhar via


Dynamic SQL in Synapse SQL

In this article, you'll find tips for using dynamic SQL and developing solutions using Synapse SQL.

Dynamic SQL Example

When developing application code, you may need to use dynamic SQL to help deliver flexible, generic, and modular solutions.

Observação

Dedicated SQL pool does not support blob data types at this time. Não suportar tipos de dados de blob pode limitar o tamanho de suas cadeias de caracteres, uma vez que os tipos de dados de blob incluem os tipos varchar(max) e nvarchar(max). If you have used these types in your application code to build large strings, you need to break the code into chunks and use the EXEC statement instead.

Um exemplo simples:

DECLARE @sql_fragment1 VARCHAR(8000)=' SELECT name '
,       @sql_fragment2 VARCHAR(8000)=' FROM sys.system_views '
,       @sql_fragment3 VARCHAR(8000)=' WHERE name like ''%table%''';

EXEC( @sql_fragment1 + @sql_fragment2 + @sql_fragment3);

If the string is short, you can use sp_executesql as normal.

Observação

As instruções executadas como SQL dinâmico ainda estarão sujeitas a todas as regras de validação do T-SQL.

Próximos passos

Para obter mais dicas de desenvolvimento, consulte Visão geral do desenvolvimento.