June 3, 2014 at 12:01 am
Comments posted to this topic are about the item Extracting data with headers using PowerShell
June 3, 2014 at 3:35 am
where do we need to keep this master.sql script file? same directory where the PowerShell script resides?
June 3, 2014 at 4:23 am
Will it properly extract .csv file and show in excel if some columns are nvarchar and contain data in Russian for example?
June 3, 2014 at 4:43 am
it.ma (6/3/2014)
where do we need to keep this master.sql script file? same directory where the PowerShell script resides?
Yes, same directory.
June 3, 2014 at 4:45 am
sql_er (6/3/2014)
Will it properly extract .csv file and show in excel if some columns are nvarchar and contain data in Russian for example?
One good way to find out is to try it.
June 3, 2014 at 6:46 am
Useful, but I'd avoid using the args[] variable and do it "the Powershell way" with a Param block at the start of the script, that way you get things like autocomplete for free and named arguments, as well as getting guarantees that mandatory parameters have to be supplied.
param(
[Parameter(Mandatory=$true, HelpMessage='Enter name of server')]
[String]$ServerName,
[Parameter(Mandatory=$true, HelpMessage='Enter the name of the database')]
[String]$Database,
[Parameter(Mandatory=$true, HelpMessage='Enter the path to the source file')]
[String]$Sourcefile
)
June 3, 2014 at 7:26 am
andycadley (6/3/2014)
Useful, but I'd avoid using the args[] variable and do it "the Powershell way" with a Param block at the start of the script, that way you get things like autocomplete for free and named arguments, as well as getting guarantees that mandatory parameters have to be supplied.
param(
[Parameter(Mandatory=$true, HelpMessage='Enter name of server')]
[String]$ServerName,
[Parameter(Mandatory=$true, HelpMessage='Enter the name of the database')]
[String]$Database,
[Parameter(Mandatory=$true, HelpMessage='Enter the path to the source file')]
[String]$Sourcefile
)
I'm thinking that would make the process batch-job resistant.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 3, 2014 at 7:33 am
@louis Li,
I realize that $dt is a loop variable but what did you intend that "dt" stand for? I'd also like to know why the use of $1? Why not give it a more readable name or is that required to be numerically name due to position or???
Also, why any abbreviations? Why not make the code more self documenting with only slightly longer names?
How does this handle embedded delimiters?
Last but not least, I know this was meant to be a short "spackle" style article but a lot of people could end up using something like this. Do you have any performance figures for this method?
--Jeff Moden
Change is inevitable... Change for the better is not.
June 3, 2014 at 7:58 am
Jeff Moden (6/3/2014)
I'm thinking that would make the process batch-job resistant.
How so? You can still specify the parameters on the command line, you can even specify them positionally and it'll work exactly the same (though in that case I'd also add Position(x) to each of the Parameter attributes to making positional parameter order explicit).
June 3, 2014 at 12:29 pm
andycadley (6/3/2014)
Jeff Moden (6/3/2014)
I'm thinking that would make the process batch-job resistant.
How so? You can still specify the parameters on the command line, you can even specify them positionally and it'll work exactly the same (though in that case I'd also add Position(x) to each of the Parameter attributes to making positional parameter order explicit).
Didn't know that. I thought it was going to popup questions that required answers that couldn't be answered by providing them in a batch file.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 3, 2014 at 1:19 pm
Can the script.sql file can have a select statement with joins? or this can be only used to export for just plain tables?
June 3, 2014 at 5:16 pm
tried the same. (with master.sql in the same directory as .ps1 script)
It says
"You cannot call a method on a null-valued expression.
Line:16 / Char:18
+$ds = $server.Databases[$database].ExecuteWithResults("$(Echo $1.OpenText().ReadToEnd())")
+
+ CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
June 4, 2014 at 5:35 am
Jeff Moden (6/3/2014)
@Louis Li,I realize that $dt is a loop variable but what did you intend that "dt" stand for? I'd also like to know why the use of $1? Why not give it a more readable name or is that required to be numerically name due to position or???
Also, why any abbreviations? Why not make the code more self documenting with only slightly longer names?
Thanks for pointing out the abbreviations. That's my bad personal habit, I will pay attention to them next time.
How does this handle embedded delimiters?
This script relies on PowerShell cmdlet export-csv to generate csv file, please refer to to MSDN for more details:
http://technet.microsoft.com/en-us/library/ee176825.aspx
Last but not least, I know this was meant to be a short "spackle" style article but a lot of people could end up using something like this. Do you have any performance figures for this method?
I haven't run performance tests on this script.
June 4, 2014 at 5:35 am
it.ma (6/3/2014)
tried the same. (with master.sql in the same directory as .ps1 script)It says
"You cannot call a method on a null-valued expression.
Line:16 / Char:18
+$ds = $server.Databases[$database].ExecuteWithResults("$(Echo $1.OpenText().ReadToEnd())")
+
+ CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Looks like either it couldn't find your server or your database. Double check your connection to the database.
June 4, 2014 at 5:37 am
muthyala_51 (6/3/2014)
Can the script.sql file can have a select statement with joins? or this can be only used to export for just plain tables?
There is no restriction on what SQL script you execute.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply