242 questions with SQL Server | SQL Server Transact-SQL tags

Sort by: Updated
1 answer

Where in error

Hi everybody, I cannot find an error in a simple statement: DECLARE @myrowid INT; DECLARE SplitProviderCursor CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY FOR SELECT rowid , npi FROM #tmp_npi_list; OPEN…

SQL Server | SQL Server Transact-SQL
asked 2025-10-26T15:20:31.68+00:00
Naomi Nosonovsky 8,771 Reputation points
answered 2025-10-26T15:51:28.6066667+00:00
Azizkhon Ishankhonov 845 Reputation points
0 answers

Where is an error?

Hi everybody, This simple code produces an error and I couldn't find it: DECLARE @myrowid INT; DECLARE SplitProviderCursor CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY FOR SELECT rowid , npi FROM #tmp_npi_list; OPEN…

SQL Server | SQL Server Transact-SQL
asked 2025-10-26T15:23:08.2333333+00:00
Naomi Nosonovsky 8,771 Reputation points
edited the question 2025-10-26T15:43:20.6966667+00:00
Naomi Nosonovsky 8,771 Reputation points
1 answer

How to solve the occasional timeout issue?

I aim to identify the cause and address the issue where the SQL execution time of occasional program calls exceeds the program's timeout threshold within a certain period, such as one day, after a timeout occurs. May I ask how I should proceed? I have a…

SQL Server | SQL Server Transact-SQL
asked 2025-09-05T07:45:01.7866667+00:00
博雄 胡 685 Reputation points
commented 2025-10-26T15:21:33.4966667+00:00
Singam shetty Sri ganesh 80 Reputation points Microsoft External Staff Moderator
0 answers

How to signing EKM providers for MsSQL Servers on Linux

getting error Failed to get verify Authenticode signature on DLL while creating cryptographic provider on SQL server on Linux need solution

SQL Server | SQL Server Transact-SQL
asked 2025-10-24T04:01:01.9066667+00:00
Pradip Chahande 0 Reputation points
commented 2025-10-24T16:46:55.75+00:00
Lakshmi Narayana Garikapati 470 Reputation points Microsoft External Staff Moderator
1 answer

Implementing User-Defined Sort via Trigger

Hi, Running SQL Server 2008 R2. We have a requirement to implement an arbitrary sort for a table using a numeric sort column. DDL and DML below: CREATE TABLE [dbo].[Test] ( [TestID] [bigint] IDENTITY(1,1) NOT NULL, …

SQL Server | SQL Server Transact-SQL
asked 2025-10-20T12:47:59.12+00:00
VDT-7677 171 Reputation points
edited a comment 2025-10-24T15:06:38.19+00:00
VDT-7677 171 Reputation points
2 answers

SPLIT_String is not recognized function name in SQL 2022

Good day, In SQL Server 2022 split_sting function shows with red underline like it doesn't exist. This statement SELECT v.value FROM #FirstResult CROSS APPLY SPLIT_STRING(Prov_Last_Name,'') AS v returns this error: Msg 208, Level 16, State 1,…

SQL Server | SQL Server Transact-SQL
asked 2025-10-19T01:22:48.07+00:00
Naomi Nosonovsky 8,771 Reputation points
commented 2025-10-24T10:26:09.8133333+00:00
Lakshmi Narayana Garikapati 470 Reputation points Microsoft External Staff Moderator
1 answer

SQL Server Database Engine error during insert/update sequence in employee audit table

I was running a SQL Server test for an internal employee-audit scenario. The script creates two tables, inserts a few rows and performs joins/updates. Expected: All inserts and updates should complete successfully. Observed: Several statements fail…

SQL Server | SQL Server Transact-SQL
asked 2025-10-23T12:44:06.4233333+00:00
aditi sharma 75 Reputation points
answered 2025-10-23T21:10:36.39+00:00
Erland Sommarskog 127.4K Reputation points MVP Volunteer Moderator
1 answer

Identity Column jumps massively from 168712 to 9999993

I have table with identity column id. There was massive jump from id 168712 to 9999993. It has happened twice in different servers. Its not identity cache/transaction rollback/manual insert/ reseed. Why its happening. Please guide me on this.

SQL Server | SQL Server Transact-SQL
asked 2025-10-17T16:06:46.38+00:00
Perumal, Gowthami 0 Reputation points
commented 2025-10-23T20:55:10.3+00:00
Erland Sommarskog 127.4K Reputation points MVP Volunteer Moderator
0 answers

SQL server 2022 job fails

Hello SQL Server 2022 16.0.4212.1 Installed in a windows server 2025 DC. SQLSERVERAGENT starts with NT Service\SQLSERVERAGENT I have some jobs that runs at the scheduled times. But one no. Failure: "Job failed. The job was invoked by Schedule 9…

SQL Server | SQL Server Transact-SQL
asked 2025-09-11T07:43:40.6466667+00:00
Delmira Oliveira 100 Reputation points
commented 2025-10-21T20:57:34.4433333+00:00
Erland Sommarskog 127.4K Reputation points MVP Volunteer Moderator
1 answer

"SQL Server Database Engine Error while inserting and updating employee records in test environment"

I'm running a SQL Server test script that inserts employee data and performs several operations. Some statements work, but a few lines throw different errors — duplicate keys, missing columns, and invalid joins. Below is my script. Can anyone explain…

SQL Server | SQL Server Transact-SQL
asked 2025-10-09T13:20:41.0766667+00:00
devid brevis 0 Reputation points
commented 2025-10-21T10:57:11.0366667+00:00
Lakshmi Narayana Garikapati 470 Reputation points Microsoft External Staff Moderator
1 answer

how to modify index maintenance script

Hi Team, How to modify T-sql indexmaintenance script only do Index Reorganize

SQL Server | SQL Server Transact-SQL
asked 2025-10-14T23:33:02.8266667+00:00
Vijay Kumar 2,061 Reputation points
commented 2025-10-21T03:58:23.58+00:00
Damien Pham (WICLOUD CORPORATION) 1,315 Reputation points Microsoft External Staff Moderator
3 answers One of the answers was accepted by the question author.

Unexplainable results

Good day, I have a complex stored procedure. This procedure returns different results when I add extra condition for specific NPI vs returning without this extra condition. Automation OHCS Professional Rendering Yes OHCS Partial…

SQL Server | SQL Server Transact-SQL
asked 2025-10-19T19:10:55.1333333+00:00
Naomi Nosonovsky 8,771 Reputation points
accepted 2025-10-21T00:09:48.6133333+00:00
Naomi Nosonovsky 8,771 Reputation points
2 answers

SSMS V22 times out on large queries where v19 did no

Hello, I use my laptop with SQL Developer to prototype databases before publishing them on a live SQL server. My laptop crashed and I had to reinstall all of the software. In my old laptop I was using SQL Server v2016 (not sure about this version) and…

SQL Server | SQL Server Transact-SQL
asked 2025-09-26T19:18:21.06+00:00
Claudio Cavalli 5 Reputation points
commented 2025-10-14T20:26:05.6166667+00:00
Erland Sommarskog 127.4K Reputation points MVP Volunteer Moderator
1 answer

SQL Server Error: Cannot insert duplicate key for EmployeeID

-- Step 1: Create Test Database CREATE DATABASE EmployeeDBTest2; GO -- Step 2: Switch to Database USE EmployeeDBTest2; GO -- Step 3: Create Employee Table CREATE TABLE Employees ( EmployeeID BIGINT NOT NULL PRIMARY KEY, Name NVARCHAR(100), varchar…

SQL Server | SQL Server Transact-SQL
asked 2025-10-01T09:02:44.05+00:00
Gaurav Kumar 5 Reputation points
commented 2025-10-13T16:34:32.47+00:00
Lakshmi Narayana Garikapati 470 Reputation points Microsoft External Staff Moderator
2 answers

How can I compare the Original T-SQL Script with the edited version of the same T-SQL

I thought Redgate and SQL Compare could compare two T-SQL scripts and highlight any changes, deltas for you...But I have no idea how to use that. I also know that there are some one-off T-SQL comparison software packages out there. Which would the forum…

SQL Server | SQL Server Transact-SQL
asked 2025-10-02T18:24:16.2066667+00:00
Bobby P 231 Reputation points
commented 2025-10-10T13:12:45.28+00:00
Shruti Dhruv 100 Reputation points Microsoft External Staff Moderator
2 answers One of the answers was accepted by the question author.

Always get latest score that is not 0 or null

Hi, I have a table like the below with scores. CREATE TABLE [dbo].[Student_Scores]( [Month] [int] NULL, [Student_no] [bigint] NULL, [Score] [numeric](18, 2) NULL ) ON [PRIMARY] GO INSERT [dbo].[Student_Scores] ([Month], [Student_no], [Score]) VALUES…

SQL Server | SQL Server Transact-SQL
asked 2025-10-03T12:32:00.38+00:00
Rontech10111 201 Reputation points
edited an answer 2025-10-06T14:07:28.0666667+00:00
Yitzhak Khabinsky 27,006 Reputation points
1 answer One of the answers was accepted by the question author.

Using a cutom policy file for Get-SqlSensitivityRecommendations

I'm trying to import a custom Information Protection Policy file into SSMS to add more patterns, mainly because I have a French audience, so most of the default terms don’t apply in my case. Since the interface works only for one database at a time and…

SQL Server | SQL Server Transact-SQL
asked 2025-10-03T14:56:21.73+00:00
Jeremie Bedard 20 Reputation points
accepted 2025-10-06T12:57:38.66+00:00
Jeremie Bedard 20 Reputation points
1 answer One of the answers was accepted by the question author.

Why this query produces an error?

Hi everybody, This this query produces an error ;WITH latestTermDate AS (SELECT MAX(status_date) AS dt, tin_number FROM #FirstResult GROUP BY tin_number UNION ALL SELECT MAX(hn.load_date) AS dt, tin…

SQL Server | SQL Server Transact-SQL
asked 2025-10-05T20:01:12.28+00:00
Naomi Nosonovsky 8,771 Reputation points
commented 2025-10-05T22:05:11.9533333+00:00
Naomi Nosonovsky 8,771 Reputation points
1 answer One of the answers was accepted by the question author.

How to fix "Msg 11535, Level 16, State 1,"

I have a complex procedure returning one result set. However, when I execute using WITH RESULT SETS I'm getting this error: Msg 11535, Level 16, State 1, Procedure im_automation.accent_changes, Line 814 [Batch Start Line 0] EXECUTE statement failed…

SQL Server | SQL Server Transact-SQL
asked 2025-10-06T01:42:36.78+00:00
Naomi Nosonovsky 8,771 Reputation points
commented 2025-10-06T13:13:37.2333333+00:00
Naomi Nosonovsky 8,771 Reputation points
2 answers One of the answers was accepted by the question author.

Where is dbo.indexoptimize procedure?

Hi, I have a SQL Server job with the step with the following code: EXECUTE [dbo].[IndexOptimize] @Databases = 'USER_DATABASES', @LogToTable = 'Y' Where is that procedure? I searched in msdb database and don't see it there.

SQL Server | SQL Server Transact-SQL
asked 2025-10-07T21:10:55.5766667+00:00
Naomi Nosonovsky 8,771 Reputation points
commented 2025-10-12T21:19:02.2066667+00:00
Naomi Nosonovsky 8,771 Reputation points