SQL基础问题整理(1)——你答对了多少?
在程序中,数据库操作是必不可少的部分,所以我们要备足数据库相关知识才能去应付程序中出现的种种问题。基于此,我特地在国外网站、博客上整理了一些问题,并附带了答案和解释、参考。为了保证“原汁原味”,我就保留了英文。大家也来看看你答对了多少?
1.SQL Server 2008 Backup
题目:Is it possible to restore a SQL Server 2008 Enterprise Edition compressed backup to a SQL Server 2008 Standard Edition?
答案:yes
解释:RESTORE from compressed backups on SQL Server 2008 Standard Edition is possible, although the backup compression feature is not supported in SQL Server 2008 Standard Edition.
参考:备份压缩 (SQL Server)
2.Identity
题目:We want to insert record into table a
create table a (a int identity(1,1))
Which statement will work?
insert into a default values
insert into a values (default)
insert into a values (1)
could not insert explicit for identity column
答案:insert into a default values
解释:An insert statement with "default values" works.
参考:INSERT
3.Dynamic SQL
题目:Sam has to run a query dynamically & get the count in a variable and do some processing based on the count. Which of the following queries will return expected output?
declare @tablevariable varchar(100)
set @tablevariable = 'Employees'
A)
declare @sql varchar(100)
Declare @cnt int
Set @sql = 'Select ' + Convert(varchar(100),@cnt) + ' =count(*) from ' + @tablevariable
Exec (@sql) select @cnt
B)
declare @sql varchar(100)
Declare @cnt int
Set @sql = 'Select count(*) from ' + @tablevariable
@cnt = Exec (@sql) select @cnt
C)
DECLARE @sql nvarchar(4000), @params nvarchar(4000), @count int
SELECT @sql = N' SELECT @cnt = COUNT(*) from dbo.' + quotename(@tablevariable)
SELECT @params = N'@cnt int OUTPUT'
EXEC sp_executesql @sql, @params, @cnt = @count OUTPUT select @count
答案:C
解释:For getting a variable as output in a dynamic statement, we need to use sp_executeSQL.
参考:Introducing Dynamic SQL
4.T-SQL Output Clause
题目:Executing the
相关文档:
SELECT EXECUTIONS , DISK_READS, BUFFER_GETS,
ROUND((BUFFER_GETS-DISK_READS)/BUFFER_GETS,2) Hit_radio,
ROUND(DISK_READS/EXECUTIONS,2) Reads_per_run,
SQL_TEXT
from V$SQLAREA
WHERE EXECUTIONS>0
AND BUFFER_GETS > 0
AND (BUFFER_GETS-DISK_READS)/BUFFER_GETS ......
SELECT语句的完整语法为:
SELECT[ALL|DISTINCT|DISTINCTROW|TOP]
{*|talbe.*|[table.]field1[AS alias1][,[table.]field2[AS alias2][,…]]}
from tableexpression[,…][IN externaldatabase]
[WHERE…]
[GROUP BY…]
[HAVING…]
[ORDER BY…]
[WITH OWNERACCESS OPTION]
注 ......
一、建立链接服务器
有人喜欢调用系统过程来建立,但我个人对系统过程没有特别的学习 ,所以用的是界面设置,当然有兴趣也可以研究一下的,因为可以把SQL执行导出来。
USE [master]
GO
EXEC master.dbo.sp_addlinkedserver @server = N'TEST2', @srvproduct=N'ORCL', @provider=N ......
最近在做一个数据统计,要求对一个表中的数据按照两个维度呈现,也就是传统的交叉表
比如,有一个问题表,有三个字段,(标题、问题类别、问题状态)
要求按照不同的类别,分别统计处各个状态的问题数量(如:产品问题中未处理的数量、服务问题中遗留问题数量等等)。
经过查找和尝试,终于生成了结果,现在分享给大家。 ......
......