I am exploring powershell to run the following SQL script stored in my machine with name "TestSQLScript.sql"
USE TestDB
GO
SELECT TOP (1000) [DocId]
,[DocumentInfo]
FROM [TestDB].[dbo].[Docs]
INSERT INTO WorkDocs
( [DocId]
,[DocumentInfo])
SELECT [DocId]
,[DocumentInfo]
FROM [TestDB].[dbo].[Docs]
SELECT TOP (1000) [DocId]
,[DocumentInfo]
FROM [TestDB].[dbo].[WorkDocs]
When I use the following command it executed well.
invoke-sqlcmd -inputfile "D:\TestSQLScript.sql" -serverinstance "MYPC\MSSQLSERVER2019" -Username "sa" -Password "***" -database "master"
When I am running the following it through error while accessing table WorkDocs.
invoke-DbaQuery -SQLInstance "MYPC\MSSQLSERVER2019" -sqlcredential $Cred -File "D:\TestSQLScript.sql" -database "master"
In the initial finding it is noticed that tables referenced as [TestDB].[dbo].[WorkDocs]
are accessible, and table referenced without [TestDB].[dbo]
is not accessible at Insert statement, while I have already used the database reference on top in the SQL file "TestSQLScript.sql"
USE TestDB
GO
I can not reference all the tables with [TestDB].[dbo] as I have hundreds of script and there are multiple database references in each SQL script. Can you suggest how can I resolve this?
In addition to this How can I get the result count for each SQL query in the file. I have Multiple Select, Insert, Update and truncate SQL command in one SQL file and I want to log each execution result. Please refer to below for more.
- For Select Statements - 1000 Records fetched
- For Insert Statements - 100 rows inserted into 'Table Name'
- For Update Statements - 50 rows affected in 'Table Name'
- For Truncate Statements - 'Table Name' has been truncated.