sql-server-2005

Is it possible to perform multiple updates with a single UPDATE SQL statement?

喜欢而已 提交于 2019-12-17 06:09:35
问题 Let's say I have a table tbl with columns id and title . I need to change all values of title column: from 'a-1' to 'a1', from 'a.1' to 'a1', from 'b-1' to 'b1', from 'b.1' to 'b1'. Right now, I'm performing two UPDATE statements: UPDATE tbl SET title='a1' WHERE title IN ('a-1', 'a.1') UPDATE tbl SET title='b1' WHERE title IN ('b-1', 'b.1') This isn't at all a problem, if the table is small, and the single statement completes in less than a second and you only need a few statements to execute

Is it possible to perform multiple updates with a single UPDATE SQL statement?

前提是你 提交于 2019-12-17 06:09:22
问题 Let's say I have a table tbl with columns id and title . I need to change all values of title column: from 'a-1' to 'a1', from 'a.1' to 'a1', from 'b-1' to 'b1', from 'b.1' to 'b1'. Right now, I'm performing two UPDATE statements: UPDATE tbl SET title='a1' WHERE title IN ('a-1', 'a.1') UPDATE tbl SET title='b1' WHERE title IN ('b-1', 'b.1') This isn't at all a problem, if the table is small, and the single statement completes in less than a second and you only need a few statements to execute

How to use alias column name in where clause in SQL Server

风流意气都作罢 提交于 2019-12-17 05:07:30
问题 When I tried to perform the below code in SQL Server 2005 I am getting the error Invalid column name DistanceFromAddress Code: select SQRT(POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) + POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)) * 62.1371192 AS DistanceFromAddress from tblProgram where DistanceFromAddress < 2 I am getting the values correctly using the select statement,but when i tried to check the condition where DistanceFromAddress

Fastest way to update 120 Million records

北城余情 提交于 2019-12-17 04:26:23
问题 I need to initialize a new field with the value -1 in a 120 Million record table. Update table set int_field = -1; I let it run for 5 hours before canceling it. I tried running it with transaction level set to read uncommitted with the same results. Recovery Model = Simple. MS SQL Server 2005 Any advice on getting this done faster? 回答1: The only sane way to update a table of 120M records is with a SELECT statement that populates a second table. You have to take care when doing this.

SQL query to find Missing sequence numbers

杀马特。学长 韩版系。学妹 提交于 2019-12-17 04:25:31
问题 I have a column named sequence . The data in this column looks like 1, 2, 3, 4, 5, 7, 9, 10, 15. I need to find the missing sequence numbers from the table. What SQL query will find the missing sequence numbers from my table? I am expecting results like Missing numbers --------------- 6 8 11 12 13 14 I am using only one table. I tried the query below, but am not getting the results I want. select de.sequence + 1 as sequence from dataentry as de left outer join dataentry as de1 on de.sequence

Delete duplicate records from a SQL table without a primary key

耗尽温柔 提交于 2019-12-17 04:17:37
问题 I have the below table with the below records in it create table employee ( EmpId number, EmpName varchar2(10), EmpSSN varchar2(11) ); insert into employee values(1, 'Jack', '555-55-5555'); insert into employee values (2, 'Joe', '555-56-5555'); insert into employee values (3, 'Fred', '555-57-5555'); insert into employee values (4, 'Mike', '555-58-5555'); insert into employee values (5, 'Cathy', '555-59-5555'); insert into employee values (6, 'Lisa', '555-70-5555'); insert into employee values

How to check which locks are held on a table

吃可爱长大的小学妹 提交于 2019-12-17 04:12:20
问题 How can we check which database locks are applied on which rows against a query batch? Any tool that highlights table row level locking in real time? DB: SQL Server 2005 回答1: To add to the other responses, sp_lock can also be used to dump full lock information on all running processes. The output can be overwhelming, but if you want to know exactly what is locked, it's a valuable one to run. I usually use it along with sp_who2 to quickly zero in on locking problems. There are multiple

Convert varchar to uniqueidentifier in SQL Server

隐身守侯 提交于 2019-12-17 03:58:17
问题 A table I have no control of the schema for, contains a column defined as varchar(50) which stores uniqueidentifiers in the format 'a89b1acd95016ae6b9c8aabb07da2010' (no hyphens) I want to convert these to uniqueidentifiers in SQL for passing to a .Net Guid. However, the following query lines don't work for me: select cast('a89b1acd95016ae6b9c8aabb07da2010' as uniqueidentifier) select convert(uniqueidentifier, 'a89b1acd95016ae6b9c8aabb07da2010') and result in: Msg 8169, Level 16, State 2,

How do you kill all current connections to a SQL Server 2005 database?

╄→гoц情女王★ 提交于 2019-12-17 03:44:19
问题 I want to rename a database, but keep getting the error that 'couldn't get exclusive lock' on the database, which implies there is some connection(s) still active. How can I kill all the connections to the database so that I can rename it? 回答1: The reason that the approach that Adam suggested won't work is that during the time that you are looping over the active connections new one can be established, and you'll miss those. You could instead use the following approach which does not have

How do I group on continuous ranges

╄→гoц情女王★ 提交于 2019-12-17 03:42:22
问题 I know some basic sql, but this one is beyond me. I have looked high and low but no dice. I need a view of the following data, I can do this in the application layer code. But unfortunately for this particular one, the code must be put in the data layer. I am using T-SQL. Table Date Crew DayType 01-02-11 John Doe SEA 02-02-11 John Doe SEA 03-02-11 John Doe SEA 04-02-11 John Doe HOME 05-02-11 John Doe HOME 06-02-11 John Doe SEA I need a view like this DateFrom DateTo Name DayType 01-02-11 03