sql-server-2008-r2

Fastest way to find string by substring in SQL?

筅森魡賤 提交于 2019-12-17 23:38:34
问题 I have huge table with 2 columns: Id and Title. Id is bigint and I'm free to choose type of Title column: varchar, char, text, whatever. Column Title contains random text strings like "abcdefg", "q", "allyourbasebelongtous" with maximum of 255 chars. My task is to get strings by given substring. Substrings also have random length and can be start, middle or end of strings. The most obvious way to perform it: SELECT * FROM t LIKE '%abc%' I don't care about INSERT, I need only to do fast

SQL Server stops loading assembly

。_饼干妹妹 提交于 2019-12-17 21:58:59
问题 We have developed an assembly for SQL Server 2008 R2. The assembly has been working for a week. The managed stored proc inside the assembly was working fine for the whole week and then it stops working. We have been seeing this problem couple times. The way to make it work again is to restart the SQL Server. Msg 10314, Level 16, State 11, Line 4 An error occurred in the Microsoft .NET Framework while trying to load assembly id 65536. The server may be running out of resources, or the assembly

SQL query to convert date ranges to per day records

走远了吗. 提交于 2019-12-17 20:59:32
问题 Requirements I have data table that saves data in date ranges. Each record is allowed to overlap previous record(s) (record has a CreatedOn datetime column). New record can define it's own date range if it needs to hence can overlap several older records. Each new overlapping record overrides settings of older records that it overlaps. Result set What I need to get is get per day data for any date range that uses record overlapping . It should return a record per day with corresponding data

SQL join following foreign key: statically check that LHS is key-preserved

不问归期 提交于 2019-12-17 19:58:32
问题 Often you join two tables following their foreign key, so that the row in the RHS table will always be found. Adding the join does not affect the number of rows affected by the query. For example create table a (x int not null primary key) create table b (x int not null primary key, y int not null) alter table a add foreign key (x) references b (x) Now, assuming you set up some data in these two tables, you can get a certain number of rows from a: select x from a Adding a join to b following

Cannot persist computed column - not deterministic

半腔热情 提交于 2019-12-17 19:39:51
问题 I have this function for a computed column : CREATE FUNCTION [dbo].[GetAllocatedStartTime](@Year INT, @Week INT) RETURNS DATETIME WITH schemabinding AS BEGIN RETURN dateadd(week,@Week-(1),dateadd(day,(-1),dateadd(week,datediff(week,(0),CONVERT([varchar](4),@Year,(0))+'-01-01'),(1)))) END GO I added the WITH schemabinding in the hope it would make it deterministic so I can persist it. It should be as the two inputs [Week] and [Year] will always yield the same results. The exact error is :

How to concatenate all columns in a select with SQL Server

我只是一个虾纸丫 提交于 2019-12-17 19:29:25
问题 I need my select to have a pattern like this: SELECT '<text> ' + tbl.* + ' </text>' FROM table tbl; The ideal solution would have all the columns separated by a comma in order to have that output: SQL result for Table 1 with two columns: '<text>col1, col2</text>' SQL result for Table 2 with three columns: '<text>col1, col2, col3</text>' I tried to use the CONCAT(...) function like this: SELECT CONCAT('<text>', tbl.*, '</text>') FROM table2 tbl But I understand it is not so simple because the

Select databases which only contain specific table

ⅰ亾dé卋堺 提交于 2019-12-17 19:28:10
问题 I'm looking for a way to select all databases on my sql server, which only contain the table "dbo.mytable" How can i do this ? I already have these two sql queries : Select name From sys.databases Where database_id > 5 And IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mytable]') AND type in (N'U')) Select 1 [Exists] Else Select 0 [Exists] The first query, lists all databases on my sql server, and the second checks if dbo.mytable exists. I would like to merge them.

Listing all Data Sources and their Dependencies (reports, items, etc) in SQL Server 2008 R2

若如初见. 提交于 2019-12-17 18:05:11
问题 I am new to SQL Server, and I am sorry if there is an obvious solution to my question but I can't seem to find it. I am looking to generate a report (or list) of all the data sources and their individual dependencies on an SQL Server 2008 R2 (reporting server). I know that I can access each individual data source to get a list of all the items that are dependent on it. I have done this in the past but it is time consuming. Is there a way to get a report that would display all the data sources

Find Locked Table in SQL Server

蓝咒 提交于 2019-12-17 17:27:10
问题 How can we find which table is locked in the database? Please, suggest. 回答1: You can use sp_lock (and sp_lock2 ), but in SQL Server 2005 onwards this is being deprecated in favour of querying sys.dm_tran_locks: select object_name(p.object_id) as TableName, resource_type, resource_description from sys.dm_tran_locks l join sys.partitions p on l.resource_associated_entity_id = p.hobt_id 回答2: sp_lock When reading sp_lock information, use the OBJECT_NAME( ) function to get the name of a table from

Why is there a HUGE performance difference between temp table and subselect

青春壹個敷衍的年華 提交于 2019-12-17 17:26:43
问题 This is a question about SQL Server 2008 R2 I'm not a DBA, by far. I'm a java developer, who has to write SQL from time to time. (mostly embedded in code). I want to know if I did something wrong here, and if so, what I can do to avoid it to happen again. Q1: SELECT something FROM (SELECT * FROM T1 WHERE condition1) JOIN ... Q1 features 14 joins Q2 is the same as Q1, with one exception. (SELECT * FROM T1 WHERE condition1) is executed before, and stored in a temp table. This is not a