Full execution history of a stored procedure - sql-server

I am wondering how to get the execution_time for all executions for a specific stored procedure. (using Microsoft SQL Server 2016)
I know that via dm_exec_procedure_stats I get information about the last_execution_time and execution_count but I am interested in the execution_time of every execution (of one stored procedure) which got counted for the execution_count.
What I get is something like this (as an example):
| name | database_id | execution_count | last_execution_time |
------------------------------------------------------------------
| sp_name1 | db_id1 | 23 | 11.09.2019 hh:mm:ss |
| sp_name2 | db_id1 | 12 | 09.09.2019 hh:mm:ss |
| sp_name3 | db_id2 | 3456 | 11.09.2017 hh:mm:ss |
So basically I want a query to get a table which has in one column the name of the procedure and in the other the execution times of this procedure such that the number of rows should equal the execution_count from the procedure.
What I want is something like this:
| name | database_id | execution_time |
------------------------------------------------
| sp_name1 | db_id1 | 11.09.2019 hh:mm:ss |
| sp_name1 | db_id1 | dd.mm.yyyy hh:mm:ss |
| sp_name1 | db_id1 | dd.mm.yyyy hh:mm:ss |
| ... | ... | ... |
which should have 23 rows.

To get the execution history of a stored procedure you can use :
Profiler :
SQL Server >Tools>SQL Server Profiler
Navigate to File > New Trace.
Trace Properties > click on the Events Selection tab > select the SP:Completed counter in the Stored Procedures grouping of counters >click on the General Tab to save the results (table / file).>Columns Filters: you can capture the database that you are working on> Finally Run

Related

How to timestamp data copied into a postgresql database

I have a shell script pulling data from a server into a postgresql table.
df -g | awk 'BEGIN{OFS=","}NR>1{$1=$1; print}' > /data/metric.csv
psql -h localhost -d metrics -U postgres -c "copy tablename from STDIN with delimiter as ',';" < /data/metric.csv
Displays as:
filesystem | gb_blocks | free | %used | iused | %iused | mounted_on
/dev/hd2 | 16.75 | 12.60 | 25% | 79098 | 3% | /usr
/dev/hd9var | 8.00 | 6.00 | 25% | 11965 | 1% | /var
/dev/hd3 | 36.75 | 18.83 | 49% | 5614 | 1% | /tmp
/dev/hd1 | 3.25 | 3.11 | 5% | 674 | 1% | /home
/dev/hd11admin | 0.25 | 0.25 | 1% | 16 | 1% | /admin
/proc | - | - | - | - | - | /proc
I'm working with Postgresql on an Ubuntu OS and pulling the info from an AIX server. I'd like to add a column with a timestamp for every time new data is added to the table because right now it just all blends together. I've tried to add another column for timestamp and give it a timestamp value but the timestamp isn't in the csv file and I'm not sure how to add it either. I appreciate an help I can get to solve this.
Create table and add date column with Default value like current_Date/now() ).
CREATE TABLE IF NOT EXISTS metrics
(
filesystem text ,
gb_blocks text ,
free text ,
per_used text ,
iused text ,
per_iused text ,
mounted_on text ,
load_dttm timestamp without time zone DEFAULT now()
);
mention columnswith table while loading data as below command
psql -h localhost -d metrics -U postgres -c "copy metrics(filesystem,gb_blocks,free,per_used,iused,per_iused,mounted_on) from STDIN with delimiter as ',';" < /data/metric.csv

Convert Excel Exponential Format back to its text in SQL Server 2008 R2

First off I am have a constraint the my solution must work from SQL Server 2008 R2
The problem that I'm trying to solve is that Excel converts the text value '002E9' to 2.00E+09. The task is to pass the original value '002E9' as text into a CSV file.
I have been passed a SSIS solution by a developer that has a the conversion as a SQL function. They have used
SELECT FORMAT(CAST(2.00E+09 AS FLOAT),'0E0');
This is fine in 2012 and above but does not work in SQL Server 2008 R2.
Is there a simple alternative? I'm happy to abandon SQL for a SSIS script if that's the best advice.
FORMAT doesn't exist in SQL Server 2008; but it's use is best avoided any way; it's an awfully slow function.
You can use CONVERT and the style 0 though:
SELECT REPLACE(CONVERT(varchar(10),CAST(2.00E+09 AS float),0),'+','');
This won't, however, give exactly the same format, and would return '2e009'. Based on the fact that you use the value '0E0' for the FORMAT function though (which would return '2E9' for your example value), I assume this is permissible.
Based upon the post Larnu made I arrived at this (note the REPLICATE function for getting the correct format from the stripped down string):
DECLARE #INPUTS AS table
(input_val varchar(100))
INSERT INTO #INPUTS
VALUES
('00923'),('00234'),('00568'),('00123'),('2.00E+09' ),('2.00E+34' ),('00RT1'),('001TL')
SELECT input_val
,REPLACE(REPLACE(REPLACE(input_val,'+',''),'0',''),'.','') paired_value
,REPLICATE('0',5-LEN(REPLACE(REPLACE(REPLACE(input_val,'+',''),'0',''),'.','')))
+REPLACE(REPLACE(REPLACE(input_val,'+',''),'0',''),'.','')+';' Converted_value
FROM #INPUTS
The results:
+-----------+--------------+-----------------+
| input_val | paired_value | Converted_value |
+-----------+--------------+-----------------+
| 00923 | 923 | 00923; |
| 00234 | 234 | 00234; |
| 00568 | 568 | 00568; |
| 00123 | 123 | 00123; |
| 2.00E+09 | 2E9 | 002E9; |
| 2.00E+34 | 2E34 | 02E34; |
| 00RT1 | RT1 | 00RT1; |
| 001TL | 1TL | 001TL; |
+-----------+--------------+-----------------+
Confirms the approach.
Thanks Larnu.

Database not created

I installed postgresql and tried to create new database but couldn't succeed in creating database:
Server [localhost]:
Database [postgres]:
Port [5433]:
Username [postgres]:
psql (9.2.17)
WARNING: Console code page (850) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# createdb gps_heatmap
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------------------+---------------------+-----------------------
database4 | postgres | UTF8 | English_Canada.1252 | English_Canada.1252 |
postgres | postgres | UTF8 | English_Canada.1252 | English_Canada.1252 |
template0 | postgres | UTF8 | English_Canada.1252 | English_Canada.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_Canada.1252 | English_Canada.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres-#
Database is not created . I couldnt solve this problem. Help needed.
You are missing ; use
createdb gps_heatmap;

Robot Framework data-driven automation testing: Can data derived from a database be used as a data source for a test template?

I am familiar with using template keywords in data-driven Robot Framework testing and know that external sources of data such as text files and csv files can be used to provide test data. However, the organisation I work for wants to use data held in a database as a source for test case data. Does anybody know if this is possible? I have searched Stack Exchange, Stack Overflow and other resources but cannot find an answer or any examples.
Here is an example of the data-driven approach I am familiar just to give you an idea of where we are now.
*** Settings ***
Library Selenium2Library
Library AFRCLibrary
| Test Template | Suspend Region
*** Variables ***
*** Test Cases ***
| Pillar 1 BPS 2019 Suspend Region | Pillar 1 | 2019 | BPS | BPS Region 1 | Pillar 1 BPS 2019 Suspend Region Comments |
| Pillar 2 FGS 2018 Suspend Region | Pillar 2 | 2018 | FGS | FGS Region 1 | Pillar 2 FGS 2018 Suspend Region Comments |
*** Keywords ***
| Suspend Region
| | [Arguments] | ${pillar} | ${year} | ${scheme} | ${region} | ${comments} |
| | Futures Open Application | http://ags125p01:8080/operationalsite/login | ff |
| | FuturesPublicsiteWM | root | gtn | http://ags125p01:8080/operationalsite/futures/maintain_budget |
| | Select Pillar | ${pillar} | ${year} |
| | Select Scheme | ${scheme} |
| | View |
| | Suspend And Confirm | ${region} | ${comments} |
| | Futures Close Application |
| |
Unfortunately, the use of test templates more-or-less require that the data is hard-coded in the test case. However, the test template is not much more than a wrapper around a for loop. You could do something like this:
| | ${database_rows}= | Run sql query
| | ... | Select * from the_database where ...
| |
| | :FOR | ${row} | IN | #{database_rows}
| | | Suspend Region | #{row}
Of course, this requires that you write the "Run sql query" keyword or an equivalent to fetch the data.
The downside of this is that all of the permutations are considered a single test case with multiple keywords, versus multiple test cases with a single keyword.
If you want to have one test case per row in a database, you could write a script that does the query, generates a test suite file using the results of the query, and then runs pybot on the generated file.

How to change default language for SQL Server?

Now when I query
SELECT ##language
it gets 'us_english'. But I need russian.
I can't use SET LANGUAGE russian for every query.
I need to set it by default (for all new sessions).
Using SQL Server Management Studio
To configure the default language option
In Object Explorer, right-click a server and select Properties.
Click the Misc server settings node.
In the Default language for users box, choose the language in which Microsoft SQL Server should display system messages.
The default language is English.
Using Transact-SQL
To configure the default language option
Connect to the Database Engine.
From the Standard bar, click New Query.
Copy and paste the following example into the query window and click Execute.
This example shows how to use sp_configure to configure the default language option to French
USE AdventureWorks2012 ;
GO
EXEC sp_configure 'default language', 2 ;
GO
RECONFIGURE ;
GO
Configure the default language Server Configuration Option
The 33 languages of SQL Server
| LANGID | ALIAS |
|--------|---------------------|
| 0 | English |
| 1 | German |
| 2 | French |
| 3 | Japanese |
| 4 | Danish |
| 5 | Spanish |
| 6 | Italian |
| 7 | Dutch |
| 8 | Norwegian |
| 9 | Portuguese |
| 10 | Finnish |
| 11 | Swedish |
| 12 | Czech |
| 13 | Hungarian |
| 14 | Polish |
| 15 | Romanian |
| 16 | Croatian |
| 17 | Slovak |
| 18 | Slovenian |
| 19 | Greek |
| 20 | Bulgarian |
| 21 | Russian |
| 22 | Turkish |
| 23 | British English |
| 24 | Estonian |
| 25 | Latvian |
| 26 | Lithuanian |
| 27 | Brazilian |
| 28 | Traditional Chinese |
| 29 | Korean |
| 30 | Simplified Chinese |
| 31 | Arabic |
| 32 | Thai |
| 33 | Bokmål |
John Woo's accepted answer has some caveats which you should be aware of:
Default language setting of a T-SQL session in SQL Server Management Studio(SSMS) is inherited/overriden from/by Default language setting of the user login used to initiate the session instead. A new tab in SSMS creates a new T-SQL session. SQL Server instance level setting does not control the Default language setting of T-SQL session directly.
Changing Default language setting at SQL Server instance level has no effect on the Default language setting of the already existing SQL Server logins. It is meant to be inherited only by the new user logins that we create after changing the instance level setting. So don't be surprised if you changed the Default language setting at SQL Server instance level but it didn't take effect for your user account. It is as per design.
So, there is an intermediate configuration level between SQL Server instance level and the T-SQL session level. It is called user login level. You can use this intermediate level configuration to control the Default language setting for T-SQL session without disrupting the SQL Server instance level settings.
SQL Server Instance level setting
|
V
User login level setting
|
V
T-SQL Query Session level setting
This intermediate level setting is very helpful in case you want to set Default language setting to some value for all new T-SQL sessions(tabs in SSMS) belonging to some specific user.
We can change the Default language setting of the target user login as per this link. You can also achieve it from SSMS console e.g. we can change the Default language setting from the properties window of sa user in SQL Server via SSMS (Refer screenshot):
Note: Also, please remember that changing the setting at user login level will not have any effect on the Default language setting of already active T-SQL sessions (tabs in SSMS) created with that user login. It will affect only the new sessions which will be created after changing the setting.
Please try below:
DECLARE #Today DATETIME;
SET #Today = '12/5/2007';
SET LANGUAGE Italian;
SELECT DATENAME(month, #Today) AS 'Month Name';
SET LANGUAGE us_english;
SELECT DATENAME(month, #Today) AS 'Month Name' ;
GO
Reference:
https://learn.microsoft.com/en-us/sql/t-sql/statements/set-language-transact-sql
If you want to change MSSQL server language, you can use the following QUERY:
EXEC sp_configure 'default language', 'British English';

Resources