If i'm on c:\Dog and want to move to d:\Cat then simply issuing the following command doesn't cut the mustard:
C:\Dog\cd d:\Cat
Instead, I need to issue it with the /D command
C:\Dog\cd /D d:\Cat
CREATE TABLE #T (i INT)
INSERT INTO #T SELECT 1
INSERT INTO #T SELECT 2
INSERT INTO #T SELECT 3
BEGIN TRAN
INSERT INTO #T SELECT 4
INSERT INTO #T SELECT 5
INSERT INTO #T SELECT 6
ROLLBACK TRAN
-- only 3 rows
SELECT * FROM #T
DECLARE @t TABLE (i INT)
INSERT INTO @t SELECT 1
INSERT INTO @t SELECT 2
INSERT INTO @t SELECT 3
BEGIN TRAN
INSERT INTO @t SELECT 4
INSERT INTO @t SELECT 5
INSERT INTO @t SELECT 6
ROLLBACK TRAN
-- 6 rows!!
SELECT * FROM @t
-- set up our test dataset of all FIFA World Cup Winners
CREATE TABLE dbo.WorldCupWinners (Yr DATE, Winner NVARCHAR(255))
INSERT INTO dbo.WorldCupWinners VALUES ('19300101', 'Uruguay')
INSERT INTO dbo.WorldCupWinners VALUES ('19340101', 'Italy')
INSERT INTO dbo.WorldCupWinners VALUES ('19380101', 'Italy')
INSERT INTO dbo.WorldCupWinners VALUES ('19500101', 'Uruguay')
INSERT INTO dbo.WorldCupWinners VALUES ('19540101', 'Germany')
INSERT INTO dbo.WorldCupWinners VALUES ('19580101', 'Brazil')
INSERT INTO dbo.WorldCupWinners VALUES ('19620101', 'Brazil')
INSERT INTO dbo.WorldCupWinners VALUES ('19660101', 'England')
INSERT INTO dbo.WorldCupWinners VALUES ('19700101', 'Brazil')
INSERT INTO dbo.WorldCupWinners VALUES ('19740101', 'Germany')
INSERT INTO dbo.WorldCupWinners VALUES ('19780101', 'Argentina')
INSERT INTO dbo.WorldCupWinners VALUES ('19820101', 'Italy')
INSERT INTO dbo.WorldCupWinners VALUES ('19860101', 'Argentina')
INSERT INTO dbo.WorldCupWinners VALUES ('19900101', 'Germany')
INSERT INTO dbo.WorldCupWinners VALUES ('19940101', 'Brazil')
INSERT INTO dbo.WorldCupWinners VALUES ('19980101', 'France')
INSERT INTO dbo.WorldCupWinners VALUES ('20020101', 'Brazil')
INSERT INTO dbo.WorldCupWinners VALUES ('20060101', 'Italy')
INSERT INTO dbo.WorldCupWinners VALUES ('20100101', 'Spain');
-- we only want to get the distinct countries that have won
-- along with their first triumph which will be the FirstWin = 1
WITH wins
AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Winner ORDER BY Yr ASC) AS FirstWin
FROM dbo.WorldCupWinners
)
DELETE
FROM wins -- note how we're deleting from the CTE and NOT the base table
WHERE FirstWin > 1
GO
-- And hey presto, we have our desired output
SELECT *
FROM dbo.WorldCupWinners
/*
Yr Winner
1930-01-01 Uruguay
1934-01-01 Italy
1954-01-01 Germany
1958-01-01 Brazil
1966-01-01 England
1978-01-01 Argentina
1998-01-01 France
2010-01-01 Spain
*/
param($sqlserver)
$filePath = "C:\SQLScripts\Tables_" + $sqlserver + "_"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-null
$srv = new-object "Microsoft.SqlServer.Management.Smo.Server" $sqlserver
$options = new-object "Microsoft.SqlServer.Management.Smo.ScriptingOptions"
$options.AppendToFile = $true
$options.ToFileOnly = $true
$options.ClusteredIndexes = $true
$options.NonClusteredIndexes = $true
$options.DriAll = $true
foreach($db in $srv.Databases | where {$_.IsSystemObject -eq $false})
{
$options.FileName = $filePath + $db.Name + ".sql"
$tables = $db.Tables
foreach($table in $tables | where {$_.IsSystemObject -eq $false})
{
$table.Script($options)
}
}
/* add this crazy stuff in so i can use syntax highlighter