sql-server-2008-r2

SQL Query sort by closest match

陌路散爱 提交于 2019-12-06 06:57:03
问题 We have a Locations search page that is giving us a challenge I've never run across before. In our database, we have a list of cities, states, etc. with the corresponding geocodes. All was working fun until now... We have two locations in a city named "Black River Falls, WI" and we've recently opened one in "River Falls, WI". So our table has records as follows: Location City State ------------------------------------- 1 Black River Falls WI 2 Black River Falls WI 3 River Falls WI Obviously

Creating procedure SQL Server 2008 R2

会有一股神秘感。 提交于 2019-12-06 06:44:10
I'm new to SQL, and I'm trying to learn it. I'm creating a procedure to INSERT Members into Member table. Im using this (which ofc works): CREATE PROCEDURE InsertMember AS INSERT INTO Member (FirstName, LastName, City) VALUES ('Blla', 'Blla', 'Blla') But... I want to know if it is possible to create a procedure which when I execute it, to ask user for input. For example: When I execute the procedure it should go like this: @FirstName = 'SOME FIRSTNAME' @LastName = 'SOME LASTNAME' @City = 'SOME CITY' I didn't try anything yet, because I don't know if that is possible. Help please ? Any

Comma Delimited Result set + SQL Query

北战南征 提交于 2019-12-06 04:47:59
I got two tables with data as listed below: Table1: Student Table2: Subject I need the output as: I got this acheived with below query using for XML PATH Code: WITH cte AS ( SELECT Stu.Student_Id , Stu.Student_Name , ( SELECT Sub.[Subject] + ',' FROM [Subject] AS Sub WHERE Sub.Student_Id = Stu.Student_Id ORDER BY Sub.[Subject] FOR XML PATH('') ) AS [Subjects] FROM dbo.Student AS Stu ) SELECT Student_id [Student Id] , student_name [Student Name] , SUBSTRING(Subjects, 1, ( LEN(Subjects) - 1 )) AS [Student Subjects] FROM cte My question is there a better way to do this without using XML Path?

Rails use MS SQL server database

倖福魔咒の 提交于 2019-12-06 04:40:47
问题 In my rails app, I need to use an existing MS SQL database. I have tried following this manual: http://rubyrailsandwindows.blogspot.com/2008/03/rails-2-and-sql-server-2008-on-windows_24.html I do not know how to do models, scaffolds, etc because rails cannot simply use another db. I didn't want to run rake db:migrate for my tables in the MS SQL database. What else do I need to write in the model? If simple: use tables from the MS SQL server's database, but I also want to solve problem with

How do I exclude rows when an incremental value starts over?

空扰寡人 提交于 2019-12-06 03:35:45
I am a newbie poster but have spent a lot of time researching answers here. I can't quite figure out how to create a SQL result set using SQL Server 2008 R2 that should probably be using lead/lag from more modern versions. I am trying to aggregate data based on sequencing of one column, but there can be varying numbers of instances in each sequence. The only way I know a sequence has ended is when the next row has a lower sequence number. So it may go 1-2, 1-2-3-4, 1-2-3, and I have to figure out how to make 3 aggregates out of that. Source data is joined tables that look like this (please

T-SQL join to get both mated and non mated start and stop records

给你一囗甜甜゛ 提交于 2019-12-06 03:04:33
问题 I have a poorly designed table that I did not design and cannot fix/change because a 3rd party blackberry app that writes to it. The meat is that there is a start record and a stop record for tracking events with NO connection or validation that there is a match. The blackberry app does nothing to tie these records together. I have tried to create a join on its self and create temp tables with the begin and one with the end to full outer join them. The problem is that I have duplicate entries

TSQL alter table adding constraint for both cascade on delete and update

谁说胖子不能爱 提交于 2019-12-06 02:56:21
I'm trying to create a constraint with both on update and delete in tsql. I've tried a couple of different methods, and now I'm a little stuck & frustrated - seems so simple. I know you can't alter an existing constraint so I'm not sure about how to do this; alter table AllowedCars add constraint FK_AllowedCars_CarID foreign key (CarID) references Cars(LocusID) on delete cascade, constraint FK_AllowedCars_CarID foreign key (CarID) references Cars(CarID) on update cascade or this; alter table AllowedCars add constraint FK_AllowedCars_CarID foreign key (CarID) references Cars(CarID) on delete

SQL get rows in '= ALL' style

烈酒焚心 提交于 2019-12-06 02:23:13
I have simple many-to-many relation in table Product_Category (MSSQL 2008 r2): CREATE TABLE #Product_Category (ProductId int, CategoryId int); go INSERT INTO #Product_Category (ProductId, CategoryId) VALUES (1, 200); go INSERT INTO #Product_Category (ProductId, CategoryId) VALUES (2, 200); go INSERT INTO #Product_Category (ProductId, CategoryId) VALUES (2, 400); go INSERT INTO #Product_Category (ProductId, CategoryId) VALUES (3, 300); go INSERT INTO #Product_Category (ProductId, CategoryId) VALUES (2, 300); go DROP TABLE #Product_Category How can I select ProductId with condition: CategoryId =

CASE THEN clause always evaluated

≯℡__Kan透↙ 提交于 2019-12-06 02:09:21
问题 I'm doing a SELECT which uses CASE to convert nvarchar values into a proper type, something like this: SELECT CASE WHEN @propType = 'money' THEN convert(money, datavalue) [...] ELSE datavalue END FROM [...] However, it seems the convert is always executed, even when @propType is not equal to money. Runnable example: declare @proptype nvarchar(50)= 'nvarchar' declare @val nvarchar(10) = 'test' select case @proptype when 'money' then convert(money, @val) else @val end Why is this, and how can I

How do UPDATE locks prevent a common form of deadlock?

为君一笑 提交于 2019-12-06 00:56:28
问题 The SQL Server books online say that UPDATE locks prevent a common form of deadlock: Update Locks Update (U) locks prevent a common form of deadlock. How do they prevent a common form of deadlock? What do i mean by that? A common form of deadlock is when two processes attempt to escalate from a Shared (S) lock (i.e. read lock ) to a Exclusive (X) lock: Process A Process B ======================== ======================== Acquire Shared lock Acquire Shared lock Attempt to escalate to X