Q&A archive

Stack Exchange Answers

Questions and answers Andy Mallon contributed across the Stack Exchange network.

Stack Overflow

Enforce a specific constraint to be checked first?

Question I have the following SQL Server table : create table Account( -- column names with their data types and properties Id int not null identity primary key, User varchar(50) not null, Job varchar(200) not null, -- table limitations constraint …

Read answer

DBA Stack Exchange

AG secondary is out of sync for hours after index maintenance

Question We have a 3TB OLTP database running Siemens Opcenter MES software at one of our manufacturing plants in China. This is setup in an Always-On Availability group with one secondary node. As this is a manufacturing plant, we are only provided 11 1/2 …

Read answer

DBA Stack Exchange

Using OPENJSON, can't get results to GROUP BY

Question I'm passing a JSON string to a stored procedure in order to insert warehouse inventory. Sometimes, there will be multiple line items with the same product, going to the same shelf, on the same pallet (LPN), with the same date. I would like to …

Read answer

DBA Stack Exchange

SQL Modulo Function gives the wrong value?

Question In Javascript, Excel, Python, and a scientific calculator, the modulo operation -34086.1576962834 % 360.0 <-- python =MOD(-34086.1576962834, 360.0) <-- Excel function gives a value of 113.84230371659942. (Or something very close to that, depending on …

Read answer

DBA Stack Exchange

What's the use case of disabling an index?

Question I just learned that indexes can be disabled on a table. Even the clustered index can be disabled. After that, users can’t access the index or the data if clustered index. When will we disable an index? I just don’t understand the use case.

Read answer

DBA Stack Exchange

snapshot replication - corrupted BCP files

Question Each night my SQLServer replication failed on a big table with the following error. It is failing on a different table each night but always on a relatively big table ( > 200,000 rows) The process could not bulk copy into table '"dbo"."Table01"'. …

Read answer

DBA Stack Exchange

Temporarily turn on Cascade Delete

Question This has probably been asked before, but I couldn't find the answer. I have a complex datamodel in sql-server-20XX (more likely to be something old) in which cascade delete has been turned off (by design). A large chunk of our database has been sold …

Read answer

DBA Stack Exchange

SQL Main query value used in the WHERE in the subquery

Question I'm really confused by this SQL here, I have this sample where I am using the value from the main query in the subquery and then using XML PATH to add a comma between the dates. The setup is 2 tables one with the dates I want to string together. I am …

Read answer

DBA Stack Exchange

Allow create table (dynamic name) and insert but nothing else

Question I'm trying to find a way to allow an application to create tables and insert data into them on a SQL Server 2019 while protecting from injection attacks in case the app credentials would leak. My experience is limited when it comes to writing code …

Read answer

DBA Stack Exchange

CASE statement issue

Question Can anyone help with this error? SQL: SELECT InvoiceDate, BillingAddress, BillingCity, Total, CASE WHEN Total < 2.00 THEN 'Baseline Purchase' WHEN Total BETWEEN 2.00 AND 6.99 THEN 'Low Purchase' WHEN Total BETWEEN 7.00 AND 15.00 THEN 'Target …

Read answer

DBA Stack Exchange

Which install kit installs sqlcmd.exe?

Question I recently had to uninstall Visual Studio 2012 and DTS, then install VS 2017 and DTS again. Afterwards, The directory containing sqlcmd.exe was not in my PATH variable. I installed the following. vs_Professional-vs2017-15.9.41.exe …

Read answer

DBA Stack Exchange

Database context for sys.databases and sys.master_files

Question I have a query that pulls information for a database from the following tables sys.master_files sys.databases sys.filegroups Currently, the context set is the specific database which I want to query about. But if the database is in a mode such that …

Read answer