sql-server-2012

Listing all Parents of an items in a hierarchy table as delimitted string SQL

帅比萌擦擦* 提交于 2019-12-24 11:55:29
问题 I have an SQL table like this ID Name ParentID ------------------------------ 1 Alex 0 2 John 0 3 Don 1 4 Philip 2 5 Shiva 2 6 San 3 7 Antony 6 8 Mathew 2 9 Cyril 8 10 Johan 9 ------------------------- Am looking for an out put like this if I pass the ID 7,10 The out put table will be ID Name Relation ------------------------------------ 7 Antony Alex->Don->San->Antony 10 Johan John->Mathew->Cyril->Johan How can I achieve that using CTE 回答1: This seems to do the trick. The key is to realise

LINQ multiple joins with multiple conditions

試著忘記壹切 提交于 2019-12-24 11:36:26
问题 I have few large tables and I need to join them. In SQL it looks like: select * from dbo.Table1 t1 join dbo.Table1 t1Parent on t1.ParentId = t1Parent.Id join dbo.MappingT1T3 t2 on t1Parent.Id = t2.ExternalId and t2.[Type] = 1 join dbo.Table3 t3 on t2.ForeignId = t3 .Id where t1.[Type] = 3 Tried to convert this query to a such LINQ: from t1 in dbo.Table1 join t1Parent in dbo.Table1 on t1.ParentId equals t1Parent.Id join t2 in dbo.MappingT1T3 on new { Id = t1Parent.Id, [Type] = (int)1 } equals

NVarchar Prefix causes wrong index to be selected

陌路散爱 提交于 2019-12-24 11:35:22
问题 I have an entity framework query that has this at the heart of it: SELECT 1 AS dummy FROM [dbo].[WidgetOrder] AS widgets WHERE widgets.[SomeOtherOrderId] = N'SOME VALUE HERE' The execution plan for this chooses an index that is a composite of three columns. This takes 10 to 12 seconds. However, there is an index that is just [SomeOtherOrderId] with a few other columns in the "include". That is the index that should be used. And when I run the following queries it is used: SELECT 1 AS dummy

Read XML data into hierarchical tables

橙三吉。 提交于 2019-12-24 11:07:25
问题 I have XML data that is used to store quiz results. I need to convert this into two tables, one containing the questions, and the other containing the answers, but crucially, have a relation between them. Currently this relation only exists in the XML structure (there are no ID values, etc.). After a day of research and testing out different approaches, I've got as far as extracting the two parts, but cannot figure out how to create the hierarchy: declare @xml xml = N'<quizresult> <question>

SQL Server to count how many times a value appears between multiple date ranges and compare to previous weeks

久未见 提交于 2019-12-24 09:48:43
问题 I first asked a similar question at SQL Server to count how many times a value appears between multiple date ranges and wasn't sure if I should post a new question or modify the original post. Though it's building on the original post I felt it was different enough to render a new post - please advise if this was the correct thing to do. I wasn't given a few solutions that worked well but unfortunately a caveat was thrown at me and I need to make some modifications based on an additional

Get list of all databases that have a view named 'foo' in them

我的未来我决定 提交于 2019-12-24 08:54:51
问题 I have a few servers that have a bunch of databases in them. Some of the databases have a view called vw_mydata. What I want to do is create a list of all databases containing a view named vw_mydata and then execute that view and store it's contents in a table that then contains al the data from all the vw_mydata. I know I can find all the databases containing that view using sp_msforeachdb 'select "?" AS dbName from [?].sys.views where name like ''vw_mydata''' But then I have as many

How to join many to many and keep the same total amount

隐身守侯 提交于 2019-12-24 08:38:51
问题 I have two data-sets. Left data-set has the same QuoteID, PolicyNumber, but can be different Year, Month and PaidLosses. Second data-set has different QuoteID, same PolicyNumber different year, and different Month and also can be multiple ClassCode. I need to join first data-set with second one and keep the same PaidLosses. Main goal is to keep the same total PaidLosses by each month. I know its probably not very business proper, but that's what boss wants to see. This is what I tried so far:

Can I Inner Join 2 tables based on multiple values for a column

流过昼夜 提交于 2019-12-24 08:22:40
问题 This is what I want, TableA Inner Join TableB on TableA.ColumnA = TableB.ColumnA OR TableB.ColumnA = NULL It gives no results back, any alternative ? Edit 回答1: One issue in your query is TableB.ColumnA = NULL . NULL is undefined and you cannot use comparison operators to check null. Replace TableB.ColumnA = NULL With TableB.ColumnA IS NULL By looking at the picture description, I think you are looking for a LEFT/RIGHT JOIN . 来源: https://stackoverflow.com/questions/24733450/can-i-inner-join-2

How to pivot multiple columns without aggregation

大憨熊 提交于 2019-12-24 07:26:27
问题 I use SqlServer and i have to admit that i'm not realy good with it ... This might be and easy question for the advanced users (I hope) I have two tables which look like this First table (ID isn't the primary key) ID IdCust Ref 1 300 123 1 300 124 2 302 345 And the second (ID isn't the primary key) ID Ref Code Price 1 123 A 10 1 123 Y 15 2 124 A 14 3 345 C 18 In the second table, the column "Ref" is the foreign key of "Ref" in the first table I'm trying to produce the following output: [EDIT]

Unable to Insert into table using EXEC

依然范特西╮ 提交于 2019-12-24 07:17:35
问题 EXEC('INSERT INTO T_MyTable('+ @Columns +') EXEC ('+ @UpdateString + ')' ) Where @Columns contains comma seperated column names and @UpdateString contains 'Update T_OtherTable Set col1 = 123, col2 = 456, col3 = 'nice' OUTPUT DELETED.col1 as Old_FirstCol INSERTED.col1 as New_FirstCol DELETED.col2 as Old_SecondCol INSERTED.col2 as New_SecondCol DELETED.col3 as Old_ThridCol INSERTED.col3 as New_ThirdCol Where ID = 1' I am getting incorrect syntax error. Can't we use EXEC inside EXEC? 回答1: you