简体   繁体   English

在Azure SQL数据仓库中执行SQL脚本

[英]Execute SQL script in an Azure SQL Data Warehouse

I'm basically trying to execute a SQL script that inserts around 50 views into an Azure Data Warehouse using a Powershell Script. 我基本上是在尝试执行SQL脚本,该脚本使用Powershell脚本将大约50个视图插入到Azure数据仓库中。 But for some reason doesn't like the syntax that I'm using. 但是由于某种原因,我不喜欢我使用的语法。

For example: 例如:

CREATE VIEW XX.FirstView 
AS 
     SELECT bookings.Activity 
     FROM XX.FirstTable bookings
GO

CREATE VIEW XX.SecondView 
AS 
     SELECT books.ID 
     FROM XX.SecondTable books

If I run it directly in the SQL Server Data warehouse seems to work fine but when running it from Powershell it complains about a syntax error. 如果我直接在SQL Server数据仓库中运行它,似乎工作正常,但是从Powershell运行它时会抱怨语法错误。

There is any SQL syntax that I have to add/modify which I'm not considering? 有什么我要添加/修改的SQL语法我没有考虑?

Syntax Error 语法错误

PowerShell Script: PowerShell脚本:

function Invoke-SQLDestination {
param([string] $sqlCommand = "")
    $sqlCommand.ToString()
    $connectionStringDestination = "XXXXXXXX"

    $connection = new-object system.data.SqlClient.SQLConnection($connectionStringDestination)
    $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
    $connection.Open()

    $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
    $dataset = New-Object System.Data.DataSet

    $adapter.Fill($dataSet) 

    $connection.Close()
    $dataSet.Tables
}


    $sqlscript = Get-Content ./SqlViewCreate.sql | Out-String
    Invoke-SQLDestination $sqlscript

Thanks! 谢谢!

The error is related to "go" in the sql script, which cannot be recognized by the powershell scripts you used. 该错误与sql脚本中的“ go”有关,您使用的powershell脚本无法识别该错误。

You can make a little changes to your powershell, when it reads "go", execute the above sql scripts. 您可以对powershell进行一些更改,当它显示为“ go”时,请执行上述sql脚本。

Code like below: 如下代码:

function Invoke-SQLDestination {
param([string] $sqlCommand = "")
    #$sqlCommand.ToString()

    $commandTxt = @(Get-Content -Path $sqlCommand)
    foreach($txt in $commandTxt)
    {
    if($txt -ne "Go")
    {
    $SQLPacket += $txt +"`n"
    }
    else
    {
    $connectionStringDestination = "xxxx"
    Write-Host $SQLPacket
    $connection = new-object system.data.SqlClient.SQLConnection($connectionStringDestination)
    $command = new-object system.data.sqlclient.sqlcommand($SQLPacket,$connection)
    $connection.Open()

    $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
    $dataset = New-Object System.Data.DataSet

    $adapter.Fill($dataSet) 

    $connection.Close()
    $dataSet.Tables

    $SQLPacket =""

    }
    }
}

    # here pass the sql file path
    $sqlscript = "D:\azure sql\test.sql"
    Invoke-SQLDestination $sqlscript

The following is my sql file: 以下是我的sql文件:

create view v1
as
select name from student
go

create view v2
as
select name from student
go

create view v3
as
select name from student
go

The test result: 测试结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM