sql-server-2012

T-SQL “not in (select ” not working (as expected)

China☆狼群 提交于 2020-01-06 05:40:15
问题 I have an ordinary one-to-many relation: customer.id = order.customerid I want to find customers who have no associated orders. I tried: -- one record select * from customers where id = 123 -- no records select * from orders where customerid = 123 -- NO RECORDS select * from customers where id not in (select customerid from orders) -- many records, as expected. select * from customers where not exist (select customerid from orders where customers.customerid = customer.id) Am I mistaken, or

Create more than one non clustered index on same column in SQL Server

烈酒焚心 提交于 2020-01-06 04:01:07
问题 What is the index creating strategy? Is it possible to create more than one non-clustered index on the same column in SQL Server? How about creating clustered and non-clustered on same column? Very sorry, but indexing is very confusing to me. Is there any way to find out the estimated query execution time in SQL Server? 回答1: The words are rather logical and you'll learn them quite quickly. :) In layman's terms, SEEK implies seeking out precise locations for records, which is what the SQL

show only categories that have products in them

旧巷老猫 提交于 2020-01-06 03:19:27
问题 excuse the bad title but I couldn't find a good way to express what I want in abstract terms. Anyway I have 3 tables tbl_product: PID | productname 1 | product 1 2 | product 2 3 | product 3 4 | product 4 .. tbl_categories, motherCategory allows me to nest categories: CID | categoriename | motherCategory 1 | electronics | NULL 2 | clothing | NULL 3 | Arduino | 1 4 | Casings, extra's | 3 .. tbl_productInCategory PID and CID are foreign keys to PID and CID in tbl_product and tbl_categories

Does SQL Server 2012 Lazy Join

人盡茶涼 提交于 2020-01-05 12:11:43
问题 I am working on an application that allows users to dynamically choose what they see. The list of columns and tables those belong to is huge. I'm working on building a dynamic query but I don't want to have to store all the join logic if I don't have too. An example is I have a query that has LEFT JOINS for 10 tables but I only select and filter by one table. Is SQL Server 2012 Smart enough to not join on the other tables that are not needed? I know Oracle's DBMS works like this but my

Split Column with delimiter into multiple columns

空扰寡人 提交于 2020-01-05 08:34:00
问题 I got a table with 20 columns. One of the columns contains multiple values separated by a semicolon. It looks like this: -9;-9;-1;-9;-9;-9;-9;-9;-1;-9;-9;-9;-9;-9;-9;-9;-9;-9;-1;-9;-9;-9;-9;-9;-9;-9;-9;-9;-1;-9;-1;-9;-9;-9;-1;-9;-9;-9;-9;-9;-9;-1;-1;-1;-1;-9;-1;-1;-9;-9;-9;-9;-1;-9;-1;-9;-9;-9;-1;-9;-1;-9;-1;-9;-9;-9;-9;-1;-9;-9;-1;-1;-9;-1;-1;0000;FFF8;-9;-9;-9;-1;-9;-1;-9;FFF6;-9;-1;-9;-1;-9;-1;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9 It contains always 115

How process JSON data that needs to be unpivoted?

核能气质少年 提交于 2020-01-05 04:55:57
问题 I have a JSON source that has a weird layout where there are unknown amount of columns. https://theunitedstates.io/congress-legislators/committee-membership-current.json The format is like the following: ColumnHeaders => HLAG HSAG HSAG01 .... to unknown Single row of Data JSON JSON JSON How can I get the data like this: Col1 Col2 HLAG JSON HSAG JSON HSAG01 JSON I am currently working in SSIS so I have C# solutions available to me. I just don't know how to deal with unknown columns in SSIS.

Maintenance jobs in SQL small databases

£可爱£侵袭症+ 提交于 2020-01-05 04:23:08
问题 My customers have very small databases of sizes 50 t0 100 MBs. We are using SQL Server 2014. The backup operations are scheduled properly. Can I recommend them to have the maintenance jobs for Index Maintenance, Update Statistics and DBCC CheckDB? How deep it will affect if these jobs are not taken care? 回答1: too vague to answer precisely, it depends a lot on the kind of activity done on the DB, the recovery model used. In the simple recovery mode, which use just db file without transaction

Dense_Rank ordering

痞子三分冷 提交于 2020-01-05 04:21:13
问题 I've read a few variations of this question, but the solutions don't seem to be working. I wish to dynamically create a "Subgroup" for each "OrderNo" & "GroupID". Subgroups should be ordered by "OrderLine" eg: (Expected outcome) OrderNo OrderLine GroupID Subgroup ------------------------------------ 10463 1 798 1 10463 2 799 2 10463 3 797 3 10463 5 65 4 10463 6 65 4 10463 7 65 4 10481 4 917 1 10481 5 918 2 10481 6 131 3 10481 7 131 3 10481 8 131 3 10481 9 130 4 I've used Dense_Rank() to

Convert Date to CYYMMDD SQL-Server

左心房为你撑大大i 提交于 2020-01-05 04:10:07
问题 I am interested in converting a date value to CYYMMDD using native SQL Server 2012 functions. I checked here: http://www.sql-server-helper.com/tips/date-formats.aspx and couldn't find anything. Anyone know? C stands for century I guess... 回答1: Have you looked at the new FORMAT function in SQL Server 2012? http://msdn.microsoft.com/en-us/library/hh213505.aspx You should be able to use something like this: SELECT FORMAT(YourDateColumn, 'yyyyMMdd') or whatever you really want to use - basically,

Correct way to execute 2 SQL commands without other command executing in between

有些话、适合烂在心里 提交于 2020-01-04 15:30:02
问题 The suggested duplicate doesn't answer the question in the title. I want to execute two MSSQL commands without any other "user" (I'm not sure what's the correct term) executing a command between them. Searching, I have found two ways that seem like they would achieve that but am unsure about them: Use TABLOCK. - But I've seen it being considered bad practice. Use a transaction - But all I could find was that it will be atomic, and not necessarily locking out other actions. What way is the