Execute SQL script in an Azure SQL Data Warehouse












0














I'm basically trying to execute a SQL script that inserts around 50 views into an Azure Data Warehouse using a Powershell Script. 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.



There is any SQL syntax that I have to add/modify which I'm not considering?



Syntax Error



PowerShell Script:



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!










share|improve this question
























  • Possible duplicate of How to execute .sql file using powershell?
    – LotPings
    Nov 20 at 18:03






  • 1




    Remove the GO and replace with ;
    – Matt
    Nov 20 at 18:37












  • I tried that also and didn't work
    – Giuseppe Adamo
    Nov 20 at 19:08










  • Can you post your Powershell script?
    – Matt
    Nov 20 at 19:38










  • I have posted the PowerShell script. Thanks! @Matt
    – Giuseppe Adamo
    Nov 20 at 20:42
















0














I'm basically trying to execute a SQL script that inserts around 50 views into an Azure Data Warehouse using a Powershell Script. 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.



There is any SQL syntax that I have to add/modify which I'm not considering?



Syntax Error



PowerShell Script:



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!










share|improve this question
























  • Possible duplicate of How to execute .sql file using powershell?
    – LotPings
    Nov 20 at 18:03






  • 1




    Remove the GO and replace with ;
    – Matt
    Nov 20 at 18:37












  • I tried that also and didn't work
    – Giuseppe Adamo
    Nov 20 at 19:08










  • Can you post your Powershell script?
    – Matt
    Nov 20 at 19:38










  • I have posted the PowerShell script. Thanks! @Matt
    – Giuseppe Adamo
    Nov 20 at 20:42














0












0








0







I'm basically trying to execute a SQL script that inserts around 50 views into an Azure Data Warehouse using a Powershell Script. 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.



There is any SQL syntax that I have to add/modify which I'm not considering?



Syntax Error



PowerShell Script:



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!










share|improve this question















I'm basically trying to execute a SQL script that inserts around 50 views into an Azure Data Warehouse using a Powershell Script. 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.



There is any SQL syntax that I have to add/modify which I'm not considering?



Syntax Error



PowerShell Script:



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!







sql-server azure powershell data-warehouse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 20:39

























asked Nov 20 at 17:53









Giuseppe Adamo

33




33












  • Possible duplicate of How to execute .sql file using powershell?
    – LotPings
    Nov 20 at 18:03






  • 1




    Remove the GO and replace with ;
    – Matt
    Nov 20 at 18:37












  • I tried that also and didn't work
    – Giuseppe Adamo
    Nov 20 at 19:08










  • Can you post your Powershell script?
    – Matt
    Nov 20 at 19:38










  • I have posted the PowerShell script. Thanks! @Matt
    – Giuseppe Adamo
    Nov 20 at 20:42


















  • Possible duplicate of How to execute .sql file using powershell?
    – LotPings
    Nov 20 at 18:03






  • 1




    Remove the GO and replace with ;
    – Matt
    Nov 20 at 18:37












  • I tried that also and didn't work
    – Giuseppe Adamo
    Nov 20 at 19:08










  • Can you post your Powershell script?
    – Matt
    Nov 20 at 19:38










  • I have posted the PowerShell script. Thanks! @Matt
    – Giuseppe Adamo
    Nov 20 at 20:42
















Possible duplicate of How to execute .sql file using powershell?
– LotPings
Nov 20 at 18:03




Possible duplicate of How to execute .sql file using powershell?
– LotPings
Nov 20 at 18:03




1




1




Remove the GO and replace with ;
– Matt
Nov 20 at 18:37






Remove the GO and replace with ;
– Matt
Nov 20 at 18:37














I tried that also and didn't work
– Giuseppe Adamo
Nov 20 at 19:08




I tried that also and didn't work
– Giuseppe Adamo
Nov 20 at 19:08












Can you post your Powershell script?
– Matt
Nov 20 at 19:38




Can you post your Powershell script?
– Matt
Nov 20 at 19:38












I have posted the PowerShell script. Thanks! @Matt
– Giuseppe Adamo
Nov 20 at 20:42




I have posted the PowerShell script. Thanks! @Matt
– Giuseppe Adamo
Nov 20 at 20:42












1 Answer
1






active

oldest

votes


















1














The error is related to "go" in the sql script, which cannot be recognized by the powershell scripts you used.



You can make a little changes to your powershell, when it reads "go", execute the above sql scripts.



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 sqltest.sql"
Invoke-SQLDestination $sqlscript


The following is my sql file:



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:
enter image description here






share|improve this answer





















  • Perfect! Thanks!
    – Giuseppe Adamo
    Nov 21 at 12:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398787%2fexecute-sql-script-in-an-azure-sql-data-warehouse%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The error is related to "go" in the sql script, which cannot be recognized by the powershell scripts you used.



You can make a little changes to your powershell, when it reads "go", execute the above sql scripts.



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 sqltest.sql"
Invoke-SQLDestination $sqlscript


The following is my sql file:



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:
enter image description here






share|improve this answer





















  • Perfect! Thanks!
    – Giuseppe Adamo
    Nov 21 at 12:06
















1














The error is related to "go" in the sql script, which cannot be recognized by the powershell scripts you used.



You can make a little changes to your powershell, when it reads "go", execute the above sql scripts.



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 sqltest.sql"
Invoke-SQLDestination $sqlscript


The following is my sql file:



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:
enter image description here






share|improve this answer





















  • Perfect! Thanks!
    – Giuseppe Adamo
    Nov 21 at 12:06














1












1








1






The error is related to "go" in the sql script, which cannot be recognized by the powershell scripts you used.



You can make a little changes to your powershell, when it reads "go", execute the above sql scripts.



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 sqltest.sql"
Invoke-SQLDestination $sqlscript


The following is my sql file:



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:
enter image description here






share|improve this answer












The error is related to "go" in the sql script, which cannot be recognized by the powershell scripts you used.



You can make a little changes to your powershell, when it reads "go", execute the above sql scripts.



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 sqltest.sql"
Invoke-SQLDestination $sqlscript


The following is my sql file:



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:
enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 8:34









Ivan Yang

1,964125




1,964125












  • Perfect! Thanks!
    – Giuseppe Adamo
    Nov 21 at 12:06


















  • Perfect! Thanks!
    – Giuseppe Adamo
    Nov 21 at 12:06
















Perfect! Thanks!
– Giuseppe Adamo
Nov 21 at 12:06




Perfect! Thanks!
– Giuseppe Adamo
Nov 21 at 12:06


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398787%2fexecute-sql-script-in-an-azure-sql-data-warehouse%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen