码迷,mamicode.com
首页 > 数据库 > 详细

PowerShell使用 invoke-sqlcmd 连行脚本文件,并向脚本文件存入参数

时间:2014-11-13 12:59:24      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:powershell使用 invoke-   并向脚本文件存入参数   

原文 http://stackoverflow.com/questions/16654866/invoke-sqlcmd-inputfile-and-variable

Init.ps1

$DATABASEFILENAME = "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008INSTANCE\MSSQL\DATA\myDB.mdf"
$DATABASELOGNAME = "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL2008INSTANCE\MSSQL\DATA\myDB_log.ldf"
$DBUSEROWNER = "domain\spsetup"

CreateDatabase.ps1

try {
        $createDatabaseScript = ($scriptsFolder,$eachRelease,$DeployEnvironment,"Config" -join "\") + "\JM SiteRequest Database.sql"
        $sqlVariable = "DATABASEFILENAME=$DATABASEFILENAME", "DATABASELOGNAME=$DATABASELOGNAME", "DBUSEROWNER=$DBUSEROWNER"
        Invoke-Sqlcmd -ServerInstance "$MySQLServer" -InputFile "$createDatabaseScript" -ErrorAction Stop -Variable $sqlVariable
}
catch [Exception] {
        Write-Error "Database error: $_.Exception"
}

$sqlVariable 对参数声名很挑剔。变量之前或之后不能有空格

注意 $sqlVariable 里变量前后不能有空格

SQL script

CREATE DATABASE [SiteRequestDB] ON  PRIMARY
( NAME = N'SiteRequestDB', FILENAME = N'$(DATABASEFILENAME)' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON 
( NAME = N'SiteRequestDB_log', FILENAME = N'$(DATABASELOGNAME)' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

在sql脚本中对$sqlVariable 中变量调用方式 $(变量名)


PowerShell使用 invoke-sqlcmd 连行脚本文件,并向脚本文件存入参数

标签:powershell使用 invoke-   并向脚本文件存入参数   

原文地址:http://blog.csdn.net/wangzhpwang/article/details/41075559

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!