我通常需要将查询结果发送到" Grid"当使用SSMS但我有一个特定的脚本时,我需要将结果发送到文本"。有没有办法在我的.sql文件的开头编写脚本? 我想这只是SSMS可以理解的东西,它不会成为通用的sql语句
答案 0 :(得分:2)
您可能会发现此Reddit主题对您的案例很有帮助:https://www.reddit.com/r/SQLServer/comments/1vrlx3/i_want_to_turn_on_text_output_via_sql_command/
基本上,如果您打开了SQLCMD模式,请运行以下命令以打印到文本:
:OUT STDOUT
SELECT * FROM TABLE
如果在未启用SQLCMD模式的情况下执行脚本,则发出警告,您可以使用以下检查:
SET NOEXEC OFF; -- previous execution may have toggled it
:setvar IsSqlCmdEnabled "True"
GO
IF ('$(IsSqlCmdEnabled)' = '$' + '(IsSqlCmdEnabled)')
BEGIN
PRINT('Use SqlCmd-mode!!');
SET NOEXEC ON;
-- RAISERROR ('This script must be run in SQLCMD mode.', 20, 1) WITH LOG
END
ELSE
BEGIN
PRINT('Using SqlCmd-mode')
-- insert the code you really want to execute:
-- ...
END
SET NOEXEC OFF; -- do not disable next execution in this session