sql-server-2008-r2

Select subset of rows using Row_Number()

 ̄綄美尐妖づ 提交于 2019-12-03 16:21:27
Select id, name, ROW_NUMBER() OVER (ORDER BY id asc) as 'RowNo' from customers where RowNo between 50 AND 60 I am trying to select a subset of rows between 50 and 60 . The problem is 'RowNo' is an invalid column name. Thank you Using SQL SERVER 2008 R2 Michał Powaga Use your query as subquery like bellow: select * from ( Select id, name, ROW_NUMBER() OVER (ORDER BY id asc) as [RowNo] from customers ) t where RowNo between 50 AND 60 You can use CTE as well but whether to choose one over another read Difference between CTE and SubQuery? and check execution plan. You need to do something like

Code First can't enable migrations

霸气de小男生 提交于 2019-12-03 15:54:23
问题 I'm trying to enable migrations but it's throwing an exception: Checking if the context targets an existing database... System.TypeInitializationException: The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System

T-SQL Is it possible to do an Update / Insert with a single fast operation

隐身守侯 提交于 2019-12-03 14:29:46
Let's say I have a table and I want to insert a row. The new row's key may already match an existing row's key in the table, in which case I want to update the existing row. Or, it may not exist in the table, in which case the new row should be inserted. What is the most efficient way to perform such an operation? I was thinking of first doing a SELECT (perhaps with EXISTS ) to see if a particular key is present, followed by an UPDATE if present and an INSERT if not. You would probably need to keep an UPDLOCK and a HOLDLOCK for this combination of statements in order to avoid race conditions,

How to check blocking queries in SQL Server

一个人想着一个人 提交于 2019-12-03 14:27:27
I have one warehouse server which got data/sync from legacy system 24/7, I noticed some of my reports/sql jobs performance is uncertain and most of the time I heard from DBA team that my query is blocking to other sync process. From DBA team I came to know command i.e. EXEC SP_WHO2 by which I can identify spid of query which cause blocking by looking into column BlkBy. Please suggest me how I can avoid blocking and other ways to check blocking in SQL Server Apart from Sp_Who2 you can use following query to identify blocking in you SQL. SELECT db.name DBName, tl.request_session_id, wt.blocking

Database default value is not inserted while create record by entity framework

心已入冬 提交于 2019-12-03 14:21:27
I have a LoginRecord table in sqlserver 2008 with the following column structure- LoginId - int, identity UserId - int LoginDateTime- Allow nulls false,default value getdate() I am inserting new record by entity framework 6 as below- db.LoginRecords.Add(new LoginRecord() { UserId = UserId }); db.SaveChanges(); But in LoginDateTime table, null value is being inserted. It supposed to be current datetime. I am using database first approach. How can overcome this issue? The Mahahaj Combined my two comments into an answer. Try setting the "StoredGeneratedPattern" attribute of your datetime in the

Keep report header visible while scrolling

佐手、 提交于 2019-12-03 13:44:30
I often check the following option in the hope that it will be implemented when the report is rendered on the RS web portal - but it never works... The reports usually have some Row Groups - does this have an impact on whether this option will work? Or is this a bug in Reporting Services ? I find it usually doesnt work as advertised and you have to find the "Advanced mode" button (a strong candidate for the worst piece of UI ever) and then fiddle with several properties. Here's probably the best reference: http://blogs.msdn.com/b/robertbruckner/archive/2008/10/13/repeat-header-and-visible

What is imprecise column in SQL Server?

爱⌒轻易说出口 提交于 2019-12-03 12:45:19
Creating index on computed column of type nvarchar raises following error: Cannot create index or statistics 'MyIndex' on table 'MyTable' because the computed column 'MyColumn' is imprecise and not persisted. Consider removing column from index or statistics key or marking computed column persisted. What does imprecise column mean? UPDATE. The definition is following: alter table dbo.MyTable add [MyColumn] as dbo.MyDeterministicClrFunction(MyOtherColumn) go create index MyIndex on dbo.MyTable(MyColumn) go UPDATE2. The MyDeterministicClrFunction is defined as following: [SqlFunction

Select parent if all children meet criteria

為{幸葍}努か 提交于 2019-12-03 12:31:27
I have tables set up like so: Parent ------ id, ... Child ----- id, parent_id, x, y I want to find the Parents, or the distinct parent_id(s), if all of the rows in Child containing a given parent_id meet a criteria involving x and y(in my case x = y). For example: Parent ------ id 1 2 3 Child id, parent_id, x, y 1, 1, 2, 3 2, 1, 3, 4 3, 2, 5, 5 4, 2, 6, 7 5, 3, 8, 8 6, 3, 9, 9 would result in 3. Currently, I have a query that finds parent_ids that any of the children meet the criteria. I then use that to retrieve those records and check them in code if all the children meet the criteria. With

ALTER INDEX failed because of QUOTED_IDENTIFIER when running from sp_msForEachTable

放肆的年华 提交于 2019-12-03 12:18:16
When I try to rebuild an index on a table: ALTER INDEX ALL ON [dbo].[Allocations] REBUILD that works fine. But when I call EXECUTE sp_msForEachTable 'ALTER INDEX ALL ON ? REBUILD' I reach the same same table, and it fails with: Msg 1934, Level 16, State 1, Line 2 ALTER INDEX failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations. And to confirm that it's

sql query to select millions record very fast

為{幸葍}努か 提交于 2019-12-03 10:20:28
问题 I want to select millions record from a table and I am using select query for this. Currently it is taking few minutes to get data. Can I get it quickly. I am using SQL Server 2008 R2. I am using below query :- SELECT sum(Orders.BusinessVolumeTotal) as BusinessVolume, sum(Orders.CommissionableVolumeTotal) as CommissionableVolume, OrderTypes.OrderTypeDescription, Orders.OrderTypeID FROM Orders INNER JOIN OrderTypes ON Orders.OrderTypeID = OrderTypes.OrderTypeID WHERE Orders.OrderDate > convert