sql-server-2012

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

Recursively retrieve LAG() value of previous record

孤者浪人 提交于 2019-12-03 14:19:59
I've made the following calculation involving LAG(): (lag(fValue,1,fValue) OVER (PARTITION BY Cluster ORDER BY iSequence) + fValue) / 2 as fValueAjusted It takes the previous (based on iSequence) record's fValue, sums with current one, and divides it by 2. But, instead of using fValue, I must do that using previous record's fValueAjusted. It means that first record's fValueAjusted will be its own fValue. Second record's fValueAjusted will be based on first record's fValue. And, starting from third record, it's calculation will be based on previous record's fValueAjusted. I need fValueAjusted

How can LIKE '%…' seek on an index?

巧了我就是萌 提交于 2019-12-03 13:47:02
I would expect these two SELECT s to have the same execution plan and performance. Since there is a leading wildcard on the LIKE , I expect an index scan. When I run this and look at the plans, the first SELECT behaves as expected (with a scan). But the second SELECT plan shows an index seek, and runs 20 times faster. Code: -- Uses index scan, as expected: SELECT 1 FROM AccountAction WHERE AccountNumber LIKE '%441025586401' -- Uses index seek somehow, and runs much faster: declare @empty VARCHAR(30) = '' SELECT 1 FROM AccountAction WHERE AccountNumber LIKE '%441025586401' + @empty Question:

how to use msbuild properties in sqlproj (SQL Server 2012) script

佐手、 提交于 2019-12-03 13:24:12
I just upgraded my existing SQL Server 2008 r2 .dbproj to a SQL Server 2012 .sqlproj (using SQL Server Data Tools). Previously, I was able to define a SQLCMD variable in my project, and then define the value by editing the project file to use msbuild values by adding the following element: <ItemGroup> <SqlCommandVariableOverride Include="ProjectDirectory=$(MSBuildProjectDirectory)" /> </ItemGroup> Which I could then use in my PostDeployment script like this: SELECT * INTO dbo.MyTable FROM dbo.MyTable WHERE 1=2 BULK INSERT dbo.MyTable FROM '$(ProjectDirectory)\data\dbo.MyTable.dat' WITH

Calling a SOAP webservice from TSQL stored procedure

こ雲淡風輕ζ 提交于 2019-12-03 13:19:16
I am trying to build a stored procedure in TSQL to call a webservice. I've done this before in Oracle, but it seems like it's not so easy in MSSQL. There are of course many reasons not to do this in a stored proc, but since this procedure is only to be used in a daily batch, performance is not too much of a issue. The thing I want to do is as follows: Send a full name to the webservice, the webservice will return a name divided in things like first name, prefix, lastname, etc. The returned values will need to be written to a table. I found a interesting procedure at http://www.vishalseth.com

MSSQL 2012 creating CLR triggers for WCF fails

本秂侑毒 提交于 2019-12-03 12:24:01
问题 I've created system that uses CLR triggers to connect to WCF server and notify it about changes in DB. It runs ok on SQL server 2008 R2. Now im trying to migrate on SQL Server 2012. To use WCF i need to load SMDiagnostics.dll assembly along the others. Ive checked that clr is enabled in db , and set trustworthy to be "on", ive disabled WCF debuging, ive checked that SQL server runs under Local System account so there is no problems with permissions. Now my problem is that when i run following

Select the top 1 row from each group

吃可爱长大的小学妹 提交于 2019-12-03 12:13:50
I have a table that lists the versions of software that are installed: id | userid | version | datetime ----+--------+---------+------------------------ 111 | 75 | 10075 | 2013-03-12 13:40:58.770 112 | 75 | 10079 | 2013-03-12 13:41:01.583 113 | 78 | 10065 | 2013-03-12 14:18:24.463 114 | 78 | 10079 | 2013-03-12 14:22:20.437 115 | 78 | 10079 | 2013-03-12 14:24:01.830 116 | 78 | 10080 | 2013-03-12 14:24:06.893 117 | 74 | 10080 | 2013-03-12 15:31:42.797 118 | 75 | 10079 | 2013-03-13 07:03:56.157 119 | 75 | 10080 | 2013-03-13 07:05:23.137 120 | 65 | 10080 | 2013-03-13 07:24:33.323 121 | 68 | 10080

How do I use spatials to search a radius of zip codes?

廉价感情. 提交于 2019-12-03 12:03:27
问题 Background I'm writing an application which finds events within a certain radius of a zip code. You can think of this like ticketmaster, where you type in your zip code and all of the concerts in the radius of x show up. I have a database table which has the zip code, and each zip codes' Latitude and Longitude. I also have an 'EventListings' table where each 'Event' has a ZipCode field. Problem Currently, I'm using the Haversine formula in a Linq-to-Entities query in my service layer to find

How to use OFFSET and Fetch without Order by in SQL Server

百般思念 提交于 2019-12-03 10:50:32
I want use OFFSET and Fetch in my SQL server 2012 query.But without any order by.I can not use order by.Because my sort order will be lost. How can I use OFFSET and Fetch without order by and row number and where in my query? My 2 select tables have same structure. INSERT INTO @TempTable [some columns] select [some columns] from table1 order by col1 INSERT INTO @TempTable [same columns] select [some columns] from table2 order by col2 select * from @TempTable OFFSET 20 ROWS FETCH NEXT 50 ROWS ONLY This query has syntax error at OFFSET keyword. By adding an identity column to the temp table

How do I generate insert statements with Sql server 2012? [duplicate]

最后都变了- 提交于 2019-12-03 10:45:23
问题 This question already has answers here : What is the best way to auto-generate INSERT statements for a SQL Server table? (21 answers) Closed 3 years ago . I have sql server 2012 express and just installed the latest version of ssms tools from http://www.ssmstoolspack.com/Download With sql server 2008 express and ssms tools I was able to right click on a table and see an ssms tools option that allowed me to generate insert statements for a table but now I don't see it? Anyone know how I can