Posts

Showing posts with the label sql

Running SQLCMD in PowerShell via SQL Server Agent

Running SQLCMD in PowerShell via SQL Server Agent Learn how to execute a SQL query via SQL Server Agent using PowerShell and sqlcmd . This method ensures UTF-8 compatibility and proper handling of non-English characters. 1️⃣ Save Your SQL Query in a File First, create a SQL file with the query you want to execute. Save it as C:\Scripts\ExportQuery.sql . SET NOCOUNT ON; SELECT TOP 100 * FROM [YourDatabase].[dbo].[YourTable]; 2️⃣ Create a PowerShell Script Now, create a PowerShell script to execute the query and export the results as a CSV file. sqlcmd -S YourServer -E -i "C:\Scripts\ExportQuery.sql" -s "," -W -h -1 | Out-File "C:\Exports\ExportedData.csv" -Encoding UTF8 3️⃣ Configure SQL Server Agent Follow these steps to create a SQL Server Agent job: Open SQL Server Management Studio (SSMS). ...

Finding Long Running Queries in Oracle Database

            It could be tricky to detect when your database slows down for no reason. At first, you should check system resources (CPU - RAM usage, network etc.) and if find some bottlenecks, then go deeper. ADDM report should be useful for detecting most problem causes.           But it is also probable that some queries may be using server resources excessively. How to detect them? Well, there is one simple query to check that: select s.sql_text,sl.sid,sl.target||'-'||sl.opname Target,sl.totalwork,sl.sofar,sl.time_remaining Seconds_remaining,sl.elapsed_seconds,sl.sql_id,sl.username from v$session_longops sl,v$sql s,v$session se where s1.sid=se.sid and se.sql_id=s.sql_id and totalwork!=0 and sofar<>totalwork;