sql-server-2005

How to get the max row number per group/partition in SQL Server?

这一生的挚爱 提交于 2019-12-18 03:27:25
问题 I'm using SQL Server 2005. I have a payments table with payment id's, user id's, and timestamps. I want to find the most recent payment for each user. This is easy to search and find an answer for. What I also want to know though is if the most recent payment is the user's first payment or not. I have the following which will number each user's payments: SELECT p.payment_id, p.user_id, ROW_NUMBER() OVER (PARTITION BY p.user_id ORDER BY p.payment_date) AS paymentNumber FROM payment p I'm not

SQL Server 2005: Insert multiple rows with single query

半腔热情 提交于 2019-12-18 03:11:13
问题 This should be a fairly straightforward question, but I haven't been able to find a solid answer online. I'm trying to insert multiple rows into the same table, but with only one statement. The most popular I've seen online is the following, but I've read that it only works with SQL Server 2008: INSERT INTO Table (Name, Location) VALUES ('Name1', 'Location1'), ('Name2', 'Location2'), ('Name3', 'Location3'), etc... I'd prefer this method if it will work with SQL Server 2005, but I don't think

How do I use BCP or Sql Server Management Studio to get BLOB data out of Sql Server?

这一生的挚爱 提交于 2019-12-18 02:50:04
问题 I'm sorry if this question has been asked already, but I couldn't find it anywhere. I have a table that stores files as BLOBS. The column that holds the file is an image datatype. I would like to be able to extract the binary data out of the column and turn it in to an actual file. I would ideally like to be able to do this with BCP or management studio if possible. I have tried BCP, but for some reason when I try and pull out an office document Word thinks it's corrupt. Here's what I've

How to split a string in T-SQL?

独自空忆成欢 提交于 2019-12-18 02:48:03
问题 I have a varchar @a='a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p' , wich have | delimitted values. I want to split this variable in a array or a table. Do anyone have any idea about this. 回答1: Use a table valued function like this, CREATE FUNCTION Splitfn(@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(

SQL Server 2005: Determine datatype of variable

ⅰ亾dé卋堺 提交于 2019-12-18 02:38:23
问题 Is it possible to determine the type of a local variable at runtime in TSQL? For example, say I wanted to do something along these lines: IF ( @value IS INTEGER ) Or IF ( TYPEOF(@value) = <whatever> ) Does anyone know of any way to accomplish this? EDIT: This is not for a specific task, this is more of a general knowledge question. I do appreciate answers that indicate that the type should be known since it is declared within the same batch, I am curious as to whether the type can be

Flattening intersecting timespans

廉价感情. 提交于 2019-12-17 23:46:39
问题 I have lots of data with start and stop times for a given ID and I need to flatten all intersecting and adjacent timespans into one combined timespan. The sample data posted below is all for the same ID so I didn't list it. To make things a bit clearer, take a look at the sample data for 03.06.2009: The following timespans are overlapping or contiunous and need to merge into one timespan 05:54:48 - 10:00:13 09:26:45 - 09:59:40 The resulting timespan would be from 05:54:48 to 10:00:13. Since

SQL join format - nested inner joins

ぃ、小莉子 提交于 2019-12-17 23:26:52
问题 I have the following SQL statement in a legacy system I'm refactoring. It is an abbreviated view for the purposes of this question, just returning count(*) for the time being. SELECT COUNT(*) FROM Table1 INNER JOIN Table2 INNER JOIN Table3 ON Table2.Key = Table3.Key AND Table2.Key2 = Table3.Key2 ON Table1.DifferentKey = Table3.DifferentKey It is generating a very large number of records and killing the system, but could someone please explain the syntax? And can this be expressed in any other

Calling stored procedure from another stored procedure SQL Server

£可爱£侵袭症+ 提交于 2019-12-17 23:26:21
问题 I have 3 insert stored procedures each SP inserts data in 2 different tables Table 1 Table 2 idPerson idProduct name productName phoneNumber productdescription FK-idProduct SP for table 1 SP for table 2 create procedure test1 create procedure test2 WITH WITH EXECUTE as caller EXECUTE as caller AS AS declare declare @idPerson int, @idProduct int, @name varchar(20), @productName varchar(50), @phone varchar(20) @productoDescription varchar(50) SET nocount on; SET nocount on; Begin Begin insert

Is there any way to view whitespace in the query editor for SQL Server Management Studio Express 2005?

无人久伴 提交于 2019-12-17 23:08:40
问题 I have it enabled in Visual Studio 2008, but I'd really like to enable this feature in SQL Server Management Studio 2005 Express. Does anyone know if its possible? Maybe in a later version? EDIT: Sorry, I meant specifically in the text/query editor. 回答1: In case you still need to know how to do this. Edit the Registry Key in HKEY_CURRENT_USER : Software\Microsoft\Microsoft SQL Server\xx\Tools\Shell\Text Editor\Visible Whitespace Where xx is the version you have (90 is 2005, 100 is 2008) And

How to list out all Foreign Keys with “WITH NOCHECK” in SQL Server

十年热恋 提交于 2019-12-17 22:44:13
问题 Does anyone know a query for listing out all foreign keys in a database with "WITH NOCHECK" description applied to it? (removing them will boost performance and stability). 回答1: The following will return the name of the foreign keys in the current database that are disabled i.e. WITH NOCHECK For SQL Server 2005/2008: select * from sys.foreign_keys where is_disabled=1 There was some discussion in the answer about the difference between disabled & not trusted. What's below explains the