sql-server-2012

grouping and switching the columns and rows

妖精的绣舞 提交于 2019-12-03 20:27:35
I don't know if this would officially be called a pivot, but the result that I would like is this: +------+---------+------+ | Alex | Charley | Liza | +------+---------+------+ | 213 | 345 | 1 | | 23 | 111 | 5 | | 42 | 52 | 2 | | 323 | | 5 | | 23 | | 1 | | 324 | | 5 | +------+---------+------+ my input data is in this form: +-----+---------+ | Apt | Name | +-----+---------+ | 213 | Alex | | 23 | Alex | | 42 | Alex | | 323 | Alex | | 23 | Alex | | 324 | Alex | | 345 | Charley | | 111 | Charley | | 52 | Charley | | 1 | Liza | | 5 | Liza | | 2 | Liza | | 5 | Liza | | 1 | Liza | | 5 | Liza | +----

Most efficient way to split string into rows

岁酱吖の 提交于 2019-12-03 20:22:07
I am using the following function to split a string into rows. It is much faster than the previous function that I was using, however I need to somehow churn through this data quicker (its an ETL job): ALTER FUNCTION [dbo].[ArrayToTable] ( @InputString VARCHAR(MAX) = '' , @Delimitter VARCHAR(1) = ',' ) RETURNS @RESULT TABLE([Position] INT IDENTITY, [Value] VARCHAR(MAX)) AS BEGIN DECLARE @XML XML SELECT @XML = CONVERT(XML, SQL_TEXT) FROM ( SELECT '<root><item>' + REPLACE(@InputString, @Delimitter, '</item><item>') + '</item></root>' AS SQL_TEXT ) dt INSERT INTO @RESULT([Value]) SELECT t.col

Get all employees under manager with CTE

蹲街弑〆低调 提交于 2019-12-03 18:12:26
问题 I have a table which has employee details EmpId ManagerId Level Value 1 0 5 CEO 2 1 4 EMP 3 1 4 ORG 4 2 3 NULL 5 2 3 NULL 6 2 2 NULL 7 1 1 NULL 8 5 0 NULL Now, I have to start wil Employee Id 2 and found all it's low level hirerachy (i.e. 2, 4, 5, 6, 8) and assign them value same as "2" (i.e. EMP). Expected output : EmpId ManagerId Level Value 1 0 5 CEO 2 1 4 EMP 3 1 4 ORG 4 2 3 EMP 5 2 3 EMP 6 2 2 EMP 7 1 1 NULL 8 5 0 EMP What I am trying: ; WITH LevelHire AS ( SELECT EmpId, ManagerId,Level

How to add dynamic column to an existing table

偶尔善良 提交于 2019-12-03 18:11:04
问题 I have 2 tables 1st table contains following columns, id code Name 1 c1 chk1 2 c2 chk2 3 c3 chk3 2nd table contains following columns, id,Name,Chk1,chk2,Chk3 i have to add the column 'Chk4' into table2 if table1 is updated with value '4,'c4','ch4' dynamically.How to write procedure to perform this? i've tried the following procedure but its not working fine. create proc Add_Check as begin declare @Column varchar(50) declare @query varchar(255) declare @query1 varchar(255) set @Column= (select

Update Target Button is disabled in Sql Server Database Schema Comparison

为君一笑 提交于 2019-12-03 18:08:08
问题 I use Visual Studio 2013 Professional Update 4 in combination with multiple SQL Server 2012 Enterprise servers/instances. The Idea was to create a Version controlled deployment using a Visual Studio Solution with SQL Server Projects. However for some reason the "Update Target" and "Generate Script" button is disabled. I can update the local database project from the sql server database but the other way around is not working. (see screenshot) Google is not helpful at all although I found a

Django SQLServer [FreeTDS][SQL Server]The data types nvarchar(max) and ntext are incompatible in the equal to operator

喜你入骨 提交于 2019-12-03 17:10:17
I am getting the following error on doing django model.objects.get_or_create . SQLServer [FreeTDS][SQL Server]The data types nvarchar(max) and ntext are incompatible in the equal to operator. The column django is complaining about is NVARCHAR(MAX) with null allowed in SQLServer (which was auto created by Django migration). The Django model element is defined as TextField(null=True) 来源: https://stackoverflow.com/questions/39055935/django-sqlserver-freetdssql-serverthe-data-types-nvarcharmax-and-ntext-are

TSQL OVER clause: COUNT(*) OVER (ORDER BY a)

≯℡__Kan透↙ 提交于 2019-12-03 16:58:44
问题 This is my code: USE [tempdb]; GO IF OBJECT_ID(N'dbo.t') IS NOT NULL BEGIN DROP TABLE dbo.t END GO CREATE TABLE dbo.t ( a NVARCHAR(8), b NVARCHAR(8) ); GO INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a', 'b'); INSERT t VALUES ('a', 'b'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('c', 'd'); INSERT t VALUES ('e', NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL); INSERT t VALUES (NULL, NULL

Use SQL INSERT to Create Directories with FileTable

非 Y 不嫁゛ 提交于 2019-12-03 16:43:53
Is there a way to create directories in a FileTable without using File I/O APIs? Is it just as simple as creating a SQL INSERT statement? This is my first experience with SQL Server 2012 and I'm not familiar with the limits of the FileTable from within SQL Server. The following worked for me. Hopefully this helps out someone else. INSERT INTO FileTable0 (name,is_directory,is_archive) VALUES ('Directory', 1, 0); You should also check out Bob Beauchemin's Techdays 2012 video for more details on FileStream and its evolution into FileTable . 来源: https://stackoverflow.com/questions/10483906/use-sql

What are the main differences between OPTION(OPTIMIZE FOR UNKNOWN) and OPTION(RECOMPILE)?

£可爱£侵袭症+ 提交于 2019-12-03 16:38:34
问题 I run into the classic Parameter Sniffing issues in SQL Server 2012. Based on some research I found multiple options around this problem. The two options that I need to understand the difference between are OPTION(OPTIMIZE FOR UNKNOWN) vs OPTION(RECOMPILE) . I am hesitating to use OPTION(RECOMPILE) at the end of my queries that are having this issue because it will force the server to generate a new execution plan each time. If I call this query often this will spike up the CPU of that

SQL - how do I generate rows for each month based on date ranges in existing dataset?

烈酒焚心 提交于 2019-12-03 16:34:51
assume I have a dataset: rowID | dateStart | dateEnd | Year | Month 121 | 2013-10-03 | 2013-12-03 | NULL | NULL 143 | 2013-12-11 | 2014-03-11 | NULL | NULL 322 | 2014-01-02 | 2014-02-11 | NULL | NULL And I want sql to generate the following datasource based on the dateStart and the dateEnd. Note the year and month grouping. rowID | dateStart | dateEnd | Year | Month 121 | 2013-10-03 | 2013-12-03 | 2013 | 10 121 | 2013-10-03 | 2013-12-03 | 2013 | 11 121 | 2013-10-03 | 2013-12-03 | 2013 | 12 143 | 2013-12-11 | 2014-03-11 | 2013 | 12 143 | 2013-12-11 | 2014-03-11 | 2014 | 1 143 | 2013-12-11 |