Category "tsql"

SUM() with entries from 2 table

Select p.pnum, SUM(CASE WHEN P.NegativeScreen = 'Type99' THEN 1 ELSE 0 END) TotalDetected, SUM(IIF(P.IsPositive = 1, 1,0)) TotalP, SUM

SQL User-Defined table Type in INTO statement

I want to Create a table @tablename with data from a User-Defined Table Type but I get the error Must declare the table variable "@tablename" ALTER PROCEDURE

ERROR: Unsupported PIVOT column type: text

I'm trying to perform a PIVOT but getting an error. I'm trying to see which patients have more than one Interval session. The column is a text type. I attempted

How do I add an error message to my stored procedure when 0 rows are affected?

I am trying to learn how to use transactions and error handling. Got stuck with custom error messages. The stored procedure generates badges for AdventureWorks

Select item that is different in each of group by a column

I have this sample table +--------+-------------+ | DBName | Description | +--------+-------------+ | A | Car | | A | Boat | | B

SQL Server : run a script on all databases

I have a SQL script with a function (function drops at the end of the script) and temporary table definitions and variables to process the data that gives me on

Leading space with STR function [closed]

I need to get a comma separated list with quotes and I'm almost at that point but I get a list with commas but a leading space at the start bu

How do I ignore null values in t-sql function?

I am doing an outer join between two market data tables. The two tables have trading volume reported differently, therefor I need to query them separately and t

TSQL Subquery in from

I am attempting to do a subquery which should work but I am missing something in the syntex. I am trying: select * from (select * from (select *, row_number

Transpose results of a sql query

How to transpose this display. SeqNo Step Date By 1 Quoted 2018-03-01 Person1 2 Checked 2018-03-02 Person2 3 A

Is there another way to write SUM(IIF([CONDITION], 1, 0))?

What SUM(IIF([CONDITION], 1, 0)) does is very simple. So simple, that what I really just want to write is COUNT([CONDITION]) (but that's invalid). Is there a sh

Are there cons to generating GUIDs with a stored procedure using NEWSEQUENTIALID?

My goal is to create a stored procedure that generates new GUIDs using NEWSEQUENTIALID that can then be used when inserting data into a different table in the s

T-SQL hierarchy query

I have a table with hierarchical data: This is a sample of data with id, parent id, name, code (which is sometimes not filled), level and isroot column. In r

Are there any advantages in using a single stored procedure for multiple operations or is my teacher wrong? [closed]

I'm working on my databases class final project, which consists of making an application that can access and do operations in a database. My t

MS SQL: extremely slow query with 4 OR condition, but each condition alone run very quickly

I have two physical tables: notTempBaseLine with 50k records (index on Version, SrcDimension2_) notTempTrans with 400k records (index on BaseLineVersion, Dimens

SSIS Alternatives to one-by-one update from RecordSet

I'm looking for a way to speed up the following process: I have a SSIS package that loads data from Excel files on a weekly basis to SQL Server. There are 3 fi

How to perform a LEFT JOIN on the same table using Linq?

Consider a table where rows may be linked to each other. We need to select the rows that meet a certain requirement, OR where its linked row meets that requirem

SELECT returns NULL if run from application

I have an INSERT trigger on a view created with VIEW_METADATA. There's the following snippet: if @has_folder is null set @has_folder=(select REPLACE(REPLAC

SQL Server TRUNCATE

I have a question to the TRUNCATE command. How does TRUNCATE work in background ? I read a text that says TRUNCATE creates a copy from the table and then star

Rolling COUNT DISTINCT of n-day active users using T-SQL

I am counting 7-day active users using T-SQL. I used the following code: SELECT *, COUNT(DISTINCT [UserID]) OVER ( PARTITION BY [HospitalID],