Uncategorized

Export Large Query Result to File (PowerShell)

Export query output to file using PowerShell. Suitable for scenarios when large result set or single column value cannot be exported using SSMS. $query = “SELECT 1 AS COL1” $connectionString = “Server=***;Database=***;Integrated Security=false;User=***;Password=***” $outputFile = “/***/export.json” $connection = New-Object System.Data.SqlClient.SqlConnection $connection.ConnectionString = $connectionString $command = $connection.CreateCommand() $command.CommandText = $query $command.CommandTimeout…

Read more
Uncategorized

Tally Table

Create Tally table and generate 100 million numbers. Change the CROSS JOIN part to limit the number of values generated. CREATE TABLE [Tally] ( [N] [INT] NOT NULL PRIMARY KEY ) GO ;WITH cte(N) AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a (n) CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0))…