Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server 2005 - exporting nvarchar(max) data

I would like to run a query and save the results as tab separated file. This is all no problem but in:

Query -> Query Options -> Results -> Text -> Maximum number of characters in each column

I can only select 8192 characters as maximum. This may not be enough. Is there a way to ensure that all characters are included if the column is nvarchar(max)?

Thanks!

Christian

like image 375
cs0815 Avatar asked Jun 09 '11 10:06

cs0815


People also ask

How do I export Nvarchar Max?

Right click on database in Management studio, Tasks => Export Data. Set the destination to a "Flat file destination" and then chose to write the query to export.

What is the max value for Nvarchar SQL?

nvarchar [ ( n | max ) ] n defines the string size in byte-pairs, and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^30-1 characters (2 GB). The storage size is two times n bytes + 2 bytes.

Is Nvarchar 4000 the same as Nvarchar Max?

The answers is: there is no different between nvarchar(7) and nvarchar(4000) in term of performance & storage size. There is an interesting thing is that: if you change nvarchar(7) or nvarchar(4000) to nvarchar(max). There is a difference in term of performance & storage size. Wow, Why is this happen?

Is varchar Max same as varchar 8000?

CHAR, VARCHAR, and VARCHAR(MAX) CHAR columns should be used for columns that vary little in length. String values that vary significantly in length and are no longer than 8,000 bytes should be stored in a VARCHAR column. If you have huge strings (over 8,000 bytes), then VARCHAR(MAX) should be used.


2 Answers

Right click on database in Management studio, Tasks => Export Data. Set the destination to a "Flat file destination" and then chose to write the query to export.

like image 150
Magnus Avatar answered Nov 12 '22 17:11

Magnus


I ususally use Powershell for this kind of stuff.

Here is a script, feel free to adjust for you needs. I'm assuming that your nvarchar(max) does not have line breaks, otherwise tab separated file doesn't make much sense.

##---[ Script Settings ]-------------------------------------------------------------------------------------------------------------
$sqlServer = "localhost"
$targetDbName = "AdventureWorks2008"
$reportName = "c:\result.txt"

##---[ Common Functions ]------------------------------------------------------------------------------------------------------------
function Get-Dataset {
param($sqlQuery, $sqlServer, $sqlCatalog)

  # Setup SQL Connection
  $sqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $sqlConnection.ConnectionString = "Server = $sqlServer; Database = $sqlCatalog; Integrated Security = True"

  # Setup SQL Command
  $sqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $sqlCmd.CommandText = $sqlQuery
  $sqlCmd.Connection = $sqlConnection
  $sqlCmd.CommandTimeout = 0

  # Setup .NET SQLAdapter to execute and fill .NET Dataset
  $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $sqlAdapter.SelectCommand = $sqlCmd
  $dataSet = New-Object System.Data.DataSet

  #Execute and Get Row Count
  $nRecs = $sqlAdapter.Fill($dataSet)
  $sqlConnection.Close();
  $dataSet
}

##---[ Main ]------------------------------------------------------------------------------------------------------------------------

$dataset = Get-DataSet "SELECT * From DatabaseLog" $sqlServer $targetDbName 

$dataset.tables[0].rows |
  #Format-Table -auto | Out-File $reportName -width 100000
  ConvertTo-CSV -Delimiter "`t" -NoTypeInformation| Out-File $reportName

& ($reportName)
like image 20
Andrew Savinykh Avatar answered Nov 12 '22 17:11

Andrew Savinykh