sql-server-2014

STUFF function to display multiple columns with comma-separated in SQL Server

Deadly 提交于 2019-12-11 16:29:43
问题 I need to display a table column (assume 2 columns) values with comma-separated in the output col1 Col2 ================ xyz abc pqr uvw Output needed: Col1 Col2 ======== ======= xyz,pqr abc,uvw I tried below query but not sure how to include col2 to the below query and make it as a single query: SELECT STUFF((SELECT DISTINCT ',' + CAST(t.Col1 AS VARCHAR(MAX)) FROM Table_1 t FOR XML PATH('')), 1, 1, '') 回答1: SQL Fiddle MS SQL Server 2017 Schema Setup : create table MyTable(ColA varchar(max)

Parallel Date Sales SQL View

柔情痞子 提交于 2019-12-11 13:48:40
问题 I have a challenge which I can't seem to resolve on my own and now need help! I have a requirement to show parallel year date sales via SQL and by that I mean if today (20/08/2015) Customer A has purchased products worth 500, I want to know how much Customer A spent on the same day last year (so 20/08/2014). Here's a SQL fiddle where I've built everything (I reckoned that would be easiest for you guys). I have 3 dimensions (DimProduct, DimDate and DimCustomer), a fact table (FactSales) and a

Table Valued Function vs Scalar Values Function for single return Value

旧城冷巷雨未停 提交于 2019-12-11 12:32:56
问题 I am returning single value from a scaler function as below: CREATE FUNCTION [dbo].[GetNoOfAssignedCases] ( @UserID INT, @FromD DATETIME, @ToD DATETIME ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @CaseCount INT = 0 SELECT @CaseCount = COUNT(1) FROM Cases WHERE CaseAssignedToAssessor = @UserID AND CAST(ActionDateTime AS DATE) >= @FromD AND CAST(ActionDateTime AS DATE) <= @ToD RETURN @CaseCount END And using it as below: SELECT [Name], [DBO].[GetNoOfAssignedCases](UserID, GETDATE()-30, GETDATE())

dynamic pivot for non aggregate

坚强是说给别人听的谎言 提交于 2019-12-11 11:21:24
问题 I know how to dynamically pivot as show, for example, here. Dynamic pivot might not work in my scenario, where the #source can contain dynamic columns and dynamic column values, which have to be pivoted as indicated here: IF OBJECT_ID('tempdb..#Source') IS NOT NULL DROP TABLE #Source IF OBJECT_ID('tempdb..#Aim') IS NOT NULL DROP TABLE #Aim CREATE TABLE #Source ( ColumnName NVARCHAR(10), ColumnValue NVARCHAR(10), Id INT ) CREATE TABLE #Aim ( Id INT, Column1 NVARCHAR(10), Column2 NVARCHAR(10),

Efficient way to generate 2 billion rows in SQL Server 2014 Developer

匆匆过客 提交于 2019-12-11 09:53:13
问题 Long story short; I am testing a system to purge entries from a table over a network connection, and the functionality is predicted to handle over 2 billion entries at most. I need to stress test this to be certain. Here's my test script (At best it's able to generate 9.8 million in ten minutes.) DECLARE @I INT=0 WHILE @I <2000000001 BEGIN INSERT INTO "Table here" VALUES(@I) SET @I=@I+1 END Can anyone suggest anything, or give me an idea what the upper limits of my test environment might be

SQL Server Management Studio does NOT let me create multiple foreign keys to multiple primary keys

半腔热情 提交于 2019-12-11 08:34:13
问题 I have a table ApplicationRegion in a one-to-many relationship to Translation . I want to set FK's from Translation.appname/isocode to ApplicationRegion.appname/isocode But that did not work as you can see from the screenshot. When the FK configure window opens 3 columns are shown on the left/right side appname isocode resourcekey Then I chose as parent table ApplicationRegion and removed the resource key column from the FK setting. And clicked OK but then I got the error you see on the

How to write the T-SQL STRING_AGG function

牧云@^-^@ 提交于 2019-12-11 08:13:21
问题 I need to write a STRING_AGG for my SQL Server 2014 master database. It is a built in function for SQL Server 2017 and Azure SQL. I have an application that executes stored procedures in an Azure SQL database, some of which use STRING_AGG . In our local development instance, we use a database on our localhost running on an older version of SQL Server. The issue is, because SQL Server 2014 doesn't have access to STRING_AGG , the stored procedures we wrote for our Azure SQL database won't work

TSQL: Find a continuous number in a string

孤者浪人 提交于 2019-12-11 07:49:05
问题 I need to find a continuous 6 or 7 digit number in a string from column name Filename . The string has other numbers in it with dashes(or another character, like an underscore), but I only need the continuous number The StudentID needs to be extracted from the filename. (I know the data is just wow, multiple vendors, multiple file naming formats is the cause.) Another option would be to just list the starting position of the continuous number. Desired outcome: Actual outcome: Test Code: DROP

TRANSACTIONS Call Stored Procedure of another database

我怕爱的太早我们不能终老 提交于 2019-12-11 07:28:54
问题 Related to this question: Executing a stored procedure inside BEGIN/END TRANSACTION I am calling my stored procedure via BizTalk. Because BizTalk is creating a TRANSACTION my stored procedure has no TRANSACTION handling. However, I have to call another stored procedure within the one called by BizTalk. BUT the second stored procedure is called against another database within the same MSSQL Instance. Is MS SQL aware of this cross database procedure call and does a rollback of the second stored

Add missing rows to a result set

元气小坏坏 提交于 2019-12-11 07:08:58
问题 I have a fact and dim table create table #fact (SKey int, HT varchar(5), TitleId int) insert into #fact values (201707, 'HFI', 1), (201707, 'HFI', 3), (201707, 'HFI', 5), (201707, 'HFI', 6), (201707, 'REO', 1), (201707, 'REO', 2), (201707, 'REO', 4), (201707, 'REO', 5) create table #dim (TitleId int, Title varchar(10)) insert into #dim values (1, 'UK'), (2, 'AF'), (3, 'LQ'), (4, 'AL'), (5, 'GT'), (6, 'ML') using below query select #fact.SKey, #fact.HT, #fact.TitleId, #dim.Title from #fact