根据提供的文件信息,我们可以整理出以下五个与数据库相关的知识点: ### 知识点一:计算一个月中的天数 **问题1:** 是否可以使用批量 SQL 或存储过程来计算一个月中的天数? **解答1:** 一种方法是使用 `datepart` 和 `dateadd` 函数结合来计算指定月份最后一天的日期,再通过获取该日期的天数部分来得出一个月的总天数。 ```sql SELECT DATEPART(DAY, DATEADD(DAY, -1, DATEADD(MONTH, 1, CAST(CAST(YEAR(GETDATE()) AS VARCHAR) + '-' + CAST(MONTH(GETDATE()) AS VARCHAR) + '-01' AS DATETIME)))) ``` 这里首先构造了一个格式为 "YYYY-MM-01" 的字符串,表示当前月的第一天,然后通过 `DATEADD` 函数将月份增加 1(即跳转到下个月),接着再将天数减去 1 天(即回到上个月的最后一日),最后使用 `DATEPART` 函数获取这一天的天数部分,即得到一个月的总天数。 ### 知识点二:条件性显示价格区间 **问题2:** 如何使用 SQL 语句,对于价格在 $10 到 $20 之间的书籍打印 "10to20",对于价格为 null 的书籍打印 "unknown",对于其他价格的书籍打印 "other"? **解答2:** ```sql SELECT bookid, bookname, price = CASE WHEN price IS NULL THEN 'unknown' WHEN price BETWEEN 10 AND 20 THEN '10to20' ELSE 'other' END FROM books ``` 这个查询使用了 `CASE` 语句来根据不同的价格区间返回相应的文本。当价格为 null 时返回 "unknown",当价格位于 $10 到 $20 之间时返回 "10to20",否则返回 "other"。 ### 知识点三:查找重复的作者姓氏 **问题3:** 如何查找具有相同姓氏的作者? **解答3:** ```sql SELECT au_lname, number_dups = COUNT(1) FROM authors GROUP BY au_lname HAVING COUNT(1) > 1; ``` 此查询通过 `GROUP BY` 按照作者姓氏分组,并使用 `COUNT` 函数统计每个姓氏的数量。通过 `HAVING` 子句进一步过滤出姓氏重复次数大于 1 的结果。 ### 知识点四:创建交叉表报告 **问题4:** 如何在我的 SQL Server 中创建一个交叉表报告? **解答4:** 假设我们有一个名为 `sales` 的表记录所有商店的销售详情,以及一个名为 `stores` 的表记录所有商店的信息,可以通过以下 SQL 查询创建所需的交叉表报告: ```sql WITH SalesByStoreQuarter AS ( SELECT s.stor_name, YEAR(sales.ord_date) AS ord_year, DATEPART(QUARTER, sales.ord_date) AS ord_quarter, SUM(sales.qty) AS TotalQty FROM sales INNER JOIN stores s ON sales.store_id = s.store_id WHERE YEAR(sales.ord_date) = 1993 GROUP BY s.stor_name, YEAR(sales.ord_date), DATEPART(QUARTER, sales.ord_date) ) SELECT stor_name, SUM(CASE WHEN ord_quarter = 1 THEN TotalQty ELSE 0 END) AS Qtr1, SUM(CASE WHEN ord_quarter = 2 THEN TotalQty ELSE 0 END) AS Qtr2, SUM(CASE WHEN ord_quarter = 3 THEN TotalQty ELSE 0 END) AS Qtr3, SUM(CASE WHEN ord_quarter = 4 THEN TotalQty ELSE 0 END) AS Qtr4, SUM(TotalQty) AS Total FROM SalesByStoreQuarter GROUP BY stor_name UNION ALL SELECT 'Total', SUM(Qtr1), SUM(Qtr2), SUM(Qtr3), SUM(Qtr4), SUM(Total) FROM ( SELECT SUM(CASE WHEN ord_quarter = 1 THEN TotalQty ELSE 0 END) AS Qtr1, SUM(CASE WHEN ord_quarter = 2 THEN TotalQty ELSE 0 END) AS Qtr2, SUM(CASE WHEN ord_quarter = 3 THEN TotalQty ELSE 0 END) AS Qtr3, SUM(CASE WHEN ord_quarter = 4 THEN TotalQty ELSE 0 END) AS Qtr4, SUM(TotalQty) AS Total FROM SalesByStoreQuarter ) AS subquery; ``` 此查询首先使用 `WITH` 语句创建了一个临时表 `SalesByStoreQuarter`,其中包含了每家商店每个季度的总销量。接着使用多个 `SUM(CASE)` 表达式来生成每个季度的总销量,最后通过 `UNION ALL` 添加了总计行。 ### 知识点五:快速重新编译所有存储过程 **问题5:** 在 SQL Server 6.5 中如何快速地重新编译所有的存储过程? **解答5:** 如果在 SQL Server 6.5 中遇到某个存储过程导致的问题,但不确定具体是哪一个存储过程,则可以考虑使用 `sp_recompile` 存储过程来逐个重新编译所有的存储过程。 ```sql DECLARE @procname SYSNAME; DECLARE proc_cursor CURSOR FOR SELECT name FROM sys.sysprocedures WHERE type IN (N'P', N'T'); OPEN proc_cursor; FETCH NEXT FROM proc_cursor INTO @procname; WHILE @@FETCH_STATUS = 0 BEGIN EXEC sp_recompile @objname = @procname; FETCH NEXT FROM proc_cursor INTO @procname; END CLOSE proc_cursor; DEALLOCATE proc_cursor; ``` 此脚本首先定义了一个游标 `proc_cursor` 来遍历系统目录视图 `sys.sysprocedures` 中的所有存储过程。接着使用 `EXEC sp_recompile` 对每一个存储过程进行重新编译。这样可以确保所有存储过程都被重新编译,有助于解决由过期的执行计划引起的问题。 以上五个知识点涵盖了从基本的日期计算、条件判断到更复杂的交叉表报告生成以及存储过程管理等多个方面,希望对您有所帮助。
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实现
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:
剩余6页未读,继续阅读
- 粉丝: 5
- 资源: 18
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助