Questions tagged [t-sql]
Transact-SQL (T-SQL) is a dialect of SQL used by Microsoft SQL Server and SAP's Sybase.
3,126 questions
5
votes
2
answers
204
views
STRING_SPLIT - issue with NCHAR(769) and SQL_Latin1_General_CP1_CI_AS
In database
on SQL Server 2022 Standard Edition (CU19)
with default collation SQL_Latin1_General_CP1_CI_AS
I am splitting CHAR(31) separated values using STRING_SPLIT. I have an issue when the ...
8
votes
1
answer
385
views
Alter table alter column does not work with user defined types
A user with db_datareader, db_datawriter, db_ddladmin and db_securityadmin roles can add columns with a user defined type, but cannot alter those.
For example: Lets say we create a user defined type ...
5
votes
1
answer
266
views
Do I need to run sp_refreshview and sp_refreshsqlmodule after changing column collations?
I look after a database for a vendor’s application that our system team uses to store passwords. This database is part of a three-node AlwaysOn Availability Group. The instance and database currently ...
0
votes
2
answers
80
views
Can I get data from an estimated Execution Plan in my C# code?
After years of growth, some reports now take minutes to extract from our database, and in some cases fail due to excess resource consumption. This depends on the parameters that our customers choose ...
1
vote
2
answers
232
views
Attach a database using mdf and ldf files with an already existing FILESTREAM
I can't attach a SQL Server database using the .mdf and .ldf files; it is crucial for me because I need the latest transactions and the latest object modifications in it.
What I have currently at hand ...
1
vote
1
answer
112
views
Can you create non-enforced foreign key on existing table in SQL Server? [duplicate]
Say I am trying to understand some unkown part of a database. I usually rely a lot on foreign keys to understand the relations between the data, but the tables in question don't have any for some ...
0
votes
1
answer
69
views
sp_executesql with ISNUMERIC(@ColumnName) not working with column name passed as parameter
Question: Why example 1 does not work while example 2 works:
SQL Table to check for non-numeric values in varchar column:
CREATE TABLE #t(Mycol varchar(15))
INSERT #t VALUES ('123.86'),('4a5.67'),('45....
0
votes
1
answer
152
views
SQL CASE statement ELSE block usage for inserting a record
The last line of the following code inserts records from #S to #D if value is a valid date; otherwise it returns NULL (since I am not using ELSE clause):
CREATE TABLE #S(COL VARCHAR(15))
INSERT #S ...
1
vote
0
answers
98
views
Is it possible to make a batch of “Alter procedure” that are stored in a table?
Currently I have hundreds of sp that are in multiple servers, and that coexist with other sp that are in other servers with linked server.
The origin of this is that when I do a restore of databases ...
0
votes
2
answers
244
views
Grouping and arranging data using FOR JSON PATH and INCLUDE_NULL_VALUES
I'm having an issue with my T-SQL script. I am thinking about and working on how to produce the expected output from it.
Here's my T-SQL script:
DECLARE @user_id VARCHAR(50) = 'UGUID-2025-01-27-14-09-...
1
vote
1
answer
227
views
Multiple UPDATE and INSERT statements using BEGIN TRAN with customized COMMIT TRAN and ROLLBACK TRAN
First things first, since I am using SET XACT_ABORT ON. If an exception happens in any of the INSERT or UPDATE statements within the transaction block, then it should terminate and roll back the ...
4
votes
1
answer
436
views
Is it possible to extract the formula or expression of a field within a SQL Server Stored Procedure?
I have a stored procedure that contains a single select query. Within the query, there are a few aliases that represent a formula or expression.
Is it possible to extract the calculation / formula / ...
18
votes
2
answers
2k
views
Why SUM function returning 4 decimal places, but AVG function returning 6 decimal places
Question: Why SUM is returning with 4 decimal places (as expected), but the Avg is returning with 6 decimal places?
CREATE TABLE #t(Col DECIMAL(19,4))
INSERT #t VALUES (123456.1200),(654321.3400)
...
0
votes
1
answer
109
views
Database user must display only one database and one schema
I have a server on which there are 5 databases: A, B, C, D, E.
I need to create a new schema on database A called ‘rpt’. Then I need to create a new user called ‘usr_rpt’.
This user must have these ...
1
vote
1
answer
113
views
Convert numbered variables into a table row
I'm working with a legacy system where a stored procedure is called and dozens (literally) of variables are passed in. The data needs to be processed the same way but currently I have to copy/paste ...