Understanding SQL Server kill statement [for non-admin user] - sql

I am looking into how a database management system performs a "cancel" operation when the cancel button is hit on the query. While it doesn't show what query it runs when it does cancel, here is what it shows in the logs:
2019/01/03 22:09:16:433 MSSQL cancelCurrentQuery failed via spid (54). Error: Msg 6102, Level 14, State 2.
User does not have permission to use the KILL statement.
2019/01/03 22:09:16:433 MSSQL cancelCurrentQuery is going to try via dbcancel.
2019/01/03 22:09:16:533 Cancelling current query (finished).
I assume KILL is trying to do KILL {sid} and it fails to do that because of permissions and then calls back to dbcancal. What is dbcancel? And what would be an example of that from the command line?
Additionally, how is it possible for me to kill the queries that I've issued if I cannot use the kill command?
Msg 6102, Level 14, State 2.
User does not have permission to use the KILL statement. (Line 1)

Related

Syntax for creating an event session in SQL Server

I am attempting to create an event session for my SQL database, as I keep receiving this error when I try and create the session using the UI under the Extended Events -> Sessions folder:
The target, "5B2DA06D-898A-43C8-9309-39BBBE93EBBD.package0.event_file", encountered a configuration error during initialization. Object cannot be added to the event session. (null) (Microsoft SQL Server, Error: 25602)
I am now attempting to create this using SQL commands, but am having trouble with the syntax as the official documentation syntax does not seem to be working. Does anyone know how I would fix this? Here is what I have so far:
CREATE EVENT SESSION CrystalLogsv3
ON SERVER
ADD EVENT sqlos.async_io_requested,
ADD TARGET package0.asynchronous_file_target
(SET filename='https://<name>logs.blob.core.windows.net/<name>/<filename>.xel',
credential = [https://<name>logs.blob.core.windows.net/<name>])
;

LoadRunner - exiting login transaction on failure and perform log off

I'm running a LoadRunner test , upon user failure at login /even at any other transaction it has to fail and execute log off portion of the script.
Note: I have put text check and with textcheck count if the transaction fails( using if condition I have handled it) it then ends transaction with fail status .I would need solution to perform log off also at the point where the if condition fails.
Can anyone share me with an example to execute log off when textcheck fails.
Depends upon your language choice.
Assuming you have the default language of C with your HTTP virtual user, then simply implement a logout function which contains your logout code. Call that function upon failure of your condition. A "return 1;" inside of that if/then conditional will also start a new iteration immediately. "return 0;" goes to a new iteration with respected pacing. "return -1;" kills the virtual user altogether.

Trying to enter Diagnostic connection

SQL Server 2014, SP3 Need to establish diagnostic connection. When I open Mgmt Studio, click on database engine query, and type in admin:MSSQLSERVER (we use the default instance on this box) I get:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'MSSQLSERVER'
Can someone tell me the correct syntax?
I have tried:
admin:servername\MSSQLSERVER
admin:servername
both give same syntax error near whatever is first typed after the colon.
Issue resolved:
When I click on the Database Engine Query button, in the login screen that pops up, put ADMIN: in front of the server name, and connect. This worked for me.

Why is my Deployment script causing me errors?

I am working on a project in visual studio 2012. Recently, I added a database project to the solution. The database already existed before I added it to the solution and everything worked fine.
Now, however, when I try to run the application I get errors. The errors are being caused by a computer-generated file called [database name].sql. At the top of the tile, it reads:
/*
Deployment script for [the database name here]
This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/
This file gets re-created every time the application runs. The errors that occur appear to be syntax errors. I cannot fix them because any changes I make to the file are irrelevant because a new file gets generated with each run and the errors re-appear.
I tried looking into this more online but had trouble. This is all rather new to me.
Here are some of the errors being created:
GO
:setvar DatabaseName "(the database name is here)"
which gives me three errors that read:
Error 88 SQL80001: Incorrect syntax near ':'.
Error 89 SQL80001: 'DatabaseName' is not a recognized option.
Error 90 SQL80001: Incorrect syntax near '"(the database name is here in the code)"'.
Also, there is a line of code that reads:
CREATE USER [(the domain)\(the username)] FOR LOGIN [(the domain)\(the username)];
GO
which gives the following error:
Error 119 SQL72014: .Net SqlClient Data Provider: Msg 15401, Level 16, State 1, Line 1 Windows NT user or group '(the domain)\(the username)' not found. Check the name again.
From the errors you have posted it looks like there are two issues:
Windows NT user or group '(the domain)\(the username)' not found. - The user being used to access the database doesn't exist. As it's a Windows user I'm guessing that it uses the current user's credentials.
Make sure that your instance of SQL can accept Windows logins and that you (and anyone else building the software) has the necessary access rights.
'DatabaseName' is not a recognized option. - This is more than likely also caused by the first issue, but double check that the database exists.
The error message caught me out recently as it did not relate to the cause of my problem at all. It turns out my SQL script was incorrectly written (for my case - I forgot to add IDENTITY (1,1) to my PK column) to begin with.
Moral of the story for me was to test out the SQL data file in SSMS first.

How can you cancel a SQL Server execution process programmatically

Let's say you have execute the following (long running) process from your code:
int processID = DB.Execute(SQL); //some long running sql statement
Is there a way to programatically call SQL Server to cancel the process if it is taking too long (kind of like hitting the "Stop" button in QueryAnalyzer)?
//cancel the process if it is taking too long
DB.Execute("sp_CancelProcess #ProcessID=" + processID);
use KILL with the process id:
KILL 53;
Just be aware that you can't kill your own spid, you need to create another connection and then kill the spid from the
if you try to kill your own SPID you will get the following error
Server: Msg 6104, Level 16, State 1, Line 1
Cannot use KILL to kill your own process.
Kill #Spid
note that this is a last effort. you should just close the connection on the client to kill the process.
sp_who2
List of process displays
Find you SPID
Kill SPID
If error message you can't kill your own process
See where the process rund in your management studio and stop it.
Or close current connection and reopen and try to kill now.
You have to run your query asynchronously, as follows:
SqlConnection _anotherConnection;
SqlCommand _anotherCommand;
IAsyncResult _anotherCommandStarted;
_anotherCommand = _anotherConnection.CreateCommand();
_anotherCommand.CommandText = string.Format("SET DEADLOCK_PRIORITY HIGH; BEGIN TRANSACTION; {0};", hookCommand);
_anotherCommand.CommandType = CommandType.Text;
_anotherCommand.ExecuteNonQuery();
_anotherCommand.CommandText = "UPDATE Data.Hook1 SET i=1-i";
_anotherCommandStarted = _anotherCommand.BeginExecuteNonQuery();
To cancel the command, run this:
_anotherCommand.EndExecuteNonQuery(_anotherCommandStarted);