开发者

help with stored procedure

i am looking at this site:

http://cloudexchange.cloudapp.net/stackoverflow/s/84/rising-stars-top-50-users-ordered-on-rep-per-day

set nocount on

DECLARE @endDate date
SELECT @endDate = max(CreationDate) from Posts

set nocount off

SELECT TOP 50
    Id AS [User Link], Reputation, Days,
    Reputation/Days开发者_开发问答 AS RepPerDays
FROM (
    SELECT *,
        CONVERT(int, @endDate - CreationDate) as Days
    FROM Users
) AS UsersAugmented
WHERE
    Reputation > 5000
ORDER BY
    RepPerDays DESC

i am also a beginner at SQL. i have the following questions about this code:

  1. is this mysql or mssql?
  2. what does this do? set nocount off
  3. why is this in brackets? [User Link]
  4. what does this do? CONVERT(int, @endDate - CreationDate) as Days

thanks!


  1. Looks like MSSQL (T-SQL)
  2. "Set nocount on" in the previous statement prevents the number of rows that are selected from being returned. "Set nocount off" turns this off again (row count will be returned).
  3. You can't have spaces in column names unless you surround them with brackets.
  4. I'm assuming CreationDate is a date field. This statement will return the number of days (as an integer) between the @endDate parameter and the CreationDate field.


1. It's Microsoft SQL Azure Database, but the syntax (Transact-SQL or T-SQL) is the same as for SQL Server (because Azure Database is "based on SQL Server technologies").

2. This is described in the T-SQL documentation for SET NOCOUNT.

When SET NOCOUNT is ON, the count (indicating the number of rows affected by a Transact-SQL statement) is not returned.

3. You need to put square brackets around a column name if it contains any character that would confuse the parser. In this case it is necessary because of the space.

4. To understand CONVERT(int, @endDate - CreationDate) as Days let's split it into smaller pieces and handle them separately:

  • @endDate - CreationDate calculates the time between the most recent post and the date that the user's account was created. This gives the time that the user's account has existed up to the last data dump.

  • CONVERT(int, @endDate - CreationDate) converts the result of the previous calculation to an integer, measured in days.

  • as Days is an alias so that the result is easy to read.


  1. MSSQL
  2. NOCOUNT OFF states that you don't wish to have the total number of rows affected returned. See http://msdn.microsoft.com/en-us/library/ms189837.aspx
  3. Returns column "ID" as "User Link" (brackets required because of the space).
  4. Returns an int as the end date parameter minus the creation date as "Days"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜