sql-server-2012

How to get the next Sequence number in Sql Server Express using Entity Framework?

蹲街弑〆低调 提交于 2019-12-05 18:32:28
问题 Now that Sql Server 2012 (including SQL Server Express 2012) has SEQUENCE feature just like Oracle as explained here, here, and here. I can get the next sequence like so, SELECT NEXT VALUE FOR SeqName But how do I do that from my code using Entity Framework 5? 回答1: I got it working using SqlQuery like so.. int sequence = context.Database.SqlQuery<int>("SELECT NEXT VALUE FOR MySequenceName").FirstOrDefault(); 来源: https://stackoverflow.com/questions/16753149/how-to-get-the-next-sequence-number

Islands and Gaps Issue

北慕城南 提交于 2019-12-05 17:57:48
Backstory: I have a database that has data points of drivers in trucks which also contain the. While in a truck, the driver can have a 'driverstatus'. What I'd like to do is group these statuses by driver, truck. As of now, I've tried using LAG/LEAD to help. The reason for this is so I can tell when a driverstatus change occurs, and then I can mark that row as having the last datetime of that status. That in itself is insufficient, because I need to group the statuses by their status and date. For this, I've got something such as DENSE_RANK, but I can't manage to get that right concerning the

How to save SQL query result to XML file on disk

青春壹個敷衍的年華 提交于 2019-12-05 16:49:11
问题 I would like to export table from SQL Server 2012 to XML file. I have found nice answer and here how to make XML result from SQL Server database query, but still I am missing how to save this result physically into file. SQL query is: SELECT [Created], [Text] FROM [db304].[dbo].[SearchHistory] FOR XML PATH('Record'), ROOT('SearchHistory') I use Microsoft SQL Server Management Studio to execute this result. I see the XML in a result window, but I cannot save it. There is "Save Result As.." in

SQL Query to return records grouped by Week, Month, and Year. Weeks with no records should return 0

老子叫甜甜 提交于 2019-12-05 16:24:01
Given the following data: ID CreatedDate ID1 2014-06-04 01:40:56.880 ID8 2014-06-05 00:27:02.403 ID6 2014-06-04 01:51:47.060 ID7 2014-06-05 00:25:35.187 ID4 2014-06-04 01:48:44.157 ID10 2014-06-05 00:28:33.993 ID43 2014-06-16 05:17:18.803 ID72 2014-06-20 04:00:07.733 . . . Etc. I need to write a query that will group records by week, month, and year. A count of the number of records that were created in a particular week needs to be returned. The output should be as follows: Week Month Year Count 23 6 2014 16 24 6 2014 0 25 6 2014 7 26 6 2014 0 27 7 2014 25 28 7 2014 18 . . . etc. Where a week

Combine multiple rows into one row

人走茶凉 提交于 2019-12-05 16:10:49
I'm trying different JOIN queries, but I'm not getting result I'm looking for. I've got 2 tables: Table 1: **StockItemShort** ItemID | Code | Name 432724 | CK002-16-09 | Green Daisy Pearl Earrings 432759 | CK002-16-149 | Emerald Crystal Centre Daisy Earrings Table 2: **StockItemCatSearchValueShort** ItemID | SearchValueID 432724 | 388839 432724 | 389061 432724 | 390269 432724 | 389214 432759 | 388839 432759 | 389051 432759 | 390269 432759 | 389214 I can't get result I'm looking for. I'd like to get following result: ItemID | Code | Name | SearchValueID | SearchValueID | SearchValueID |

updating multiple columns using case statement in sql server

最后都变了- 提交于 2019-12-05 15:34:53
I would like to update the table using case statement the query is like this... select case(@columnname) when 'name1' then begin update table set pay1=pay1* 100 pay2=pay2*20 pay3=pay3* 100 end when 'name2' then begin update table set pay2=pay2*20 pay3=pay3* 100 end when 'name3' then begin update table set pay3=pay3* 100 end end can u please tell the correct logic to complete the query using case statement You'll have to swap the syntax around. The case statement will be applied for every value you want to update... UPDATE table SET pay1 = CASE WHEN @columnname IN('name1') THEN pay1 * 100 ELSE

sql select min or max based on condition

无人久伴 提交于 2019-12-05 14:26:34
Hi all I'm trying to find a way to select min or max from a range of data based on these conditions: If setuptime and processtime cols are all 0 select MIN(oprNum) (operation hasn't started yet so get first oprnum) If setuptime and process time are not 0, get max oprnum (active operation). Based on either of these I want ONE row... Please see attached example of data. Thanks! This is part of a much larger query so i need 1 output row per prodid... +------------+--------+---------+--------------------+--------------------+ | ProdId | OprNum | Company | SetupTime | ProcessTime | +------------+--

How to figure out what object is represented by associatedObjectId during blocking?

喜你入骨 提交于 2019-12-05 13:24:09
I got report from another team about blocking in SQL Server. Looking at results of Exec sp_who2 and query from blog by Glenn Berry SELECT blocking.session_id AS blocking_session_id ,blocked.session_id AS blocked_session_id ,waitstats.wait_type AS blocking_resource ,waitstats.wait_duration_ms ,waitstats.resource_description ,blocked_cache.text AS blocked_text ,blocking_cache.text AS blocking_text FROM sys.dm_exec_connections AS blocking INNER JOIN sys.dm_exec_requests blocked ON blocking.session_id = blocked.blocking_session_id CROSS APPLY sys.dm_exec_sql_text(blocked.sql_handle) blocked_cache

Unable to use SSIS's SSDT in VS2010 / SQL Server2012

我怕爱的太早我们不能终老 提交于 2019-12-05 13:14:28
I have a full version of SqlServer 2012 installed on my local machine and have installed SSDT from the installation package (and updates) I've then opened the SSDT visual studio application and installed the SSDT package from Microsoft which appears to have worked correctly. However, when I attempt to start an Integration Services project I get the following error message: Microsoft Visual Studio is unable to load this document: To design Integration Services packages in SQL Server Data Tools (SSDT). SSDT has to be installed by one of these editions of SQL Server 2012: Standard. Enterprise.

SQL: Count Users with Activity in the Past Week

懵懂的女人 提交于 2019-12-05 13:12:43
I am trying to count the number of users who have had at least two sessions within 7 days of OR ten in 30 days of all dates. My data is as follow: Date UserID SessionID 1/1/2013 Bob1234 1 2/1/2013 Bob1234 2 2/2/2013 Bob1234 3 2/3/2013 Cal5678 4 Which would result in the following table (only select dates shown) Date CountActiveUsers 1/1/2013 1 1/15/2013 0 2/2/2013 1 2/3/2013 2 The real data set has values for all dates in a continuous data range and the results table should have an entry for every date. SessionIDs are unique and a UserID always refers to the same person. So far I have two