sql-server-2016

how to detect sql server timeout from .NET application without using catch Exception

半腔热情 提交于 2019-12-02 18:36:30
In my current application, i am performing an update by invoking T-SQL Update command. The problem is when the same record is locked by other users at that time. At .NET application, the application will wait until SQL Server timeout, then it will throw the SqlException timeout. Is it possible to perform a check first whether a particular record is locked by other process rather than catching the exception ? No, not really. The standard way is to use try/catch and handle SqlException Number 1205 (deadlock victim), and retry your query: try { // do stuff... } catch (SqlException sqlEx) { switch

Why is 199.96 - 0 = 200 in SQL?

為{幸葍}努か 提交于 2019-12-02 17:22:14
I have some clients getting weird bills. I was able to isolate the core problem: SELECT 199.96 - (0.0 * FLOOR(CAST(1.0 AS DECIMAL(19, 4)) * CAST(199.96 AS DECIMAL(19, 4)))) -- 200 what the? SELECT 199.96 - (0.0 * FLOOR(1.0 * CAST(199.96 AS DECIMAL(19, 4)))) -- 199.96 SELECT 199.96 - (0.0 * FLOOR(CAST(1.0 AS DECIMAL(19, 4)) * 199.96)) -- 199.96 SELECT 199.96 - (CAST(0.0 AS DECIMAL(19, 4)) * FLOOR(CAST(1.0 AS DECIMAL(19, 4)) * CAST(199.96 AS DECIMAL(19, 4)))) -- 199.96 SELECT 199.96 - (CAST(0.0 AS DECIMAL(19, 4)) * FLOOR(1.0 * CAST(199.96 AS DECIMAL(19, 4)))) -- 199.96 SELECT 199.96 - (CAST(0.0

SQL Server 2016 How to use a simple Regular Expression in T-SQL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 10:50:31
I have a column with the name of a person in the following format: "LAST NAME, FIRST NAME" Only Upper Cases Allowed Space after comma optional I would like to use a regular expression like: [A-Z]+,[ ]?[A-Z]+ but I do not know how to do this in T-SQL. In Oracle, I would use REGEXP_LIKE, is there something similar for SQL Server 2016? I need something like the following: UPDATE table SET is_correct_format = 'YES' WHERE REGEXP_LIKE(table.name,'[A-Z]+,[ ]?[A-Z]+'); First, case sensitivity depends on the collation of the DB, though with LIKE you can specify case comparisons. With that... here is

Select last value from Json array

[亡魂溺海] 提交于 2019-12-02 08:59:44
问题 I need to get the last item in an array from a JSON format. I have this JSON. @json = N'{ "solution": "xxxxxxxxxxxxxxxxxxxxx", "options": [ { "choice_id": 205073, "choice": "aaaa" }, { "choice_id": 205074, "choice": "bbbb" }, { "choice_id": 205075, "choice": "cccc" }, { "choice_id": 205076, "choice": "dddd" } ], }' And I would like to get @json = N'{ "solution": "xxxxxxxxxxxxxxxxxxxxx", "options": { "choice_id": 205076, "choice": "dddd" } } How can I achieve that result? 回答1: You can try this

MSSQL 2016 Server: All databases marked Recovery Pending state

点点圈 提交于 2019-12-02 08:53:27
Tonight I had run into this issue where all my databases on our production server were in a Recovery Pending state after doing a Windows (server 2016) Update, an update to my Redgate SQL Backup software and restarting the server. Looking at the logs I found that all of the databases were not accessible with an "Access denied" error. So I figured it was something to do with the user under which the MSSQLSERVER service was running. So I explicitly set permissions to the data and log folders for that user and restarted SQL Server service and all is fine again. HOWEVER, this server has been

Incorrect Syntax Near GO, T-SQL EXEC()

独自空忆成欢 提交于 2019-12-02 06:12:38
问题 I am using the following script: DECLARE @dbName NVARCHAR(20) = 'ABC'; EXEC ( N' USE ' + @dbName + ' GO -- Create Schema CREATE SCHEMA meta GO -- Create Log Table CREATE TABLE meta.LogAudit( [EventDate] [datetime] NOT NULL DEFAULT (getdate()), [EventType] [nvarchar](64) NULL ) GO ' ); but it throws me the following error. Msg 111, Level 15, State 1, Line 5 'CREATE SCHEMA' must be the first statement in a query batch. Msg 102, Level 15, State 1, Line 22 Incorrect syntax near 'GO'. Why is that?

Complex SQL Update on 2 interdependent tables

时光总嘲笑我的痴心妄想 提交于 2019-12-02 05:27:53
问题 I have a database with several tables keeping track of phone calls/sms/data and allowances and I'm trying to work out if it is possible to allocate calls to allowances without resorting to cursors, but I can't figure out a way of structuring the SQL to do so. I don't have any useful SQL from my attempts as I can't seem to get my head around how to approach it! The problem is that to me this seems like an inherently iterative process and I can't work out if there is a sensible way to translate

Select last value from Json array

北慕城南 提交于 2019-12-02 03:26:21
I need to get the last item in an array from a JSON format. I have this JSON. @json = N'{ "solution": "xxxxxxxxxxxxxxxxxxxxx", "options": [ { "choice_id": 205073, "choice": "aaaa" }, { "choice_id": 205074, "choice": "bbbb" }, { "choice_id": 205075, "choice": "cccc" }, { "choice_id": 205076, "choice": "dddd" } ], }' And I would like to get @json = N'{ "solution": "xxxxxxxxxxxxxxxxxxxxx", "options": { "choice_id": 205076, "choice": "dddd" } } How can I achieve that result? You can try this DECLARE @json NVARCHAR(MAX) = N'{ "solution": "xxxxxxxxxxxxxxxxxxxxx", "options": [ { "choice_id": 205073,

Deploy SSAS Project using TFS Command Line

你说的曾经没有我的故事 提交于 2019-12-02 01:32:02
问题 How do I deploy an SSAS Cube project from TFS 2015? For regular database projects, it is sqlpackage.exe /publish with publish profile. What is the command line argument to auto deploy SSAS Project Model into a server? We are currently using SQL Server 2016 Enterprise. 回答1: You must use Microsoft.AnalysisServices.Deployment.exe located in C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio Syntax Microsoft.AnalysisServices.Deployment [ASdatabasefile] {[/s[:logfile]] | [

Incorrect Syntax Near GO, T-SQL EXEC()

你离开我真会死。 提交于 2019-12-02 01:12:11
I am using the following script: DECLARE @dbName NVARCHAR(20) = 'ABC'; EXEC ( N' USE ' + @dbName + ' GO -- Create Schema CREATE SCHEMA meta GO -- Create Log Table CREATE TABLE meta.LogAudit( [EventDate] [datetime] NOT NULL DEFAULT (getdate()), [EventType] [nvarchar](64) NULL ) GO ' ); but it throws me the following error. Msg 111, Level 15, State 1, Line 5 'CREATE SCHEMA' must be the first statement in a query batch. Msg 102, Level 15, State 1, Line 22 Incorrect syntax near 'GO'. Why is that? -- Edit: This answer seems to be answering my question: dynamic sql error: 'CREATE TRIGGER' must be