From : http://sqlblog.com/blogs/maria_zakourdaev/archive/2012/05/11/sqlcmd-mode-give-it-one-more-chance.aspx?utm_source=tuicool

- Click on me. Choose me. - asked one forgotten feature when some bored DBA was purposelessly wondering through the Management Studio menu at the end of her long and busy working day.

- Why would I use you? I have heard of no one who does. What are you for? - perplexedly wondered aged and wise DBA. At least that DBA thought she was aged and wise though each day tried to prove to her that she wasn't.

- I know you. You are quite lazy. Why would you do additional clicks to move from window to window? From Tool to tool ? This is irritating, isn't it? I can run windows system commands, sql statements and much more from the same script, from the same query window!

- I have all my tools that I‘m used to, I have Management Studio, Cmd, Powershell. They can do anything for me. I don’t need additional tools.

- I promise you, you will like me. – the thing continued to whine .

- All right, show me. – she gave up. It’s always this way, she thought sadly, - easier to agree than to explain why you don’t want.

- Enable me and then think about anything that you always couldn’t do through the management studio and had to use other tools.

- Ok. Google for me the list of greatest features of SQL SERVER 2012.

- Well... I’m not sure... Think about something else.

- Ok, here is something easy for you. I want to check if file folder exists or if file is there. Though, I can easily do this using xp_cmdshell …

- This is easy for me. – rejoiced the feature.

By the way, having the items of the menu talking to you usually means you should stop working and go home. Or drink coffee. Or both. Well, aged and wise dba wasn’t thinking about the weirdness of the situation at that moment.

- After enabling me, – said unfairly forgotten feature (it was thinking of itself in such manner) – after enabling me you can use all command line commands in the same management studio query window by adding two exclamation marks!! at the beginning of the script line to denote that you want to use cmd command:

-Just keep in mind that when using this feature, you are actually running the commands ON YOUR computer and not on SQL server that query window is connected to. This is main difference from using xp_cmdshell which is executing commands on sql server itself. Bottomline, use UNC path instead of local path.

- Look, there are much more than that. - The SQLCMD feature was getting exited.- You can get IP of your servers, create, rename and drop folders. You can see the contents of any file anywhere and even start different tools from the same query window:

Not so aged and wise DBA was getting interested: - I also want to run different scripts on different servers without changing connection of the query window.

- Sure, sure! Another great feature that CMDmode is providing us with and giving more power to querying. Use “:” to use additional features, like :connect that allows you to change connection:

- Now imagine, you have one script where you have all your changes, like creating staging table on the DWH staging server, adding fact table to DWH itself and updating stored procedures in the server where reporting database is located.

- Now, give me more challenges!

- Script out a list of stored procedures into the text files.

- You can do it easily by using command :out which will write the query results into the specified text file. The output can be the code of the stored procedure or any data. Actually this is the same as changing the query output into the file instead of the grid.

- Now, take all of the scripts and run all of them, one by one, on the different server.

- Easily

- Come on... I’m sure that you can not...

-Why not? Naturally, I can do it using :r commant which is opening a script and executing it. Look, I can also use :setvar command to define an environment variable in SQLCMD mode. Just note that you have to leave the empty string between :r commands, otherwise it’s not working although I have no idea why.

- Wow.- She was really impressed. - Ok, I’ll go to try all those…

-Wait, wait! I know how to google the SQL SERVER features for you! This example will open chrome explorer with search results for the “SQL server 2012 top features” ( change the path to suit your PC):

“Well, this can be probably useful stuff, maybe this feature is really unfairly forgotten”, thought the DBA while going through the dark empty parking lot to her lonely car. “As someone really wise once said: “It is what we think we know that keeps us from learning. Learn, unlearn and relearn”.

SQLCMD Mode: give it one more chance的更多相关文章

  1. SQLCMD备忘录:执行文件夹所有Sql文件

    在做性能测试的时候最希望的一件事情是数据自动导入. 一般做法就是写很多SQL文件,通过Bat自动执行所有Sql文件. Bat代码: @ECHO OFF SET SQLCMD="C:\Prog ...

  2. ms sql server 在cmd中执行sqlcmd的时候报错

    cmd下直接输入sqlcmd会提示 错误: HResult 0x2,级别 16,状态 1命名管道提供程序: 无法打开与 SQL Server 的连接 [2].Sqlcmd: 错误: Microsoft ...

  3. Chance – 功能强大的 JavaScript 随机数生成类库

    Chance 是一个基于 JavaScript 的随机数工具类.可以生成随机数字,名称,地址,域名,邮箱,时间等等,几乎网站中使用的任何形式的内容都能够生成.这个随机数工具可以帮助减少单调的测试数据编 ...

  4. poj 1698 Alice‘s Chance

    poj 1698  Alice's Chance 题目地址: http://poj.org/problem?id=1698 题意: 演员Alice ,面对n场电影,每场电影拍摄持续w周,每周特定几天拍 ...

  5. sqlserver执行sql文件命令(sqlcmd)

    个人自用sqlcmd命令: sqlcmd -E -i test.sql -d databasename -s 127.0.0.1 sqlcmd命令解释: 用法: Sqlcmd            [ ...

  6. sqlcmd

    使用sqlcmd可以在批处理脚本中执行SQL.虽然这个命令的参数很多,但幸运的是,我们不需要全部理解,在这里简要介绍以下几个: { -U login_id [ -P password ] } | –E ...

  7. (转)使用SQLCMD在SQLServer执行多个脚本

    概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个sql文件,并且有执行顺序,如我现在工作的公司,十几个客 ...

  8. sqlcmd 登录和执行语句。

    sqlcmd -U sa -P atc@2014 -S HK-DB01 -d msdb -Q "exec sp_start_job @job_name='3PL_OUT_TEST'" ...

  9. linux安装sqlcmd登录sqlserver

    首先从微软网站下载sqlncli安装文件,link. 因为是在内网安装,首先手工下载unixODBC2.3.0.tar.gz,下载后上传到服务器. 将下载的tar文件文件,放在同build_dm.sh ...

随机推荐

  1. Java常见知识点(二)

    21.常量池专门用于管理在编译时被确定并被保存在已编译的.class文件中的一些数据.它包括了关于类.方法.接口中的常量,还包括字符串常量.   22.String已经重写了Object的equals ...

  2. html中给元素添加背景图片或者gif动图

    添加背景图片有四种常用的方式,分别是: repeat    完全平铺 repeat-x    横向平铺 repeat-y    纵向平铺 no-repeat    不平铺 使用示例: <!DOC ...

  3. 在什么情况下Java比C++快?

    转载   http://www.importnew.com/16056.html

  4. SPOJ-913

    Query on a tree II Time Limit: 433MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Su ...

  5. Selenium2+python自动化39-关于面试的题【转载】

    前言 最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下, 回答不妥的地方欢迎各位高手拍砖指点.   一.selenium中如何判断元素是否存在? 首先selen ...

  6. 安装tomcat7.0.82

    下载安装tomcat mkdir /tools cd /tools wget http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.82/bin/ap ...

  7. kotlin 写的一个简单 sql 查询解析器

    package com.dx.efuwu.core import org.apache.commons.lang.StringUtils import java.sql.PreparedStateme ...

  8. unity制作简单血条

    学习Unity已经10天了,也没发现有什么长进,真的急.昨天仿着官方Demo做了个射击游戏轮廓,其中需要给每个怪做一个血条. 搜了一些,挺复杂的,用NGUI或者UGUI,外加很长的代码...不过还是找 ...

  9. golang笔记:cookie

    在同一个问题上栽了两次,以后碰到cookie出问题多半都是因为这个. Request.Cookie(name)取Cookie的时候,返回值只有name和value cookie.go cookies ...

  10. POJ2032 Building a Space Station(Kruskal)(并查集)

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7469   Accepte ...