sql-server-2000

SQL: Numbering the rows returned by a SELECT statement

余生长醉 提交于 2019-12-19 03:36:06
问题 Suppose I have a SELECT statement that returns some set of results. Is there some way I can number my results in the following way: SELECT TOP 3 Name FROM PuppyNames ORDER BY NumberOfVotes would give me... Fido Rover Freddy Krueger ...but I want... 1, Fido 2, Rover 3, Freddy Krueger where of course the commas signify that the numbers are in their own column. [I am using SQL Server 2000.] 回答1: In Microsoft SQL Server 2005, you have the ROW_NUMBER() function which does exactly what you want. If

SQL: Numbering the rows returned by a SELECT statement

故事扮演 提交于 2019-12-19 03:36:06
问题 Suppose I have a SELECT statement that returns some set of results. Is there some way I can number my results in the following way: SELECT TOP 3 Name FROM PuppyNames ORDER BY NumberOfVotes would give me... Fido Rover Freddy Krueger ...but I want... 1, Fido 2, Rover 3, Freddy Krueger where of course the commas signify that the numbers are in their own column. [I am using SQL Server 2000.] 回答1: In Microsoft SQL Server 2005, you have the ROW_NUMBER() function which does exactly what you want. If

Get the last 24 hour job record form msdb.dbo.sysjobhistory

一曲冷凌霜 提交于 2019-12-19 02:46:25
问题 I want write a query to get the last 24 hours worth of job record from the "msdb.dbo.sysjobhistory" table, but I can't get because I get the "run_date" and "run_time" columns are returned as a number. How can I convert the "run_date" and "run_time" columns into a datetime variable, and use this to get the last 24 hour job history? 回答1: Check out this post - it shows how to "decode" those run_date columns from sysjobhistory . You should be able to get the entries from the last 24 hours with a

Prevent a Stored Procedure from being executed twice at the same time

别等时光非礼了梦想. 提交于 2019-12-18 16:45:31
问题 I have a stored procedure for SQL Server 2000 that can only have a single instance being executed at any given moment. Is there any way to check and ensure that the procedure is not currently in execution? Ideally, I'd like the code to be self contained and efficient (fast). I also don't want to do something like creating a global temp table checking for it's existence because if the procedure fails for some reason, it will always be considered as running... I've searched, I don't think this

Wrong week number using DATEPART in SQL Server

↘锁芯ラ 提交于 2019-12-18 15:53:44
问题 I've got the problem that select datepart(ww, '20100208') is returning as result week 7 in SQL Server 2000. But 08.02.2010 should be week 6 according to the ISO 8601 specification! This is causing problems in delivery week calculations. What should I do to get week number values according to ISO 8601? 回答1: You can do this within SQL 2008 very easily as it now supports isoww as the first datepart argument. However, this wasn't in SQL 2000 (or 2005). There is a function in this article which

How to catch the output of a DBCC-Statement in a temptable

南楼画角 提交于 2019-12-18 12:49:45
问题 I tried the following on SQL-Server: create table #TmpLOGSPACE( DatabaseName varchar(100) , LOGSIZE_MB decimal(18, 9) , LOGSPACE_USED decimal(18, 9) , LOGSTATUS decimal(18, 9)) insert #TmpLOGSPACE(DatabaseName, LOGSIZE_MB, LOGSPACE_USED, LOGSTATUS) DBCC SQLPERF(LOGSPACE); ...but this rises an syntax-error... Any sugestions? 回答1: Put the statement to be run inside EXEC('') insert #TmpLOGSPACE(DatabaseName, LOGSIZE_MB, LOGSPACE_USED, LOGSTATUS) EXEC('DBCC SQLPERF(LOGSPACE);') 回答2: This does not

Select column, if blank select from another

柔情痞子 提交于 2019-12-18 10:48:28
问题 How does one detect whether a field is blank (not null) and then select another field if it is? What I really need is a IsBlank function that works the same as IsNull but with with blanks. REPLACE doesn't work with blanks, COALESCE only works with NULLS. 回答1: How about combining COALESCE and NULLIF. SELECT COALESCE(NULLIF(SomeColumn,''), ReplacementColumn) FROM SomeTable 回答2: You can use a CASE statement for this select Case WHEN Column1 = '' OR Column1 IS NULL OR LEN (TRIM (Column1)) = 0

Distinct in SQL Server

巧了我就是萌 提交于 2019-12-18 09:14:48
问题 I am executing the following query, Select distinct a.cr_id, Case When ca.ca_vote = 'Approve' and ca.ca_title='MANAGER' Then ca.ca_email When ca.ca_vote = 'Reject' Then '' When ca.ca_vote = 'Pending' Then '' When ca.ca_vote = 'IN PROCESS' Then '' End as ca_email from credit a inner join credit_approvals ca on ca.c_id=a.cr_id where a.cr_cs_date between Convert(varchar(20),'11/16/2011',101) and dateadd(day,1,convert (varchar(20),'11/16/2011',101)) order by a.cr_id Despite distinct for cr_id ,

Stored Procedure failing on a specific user

ぐ巨炮叔叔 提交于 2019-12-18 08:55:19
问题 I have a Stored Procedure that is constantly failing with the error message "Timeout expired," on a specific user. All other users are able to invoke the sp just fine, and even I am able to invoke the sp normally using the Query Analyzer--it finishes in just 10 seconds. However with the user in question, the logs show that the ASP always hangs for about 5 minutes and then aborts with a timeout. I invoke from the ASP page like so " EXEC SP_TV_GET_CLOSED_BANKS_BY_USERS '006111' " Anybody know

how to get the next autoincrement value in sql

一个人想着一个人 提交于 2019-12-18 04:36:11
问题 I am creating a winform application in c#.and using sql database. I have one table, employee_master , which has columns like Id, name, address and phone no . Id is auto increment and all other datatypes are varchar . I am using this code to get the next auto increment value: string s = "select max(id) as Id from Employee_Master"; SqlCommand cmd = new SqlCommand(s, obj.con); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); int i = Convert.ToInt16(dr["Id"].ToString()); txtId.Text = (i + 1)