【问题标题】:SQLCMD mode: Environment variable syntaxSQLCMD 模式:环境变量语法
【发布时间】:2014-08-10 23:22:53
【问题描述】:

我假设在 T-SQL 运行期间设置我的 SSMS 输出文件路径的最佳方法是使用 SQLCMD 模式。 如何在代码中使用环境变量(例如 %systemroot%)?以下炸弹:

:set mypath %systemroot%
:out $(mypath)"\the_result.txt"
select * from myTab 

也许还有:Query.sql 的文件夹是否有此代码或工作文件夹的环境变量? 谢谢!

【问题讨论】:

    标签: tsql environment-variables ssms sqlcmd


    【解决方案1】:

    如何使用 T-SQL 读取 SQL Server 中的环境变量

    要读取 T-SQL 中的环境变量,可以使用 SQL Server 中的xp_cmdshell 扩展存储过程。

    以下示例显示如何读取 %windir% 环境变量,该变量使用 xp_cmdshell 给出 SQL Server 上的 Windows 目录路径:

    DECLARE @windir nvarchar(255)
    CREATE TABLE #Tmp
    (
    EnvVar nvarchar(255)
    )
    INSERT INTO #Tmp exec xp_cmdshell 'echo %windir%'
    SET @windir = (SELECT TOP 1 EnvVar from #Tmp)
    
    SELECT @windir as 'Windows Directory'
    

    注意:要运行此命令,您需要是 sysadmin 固定服务器的成员。如果您希望其他人能够执行此命令,则必须显式授予他们执行 xp_cmdshell 存储过程的权限。

    MSDN 上查找有关此存储过程的更多信息。 Source

    在 sqlcmd 中使用命令提示符环境变量 在以下示例中,设置了四个环境变量,然后从 sqlcmd 调用。

    C:\>SET tablename=Person.Person
    C:\>SET col1=FirstName
    C:\>SET col2=LastName
    C:\>SET title=Ms.
    C:\>sqlcmd -d AdventureWorks2012
    1> SELECT TOP 5 $(col1) + ' ' + $(col2) AS Name
    2> FROM $(tablename)
    3> WHERE Title ='$(title)'
    4> GO
    

    Source

    【讨论】:

    • 谢谢,不胜感激!
    【解决方案2】:

    一些注意事项:

    [1] SSMS 中支持的 SQLCMD 脚本

    https://docs.microsoft.com/en-us/sql/ssms/scripting/edit-sqlcmd-scripts-with-query-editor?view=sql-server-ver15

    [2] 从上面的链接,您似乎已经阅读过(很久以前),进一步了解 :!!

    那就以这个为例来看看

    :!!set
    

    ...和这个一样...

    !!set
    

    无论如何,输出显示它是 SystemRoot,而不是 systemroot。

    [3] SSMS 默认包含完成时间

    i) 工具 > 选项 > 查询执行 > SQL Server > 高级。

    ii) 取消选中显示完成时间复选框。 所以,对于答案...

    :out $(SystemRoot)"\the_result.txt"
    select * from myTab 
    :out stdout
    

    我知道了……

    <p style="color: red"> 
    Unable to redirect output to C:\WINDOWS\the_result.txt. <br>
    Access to the path 'C:\WINDOWS\the_result.txt' is denied.<br>
    Msg 208, Level 16, State 1, Line 31<br>
    Invalid object name 'myTab'.
    </p>

    ...再说一次,我为什么要向 C:\WINDOWS 中的文件输出一个不存在的表的查询! ;) 让我们创建一个测试表,最终的脚本是:

    --[4] Turn off (x row affected)
    set nocount on
    GO
    
    create table myTab(
        ID int identity(1,1) not null
        ,colTest varchar(20) null
    );
    insert into myTab
        select 'Hello world';
    go
    
    --change the output to be a file in the temporary directory
    --This file will be overwritten each time the script is ran
    :out $(TEMP)\the_result.txt
    --print this to the output
    print ' <- [5] There might be squiggly bits'
    print '$(SystemRoot)'
    select * from myTab
    go
    
    --reset the output
    :out stdout
    --use type to see file contents
    !!type %TEMP%\the_result.txt
    --let's be tidy
    !!del %TEMP%\the_result.txt
    drop table myTab
    go
    

    消息输出

     <- [5] There might be squiggly bits
    C:\WINDOWS
    ID          colTest
    ----------- --------------------
    1           Hello world
    

    总结

    什么都不做,您可以在 SSMS 中的 SQLCMD 模式下访问环境变量一次

    print '$(SystemRoot)'
    

    【讨论】:

    • 您好,非常感谢您的贡献!我会在闲暇时阅读它,它似乎很有用......一切顺利
    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多