SQL脚本如何在脚本中添加选项卡或新行

发布于 2024-09-07 07:10:44 字数 520 浏览 5 评论 0原文

我有一个 SQL 脚本,它获取表名并为这些表创建触发器。当我创建触发器后打开触发器时,所有代码都在一行上。我将如何在脚本中添加制表符和换行符以使触发器更具可读性。

示例代码:

SET @SQL = 'ALTER TRIGGER [dbo].[TRG_' + SUBSTRING(@TABLE_NAME,5, LEN(@TABLE_NAME)) + '_$AUD] '
        SET @SQL = @SQL + 'ON [dbo].[' + @TABLE_NAME + '] '
        SET @SQL = @SQL + 'FOR UPDATE, DELETE '
        SET @SQL = @SQL + 'AS '
        SET @SQL = @SQL + 'DECLARE '
        SET @SQL = @SQL + '@BIT INT, '
        SET @SQL = @SQL + '@FIELD INT, '
        SET @SQL = @SQL + '@CHAR INT '

I have a SQL script that acquires table names and creates trigger for those tables. When I open the trigger after creating it all of the code is on one line. How would I go about adding tab and new line characters within the script to make the trigger more readable.

Example code:

SET @SQL = 'ALTER TRIGGER [dbo].[TRG_' + SUBSTRING(@TABLE_NAME,5, LEN(@TABLE_NAME)) + '_$AUD] '
        SET @SQL = @SQL + 'ON [dbo].[' + @TABLE_NAME + '] '
        SET @SQL = @SQL + 'FOR UPDATE, DELETE '
        SET @SQL = @SQL + 'AS '
        SET @SQL = @SQL + 'DECLARE '
        SET @SQL = @SQL + '@BIT INT, '
        SET @SQL = @SQL + '@FIELD INT, '
        SET @SQL = @SQL + '@CHAR INT '

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀里藏娇 2024-09-14 07:10:44

至少对于 MS SQL,您可以使用 CHAR() 和正确的 ASCII 值并在字符串中的正确位置连接,或者您可以在 SQL 字符串本身中包含换行符、制表符等。该字符串可以跨越多行。例如,这应该有效:

SET @SQL = 'ALTER TRIGGER [dbo].[TRG_' + SUBSTRING(@TABLE_NAME,5, LEN(@TABLE_NAME)) + '_$AUD]
ON [dbo].[' + @TABLE_NAME + ']
...

'

For MS SQL at least, you can either use CHAR() with the correct ASCII values and concatenate at the correct places in your strings, or you can just include the newlines, tabs, etc. within your SQL strings themselves. The string can span multiple lines. For example, this should work:

SET @SQL = 'ALTER TRIGGER [dbo].[TRG_' + SUBSTRING(@TABLE_NAME,5, LEN(@TABLE_NAME)) + '_$AUD]
ON [dbo].[' + @TABLE_NAME + ']
...

'
痴者 2024-09-14 07:10:44

这不会更改实际的触发器定义,但您可以使用 Instant SQL Formatter,当你需要处理它时。

This won't change the actual trigger definition, but you could use a tool like Instant SQL Formatter, when you need to work on it.

何止钟意 2024-09-14 07:10:44

用这个

SET @SQL = 'ALTER TRIGGER [dbo].[TRG_' + SUBSTRING(@TABLE_NAME,5, LEN(@TABLE_NAME)) + '_$AUD] 
        ON [dbo].[' + @TABLE_NAME + '] 
        FOR UPDATE, DELETE 
        AS 
        DECLARE 
        @BIT INT, 
        @FIELD INT, 
        @CHAR INT '

use this

SET @SQL = 'ALTER TRIGGER [dbo].[TRG_' + SUBSTRING(@TABLE_NAME,5, LEN(@TABLE_NAME)) + '_$AUD] 
        ON [dbo].[' + @TABLE_NAME + '] 
        FOR UPDATE, DELETE 
        AS 
        DECLARE 
        @BIT INT, 
        @FIELD INT, 
        @CHAR INT '
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文