Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Administration - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 6525

Powershell works differently in SQL Server 2014

$
0
0
Hi,I had a job to script my database on the old SQL 2008 R2 server:[code="vb"]#DECLARE TIMESTAMP FOR THE FILES$timestamp = Get-Date -Format yyyy-MM-dd#SCRIPTSL SQLSERVER:\SQL\"MyServer\DEFAULT"\Databases\"MyDB"\StoredProcedures$so = new-object Microsoft.SqlServer.Management.Smo.ScriptingOptions$so.IncludeIfNotExists = 1Invoke-sqlcmd -Database tempdb -Query "select name from MyDB.sys.objects (nolock) where [type] = 'P' and name not like 'sp_MS%'" | foreach {$procs +=,$_.name}gci | %{if ($procs -contains $_.Name) {$Proc = $_.Name; $_.Script($so) + " GO " | out-file -filepath "C:\temp\08_SPs $timestamp.sql" -Append}}[/code]On SQL 2008R2 I was getting something like this:[code="sql"]SET ANSI_NULLS ONSET QUOTED_IDENTIFIER OFFIF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aspnet_AnyDataInTables]') AND type in (N'P', N'PC'))BEGINEXEC dbo.sp_executesql @statement = N'[b]CREATE PROCEDURE [dbo].[aspnet_AnyDataInTables][/b] @TablesToCheck intAS...[/code]Now I am getting the script below and it fails to run due to the missing "GO" statement before the "Alter" command. Is this a bug? How do I work this around? Is there a way to put the "GO" statement before "Alter" command?Thanks.[code="sql"]IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[aspnet_AnyDataInTables]') AND type in (N'P', N'PC'))BEGINEXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[aspnet_AnyDataInTables] AS' END[b]ALTER PROCEDURE [dbo].[aspnet_AnyDataInTables][/b] @TablesToCheck int[/code]

Viewing all articles
Browse latest Browse all 6525

Trending Articles