Closed Gincules closed 1 year ago
that's not a Query, it's a batch, and with savepoints also. Invoke-DbaQuery is not the right tool for the job.
sorry I forgot to mention that this only fails using wildcard '%' for the @command
part.
@command=N'IF OBJECTPROPERTY(OBJECT_ID(N''sp_MyGreatProcedure''), ''IsProcedure'') = 1
DROP PROCEDURE sp_MyGreatProcedure;
GO
CREATE PROCEDURE dbo.sp_MyGreatProcedure
(
@Counter NVARCHAR(128) = N''%''
)
AS
BEGIN;
SELECT *
FROM [dbo].[PerformanceMonitorData]
WHERE [Counter] LIKE @Counter
ORDER BY [Counter], [CaptureDate]
END;
GO',
for other queries (example: @command=N'SELECT @@VERSION',
or @command=N'EXEC master.sys.sp_MSforeachdb @command; GO'
) it's executed without any issue.
I had the same issue running a create agent job - If the step command is split over multiple lines the query fails. With
@command=N'exec usp_SweepStockReservations
Go
',
I get the following error.
WARNING: [10:50:55][Invoke-DbaQuery] [mnp-kammac-sand] Failed during execution | Unclosed quotation mark after the character string 'exec usp_SweepStockReservationstockReservations
'.
Incorrect syntax near 'exec usp_SweepStockReservations
'.
that's not a Query, it's a batch, and with savepoints also. Invoke-DbaQuery is not the right tool for the job.
What is the right tool for the job?
If you are wanting to execute a script file as-is in your example use sqlserver/Invoke-SqlCmd
module. dbatools/Invoke-DbaQuery
will not support running files with that syntax in them.
Verified issue does not already exist?
I have searched and found no existing issue
What error did you receive?
Steps to Reproduce
command executed:
Agent_Jobs.sql
Please confirm that you are running the most recent version of dbatools
dbatools version: 1.1.145: syntax error (above)
SSMS: Commands completed successfully. Completion time: 2023-03-29T13:59:50.4673509+02:00
PowerShell Module SqlServer: Invoke-Sqlcmd -ServerInstance MyComputer.domain.net -Database msdb -InputFile .\Agent_Jobs.sql -Verbose OK - created
Other details or mentions
Invoke-DbaQuery -File does not work trying to create the above SQL Agent job from file (reduced to minimum I receive the error).
What PowerShell host was used when producing this error
Windows PowerShell (powershell.exe)
PowerShell Host Version
PSVersion: 5.1.20348.1366 PSEdition: Desktop PSCompatibleVersions: {1.0, 2.0, 3.0, 4.0...} BuildVersion: 10.0.20348.1366 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1
SQL Server Edition and Build number
Microsoft SQL Server 2022 (RTM-CU2) (KB5023127) - 16.0.4015.1 (X64) Feb 27 2023 15:40:01 Copyright (C) 2022 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2022 Standard 10.0 (Build 20348: ) (Hypervisor)
.NET Framework Version
Client: 4.8.04161 Full: 4.8.04161 Client: 4.0.0.0