SQL Server面试题(请高人解答邮箱white-lx@163.com谢谢)

来源:互联网 发布:linux查看最后100行 编辑:程序博客网 时间:2024/04/27 14:04
Question 1:Can you use a batch SQL or store procedure to calculating the Number of Days in a Month
Answer 1:找出当月的天数

程序代码
select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' as datetime)))) 软件开发网

Question2:Can you use a SQL statement to calculating it!
How can I print "10 to 20" for books that sell for between $10 and $20,"unknown" for books whose price is null, and "other" for all other prices?
Answer 2:
select bookid,bookname,price=case when price is null then 'unknown'
       when  price between 10 and 20 then '10 to 20' else price end
from books
Question3:Can you use a SQL statement to finding duplicate values!
How can I find authors with the same last name?
You can use the table authors in datatabase pubs. I want to get the result as below:
Output:
程序代码
au_lname                                 number_dups
---------------------------------------- -----------
Ringer                                   2
(1 row(s) affected)
Answer 3
select au_lname,number_dups=count(1) from authors group by au_lname
Question4:Can you create a cross-tab report in my SQL Server!
How can I get the report about sale quality for each store and each quarter and the total sale quality for each quarter at year 1993?
You can use the table sales and stores in datatabase pubs.
Table Sales record all sale detail item for each store. Column store_id is the id of each store, ord_date is the order date of each sale item, and column qty is the sale qulity. Table stores record all store information.
I want to get the result look like as below:
Output: 软件开发网

程序代码
stor_name                                Total       Qtr1        Qtr2        Qtr3        Qtr4        
---------------------------------------- ----------- ----------- ----------- ----------- -----------
Barnum's                                 50          0           50          0           0
Bookbeat                                 55          25          30          0           0
Doc-U-Mat: Quality Laundry and Books     85          0           85          0           0
Fricative Bookshop                       60          35          0           0           25

Total                                    250         60          165         0           25

Answer 4:用动态SQL实现 http://www.mscto.com
Question5: The Fastest Way to Recompile All Stored Procedures
I have a problem with a database running in SQL Server 6.5 (Service Pack 4). We moved the database (object transfer) from one machine to another last night, and an error (specific to a stored procedure) is cropping up. However, I can't tell which procedure is causing it. Permissions are granted in all of our stored procedures; is there a way from the isql utility to force all stored procedures to recompile?
Tips: sp_recompile can recomplie a store procedure each time
Answer 5:在执行存储过程时,使用 with recompile 选项强制编译新的计划;使用sp_recompile系统存储过程强制在下次运行时进行重新编译

Question6: How can I add row numbers to my result set?
In database pubs, have a table titles , now I want the result shown as below,each row have a row number, how can you do that?
Result:
程序代码
line-no     title_id
----------- --------
1           BU1032
2           BU1111
3           BU2075
4           BU7832
5           MC2222
6           MC3021
7           MC3026
8           PC1035
9           PC8888
10          PC9999
11          PS1372
12          PS2091
13          PS2106
14          PS3333
15          PS7777
16          TC3218
17          TC4203
18          TC7777

Answer 6:
--SQL 2005的写法
select row_number() as line_no ,title_id from titles
--SQL 2000的写法
select line_no identity(int,1,1),title_id into #t from titles
select * from #t
drop table #t

请问在SQL2000中怎么区分登入,用户,角色,并用例子举例说明;
  再问:       (1)登入ID是不是就是用户名(当我创建了一个登入, 我在点击登入所对应的数据,实例中的用户一栏看到登入ID与用户名一致)
  (2)一个登入ID是不是只能对应一个用户 http://www.mscto.com

  教科书答案:登录 ID 仅能使您连接到 SQL Server 实例。特定数据库内的权限由用户帐户控制。数据库管理员将您的登录帐户映射到您有权访问的任何数据库中的用户帐户
  用户如:sa http://www.mscto.com

  角色如:public/db_owner/db_datareader/db_datawriter等 软件开发网

  只有给用户赋予角色,该用户才有相应的操作数据库的权限 http://www.mscto.com
  如将public/db_owner角色赋给sa,则该用户有对数据库进行一切操作的权限

  角色:完成特定的、与服务器相关的管理任务所需的权限,一个用户可以属于多个角色。
  登录:仅能使您连接到 SQL Server 实例。
  命题官的理解:
  登录是sql实例级的

  用户是数据库级的

  角色有实例级和数据库级两种

  登录决定你是否能访问sql实例

  用户与登录对应, 确实某个登录后, 它对那些数据库有那些权限. http://www.mscto.com

  角色是为了方便管理一类登录或者用户所具有的权限, 当某一类登录或者用户具有相同的权限时, 可以简单地给予他们对应的角色即可.

  做个形象的比喻:

  sql实例就相当于一个公司.
  如果你要在公司工作自然就要成为公司的员工, 因此公司的员工就相当于登录
  公司有不同的部门, 这相当于数据库, 你要在某个部门做事, 必须把你分配到某个部门, 也就是在部门的名单中要有你, 这个部门的名单就相当于用户. 名单必须对应公司的某个员工.
  但你一个人是可以在多个部门工作的 软件开发网

  因此, 一个登录可以对应多个数据库的不同用户.
  同样, 一个员工在一个部门的名单中只可能出现一次

  因此, 一个登录在同一数据库中, 只可能对应一个用户 软件开发网

  为了方便定义每个员工应该做些什么, 应该承担什么职责, 公司会定义职位, 我个职位对应的, 在数据库中就叫角色.

  因为有的职位是公司层面的, 比如懂事长, 他什么都可以管 软件开发网
  因此, 有sql实例级的角色
  也可以具体地为每个部位定义职位, 不同部门的同一名称的职位它的具体内容可以不同.

  因此,对应于sql而言, 它又有数据库级的角色
  某个员工是某个职位, 则具有该职位对应的权限与责任
  因此, 对于sql而言, 某个登录或者用户被授予某个或者某些角色, 它就具有对应的权限.

  不同职位, 在不同时期, 可以由不人担任, 换人只需要取消和授予对应人员的职位就可以了. 不用改职位定义.
  因此, 对于sql而言, 可以根据需要取消和授予某个登录或者用户的角色.

(这是管理方便性需要, 你完全可以不理会角色, 自己为用户或者登录指定权限)
原创粉丝点击